49 lines
1.9 KiB
TypeScript
49 lines
1.9 KiB
TypeScript
"use client"
|
|
|
|
// The project's documents and one document, live (umbrella TS6.4 and TS6.6): the page's loader reads them
|
|
// and its view hands them over as the seed, so the page arrives complete; these keep them current through
|
|
// the organization's live stream. The job's change of a document -- taken, read, failed -- is a change of
|
|
// `documents.document` (contract/collections.ts), and each read again answers the files, the rows and the
|
|
// line counts together, under the list's permission. An upload or a decision on a line is this person's
|
|
// own write: the component that made it reads again (`refetch`) once it has answered, so nothing reloads.
|
|
|
|
import type { ProjectSlugs } from "@conusai-kit/ui"
|
|
import { useRead } from "@conusai-kit/ui/data"
|
|
import { DOCUMENT_COLLECTION, type DocumentView, type LineView } from "../contract/index.js"
|
|
|
|
const liveness = (slugs: ProjectSlugs) => ({
|
|
org: slugs.org,
|
|
topic: { ws: slugs.workspace, project: slugs.project },
|
|
entities: [DOCUMENT_COLLECTION.entity],
|
|
})
|
|
|
|
export function useLiveDocuments(
|
|
slugs: ProjectSlugs,
|
|
seed: readonly DocumentView[],
|
|
): { documents: readonly DocumentView[]; refetch(): Promise<void> } {
|
|
const { data, refetch } = useRead<{ documents: readonly DocumentView[] }>(
|
|
DOCUMENT_COLLECTION.list,
|
|
{ ...slugs },
|
|
{ live: liveness(slugs), initial: { documents: seed } },
|
|
)
|
|
return { documents: data?.documents ?? seed, refetch }
|
|
}
|
|
|
|
export interface DocumentWithLines {
|
|
readonly document: DocumentView
|
|
readonly lines: readonly LineView[]
|
|
}
|
|
|
|
export function useLiveDocument(
|
|
slugs: ProjectSlugs,
|
|
fileId: string,
|
|
seed: DocumentWithLines,
|
|
): DocumentWithLines & { refetch(): Promise<void> } {
|
|
const { data, refetch } = useRead<DocumentWithLines>(
|
|
"documents.document.get",
|
|
{ ...slugs, fileId },
|
|
{ live: liveness(slugs), initial: seed },
|
|
)
|
|
return { ...(data ?? seed), refetch }
|
|
}
|