Skip to content

Commit

Permalink
Attempts re-run of load function
Browse files Browse the repository at this point in the history
  • Loading branch information
allanlasser committed Dec 2, 2024
1 parent 946d701 commit a2e408f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
5 changes: 4 additions & 1 deletion src/lib/load/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ interface Load {
fetch: typeof globalThis.fetch;
params: { id: string };
url: URL;
depends: (key: string) => void;
}

/**
* Load a document and its assets
*/
export default async function load({ fetch, params, url }: Load) {
export default async function load({ fetch, params, url, depends }: Load) {
const { data: document, error: err } = await documents.get(+params.id, fetch);

if (err) {
Expand All @@ -44,6 +45,8 @@ export default async function load({ fetch, params, url }: Load) {
search = await documents.searchWithin(document.id, query, undefined, fetch);
}

depends("document:" + params.id);

return {
document,
asset_url,
Expand Down
41 changes: 33 additions & 8 deletions src/routes/(app)/documents/[id]-[slug]/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
* We do this in a layout module because sub-routes can use the same
* document without loading it again.
*/
import type { ReadMode } from "@/lib/api/types";
import type {
APIResponse,
Highlights,
Maybe,
ViewerMode,
ReadMode,
} from "$lib/api/types";

import { redirect } from "@sveltejs/kit";
import { redirect, error } from "@sveltejs/kit";

import { VIEWER_MAX_AGE } from "@/config/config.js";
import * as documents from "$lib/api/documents";
import { breadcrumbTrail } from "$lib/utils/index";
import loadDocument from "$lib/load/document";

/** @type {import('./$types').PageLoad} */
export async function load({
Expand All @@ -21,11 +26,15 @@ export async function load({
url,
setHeaders,
}) {
const { document, asset_url, mode, search } = await loadDocument({
fetch,
params,
url,
});
const { data: document, error: err } = await documents.get(+params.id, fetch);

if (err) {
return error(err.status, err.message);
}

if (!document) {
return error(404, "Document not found");
}

depends(`document:${document.id}`);

Expand All @@ -34,6 +43,22 @@ export async function load({
redirect(302, canonical.pathname);
}

let mode: ViewerMode =
(url.searchParams.get("mode") as ViewerMode) ?? "document";
const asset_url = await documents.assetUrl(document, fetch);

if (!documents.MODES.has(mode)) {
mode = documents.MODES[0];
}

// If in search mode, get the query from the URL and
// initialize a Promise for the first page of search results
let search: Maybe<APIResponse<Highlights, null>> = undefined;
const query = url.searchParams.get("q");
if (query) {
search = await documents.searchWithin(document.id, query, undefined, fetch);
}

if (!document.edit_access && !documents.READING_MODES.has(mode as ReadMode)) {
return redirect(302, canonical);
}
Expand Down

0 comments on commit a2e408f

Please sign in to comment.