| 1 | import { LIST_PAGE } from "@g1t/contracts"; |
| 2 | |
| 3 | import type { Route } from "./+types/plan-later"; |
| 4 | import { originPath } from "../../../lib/plan"; |
| 5 | import { work } from "../../../lib/services.server"; |
| 6 | import { getViewer } from "../../../lib/session.server"; |
| 7 | import { repoFilter } from "../../../lib/work-items"; |
| 8 | |
| 9 | /** |
| 10 | * One of the Plan's Later groups, read when it is opened: the open work |
| 11 | * items on Later filed in a repository (`?repo=acme/web`), or in the |
| 12 | * workspace itself (`?repo=`), in the plan's order, at most 100. |
| 13 | */ |
| 14 | export async function loader({ params, context, request }: Route.LoaderArgs) { |
| 15 | const viewer = getViewer(context); |
| 16 | const workspace = params.owner.toLowerCase(); |
| 17 | const repoName = new URL(request.url).searchParams.get("repo")?.trim() ?? ""; |
| 18 | const repo = repoName ? repoFilter(workspace, repoName) : null; |
| 19 | if (repoName && !repo) return { error: "That is not a repository of the workspace." }; |
| 20 | const found = await work |
| 21 | .listWorkItems(workspace, viewer, { horizon: "later", limit: LIST_PAGE, ...(repo ? { repo, role: "origin" as const } : {}) }) |
| 22 | .catch(() => null); |
| 23 | if (!found) return { error: "Later could not be loaded just now." }; |
| 24 | if (!found.ok) return { error: found.error.message }; |
| 25 | // Those filed in the workspace have no origin repository. |
| 26 | const items = repo ? found.value : found.value.filter((item) => originPath(item) == null); |
| 27 | return { items, more: found.value.length >= LIST_PAGE }; |
| 28 | } |