| 1 |
import { type Component, mount, unmount } from "svelte"; |
| 2 |
|
| 3 |
import { log_error } from "../log"; |
| 4 |
import ReportLoadError from "./ReportLoadError.svelte"; |
| 5 |
import { updateable_props } from "./route.svelte"; |
| 6 |
|
| 7 |
export interface FrontendRoute { |
| 8 |
readonly report: string; |
| 9 |
readonly title: string; |
| 10 |
destroy(): void; |
| 11 |
render( |
| 12 |
target: HTMLElement, |
| 13 |
url: URL, |
| 14 |
previous?: FrontendRoute, |
| 15 |
): Promise<void>; |
| 16 |
} |
| 17 |
|
| 18 |
/** This class pairs the components and their load functions to use them in a type-safe way. */ |
| 19 |
// The base type for the component props needs to be typed as Record<string,any> to allow for T |
| 20 |
// to be correctly inferred from the imported svelte components |
| 21 |
// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 22 |
export class Route<T extends Record<string, any>> implements FrontendRoute { |
| 23 |
/** The currently rendered instance - if loading failed, we render an error component. */ |
| 24 |
private instance?: |
| 25 |
| { |
| 26 |
error: false; |
| 27 |
component: Record<string, unknown>; |
| 28 |
update_props: (v: T) => void; |
| 29 |
} |
| 30 |
| { error: true; component: Record<string, unknown> }; |
| 31 |
|
| 32 |
/** The currently rendered URL. */ |
| 33 |
url?: URL; |
| 34 |
|
| 35 |
/** |
| 36 |
* Create a new frontend-rendered route. |
| 37 |
* @param report URL slug of this report. |
| 38 |
* @param Component the component to render for this report. |
| 39 |
* @param load function to load the necessary data. |
| 40 |
* @param get_title function to get the page title. |
| 41 |
*/ |
| 42 |
constructor( |
| 43 |
readonly report: string, |
| 44 |
private readonly Component: Component<T>, |
| 45 |
private readonly load: (url: URL) => T | Promise<T>, |
| 46 |
private readonly get_title: (route: Route<T>) => string, |
| 47 |
) {} |
| 48 |
|
| 49 |
/** The title of this report. */ |
| 50 |
get title(): string { |
| 51 |
return this.get_title(this); |
| 52 |
} |
| 53 |
|
| 54 |
/** Destroy any components that might be rendered by this route. */ |
| 55 |
destroy(): void { |
| 56 |
if (this.instance) { |
| 57 |
void unmount(this.instance.component); |
| 58 |
} |
| 59 |
this.instance = undefined; |
| 60 |
} |
| 61 |
|
| 62 |
/** Load data and render the component for this route to the given target. */ |
| 63 |
async render( |
| 64 |
target: HTMLElement, |
| 65 |
url: URL, |
| 66 |
previous?: FrontendRoute, |
| 67 |
): Promise<void> { |
| 68 |
if (previous !== this) { |
| 69 |
previous?.destroy(); |
| 70 |
} |
| 71 |
try { |
| 72 |
const raw_props = await this.load(url); |
| 73 |
// Check if the component is unchanged and only update the data in this case. |
| 74 |
if (previous === this && this.instance?.error === false) { |
| 75 |
this.instance.update_props(raw_props); |
| 76 |
} else { |
| 77 |
this.destroy(); |
| 78 |
target.innerHTML = ""; |
| 79 |
const [props, update_props] = updateable_props(raw_props); |
| 80 |
this.instance = { |
| 81 |
error: false, |
| 82 |
component: mount(this.Component, { target, props }), |
| 83 |
update_props, |
| 84 |
}; |
| 85 |
} |
| 86 |
} catch (error: unknown) { |
| 87 |
log_error(error); |
| 88 |
if (error instanceof Error) { |
| 89 |
this.destroy(); |
| 90 |
target.innerHTML = ""; |
| 91 |
this.instance = { |
| 92 |
error: true, |
| 93 |
component: mount(ReportLoadError, { |
| 94 |
target, |
| 95 |
props: { title: this.title, error }, |
| 96 |
}), |
| 97 |
}; |
| 98 |
} |
| 99 |
} finally { |
| 100 |
this.url = url; |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
type NoProps = Record<string, never>; |
| 106 |
|
| 107 |
const noload = () => ({}); |
| 108 |
|
| 109 |
/** A frontend rendered route that does not need to load any props. */ |
| 110 |
export class DatalessRoute extends Route<NoProps> { |
| 111 |
constructor( |
| 112 |
report: string, |
| 113 |
Component: Component<NoProps>, |
| 114 |
get_title: (route: Route<NoProps>) => string, |
| 115 |
) { |
| 116 |
super(report, Component, noload, get_title); |
| 117 |
} |
| 118 |
} |