From 6e49007ffbb424fdd9e9c4c02925b890db801562 Mon Sep 17 00:00:00 2001 From: Allan Lasser Date: Wed, 24 Apr 2024 12:44:42 -0400 Subject: [PATCH] Attempting a different document loading strategy for projects --- src/lib/api/projects.ts | 5 +- src/lib/api/types.d.ts | 1 + .../app/projects/[id]-[slug]/+page.svelte | 66 +++++++++++++++---- src/routes/app/projects/[id]-[slug]/+page.ts | 6 +- 4 files changed, 61 insertions(+), 17 deletions(-) diff --git a/src/lib/api/projects.ts b/src/lib/api/projects.ts index 7869bf5d0..84b88c50a 100644 --- a/src/lib/api/projects.ts +++ b/src/lib/api/projects.ts @@ -1,5 +1,6 @@ // api methods for projects -import type { Project, ProjectResults, ProjectMembershipList } from "./types"; +import type { Page } from "@/api/types"; +import type { Project, ProjectResults, Document } from "./types"; import { error, type NumericRange } from "@sveltejs/kit"; @@ -134,7 +135,7 @@ export async function pinProject( export async function documents( id: number | string, fetch = globalThis.fetch, -): Promise { +): Promise> { const endpoint = new URL(`projects/${id}/documents/`, BASE_API_URL); const expand = ["user", "organization", "document"]; diff --git a/src/lib/api/types.d.ts b/src/lib/api/types.d.ts index 8ac1a6c18..54c1221ab 100644 --- a/src/lib/api/types.d.ts +++ b/src/lib/api/types.d.ts @@ -166,6 +166,7 @@ export interface SearchOptions { cursor?: string; expand?: string; version?: number | string; + project?: number | string; } export interface OEmbed { diff --git a/src/routes/app/projects/[id]-[slug]/+page.svelte b/src/routes/app/projects/[id]-[slug]/+page.svelte index 5c68bbeef..4e33c268c 100644 --- a/src/routes/app/projects/[id]-[slug]/+page.svelte +++ b/src/routes/app/projects/[id]-[slug]/+page.svelte @@ -1,6 +1,5 @@ @@ -33,21 +47,45 @@ - {#await documents} + + + + {#await documentSearch} Loading project documents… - {:then projectItems} -
- {#each projectItems.results as { document }} - {#if typeof document !== "number"} - - {/if} - {:else} - This project has no documents. - {/each} -
+ {:then documentSearchResults} + {:catch} Error loading project documents {/await} + + + + + {#if $visible && $total} + Showing {$visible.size.toLocaleString()} of {$total.toLocaleString()} + results + {/if} + +
diff --git a/src/routes/app/projects/[id]-[slug]/+page.ts b/src/routes/app/projects/[id]-[slug]/+page.ts index a0ec4862b..b8ba2b18a 100644 --- a/src/routes/app/projects/[id]-[slug]/+page.ts +++ b/src/routes/app/projects/[id]-[slug]/+page.ts @@ -1,4 +1,5 @@ import * as projectApi from "$lib/api/projects"; +import { search } from "$lib/api/documents"; import { breadcrumbTrail } from "$lib/utils/navigation"; export async function load({ params, url, parent, fetch }) { @@ -7,10 +8,13 @@ export async function load({ params, url, parent, fetch }) { const breadcrumbs = await breadcrumbTrail(parent, [ { href: url.pathname, title: project.title }, ]); - const documents = projectApi.documents(id, fetch); + const query = url.searchParams.get("q"); + const cursor = url.searchParams.get("cursor"); + const documents = search(query, { cursor, project: project.id }); return { breadcrumbs, project, documents, + query, }; }