import { FreshContext } from "$fresh/server.ts"; import { Route, State } from "$root/defaults/interfaces.ts"; import { ComponentChildren } from "preact"; /** * Generates a catch-all [slug] route that dynamically loads partials. * This enables direct URL navigation to sub-pages (e.g. /admin/modules). * @param basePath The base path of the module, should be `import.meta.dirname!`. * @returns A route handler that loads the partial matching the slug. */ export default function makeSlug(basePath: string): Route { return async function SlugRoute( request: Request, context: FreshContext, ): Promise { const slug = context.params.slug; // Try partials/.tsx, then partials/(admin)/.tsx let page: Route | undefined; try { page = (await import(`${basePath}/partials/${slug}.tsx`)).Page; } catch { try { page = (await import(`${basePath}/partials/(admin)/${slug}.tsx`)).Page; } catch { // No partial found for this slug } } if (!page) { return context.renderNotFound(); } return page(request, context); }; }