frontend: generar titulo de página

This commit is contained in:
Cat /dev/Nulo 2023-12-08 20:39:16 -03:00
parent 2ac17883a9
commit c9347b583b
2 changed files with 19 additions and 7 deletions

View file

@ -1,7 +1,7 @@
<script lang="ts">
import { inject } from "regexparam";
import ChevronRight from "eva-icons/outline/svg/chevron-right-outline.svg?component";
import { routes } from "../router";
import { generateDumpName, routes } from "../router";
import Portal from "../routes/Portal.svelte";
export let params:
@ -9,11 +9,6 @@
| { dumpUrl: string; portal: string }
| { dumpUrl: string; portal: string; id: string };
function generateDumpName(dumpUrl: string) {
const clean = decodeURIComponent(dumpUrl).replace(/\/+$/, "");
return clean.slice(clean.lastIndexOf("/") + 1);
}
$: dumpName = generateDumpName(params.dumpUrl);
</script>

View file

@ -8,13 +8,30 @@ export const routes = {
Dataset: "/dump/:dumpUrl/:portal/dataset/:id",
};
export function generateDumpName(dumpUrl: string) {
const clean = decodeURIComponent(dumpUrl).replace(/\/+$/, "");
return clean.slice(clean.lastIndexOf("/") + 1);
}
const title = "Archivo de portales de datos de Argentina";
const titles: { [key in ComponentType]: (params: Params) => string } = {
Home: () => title,
NotFound: () => title,
Dump: (params) => `${generateDumpName(params.dumpUrl)} - ${title}`,
Portal: (params) => `${params.portal} - ${title}`,
Dataset: (params) => `${params.id} - ${title}`,
};
export type ComponentType = "NotFound" | keyof typeof routes;
type Route = {
component: ComponentType;
params?: Params;
};
export const currentRoute = writable<Route>();
export const currentRoute = writable<Route>({ component: "Home" });
currentRoute.subscribe(
(route) => (document.title = titles[route.component](route.params!)),
);
export const router = navaid(undefined, () =>
currentRoute.set({ component: "NotFound" }),