Skip to content

Commit

Permalink
added page title update on hash change
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienR1 committed Jan 30, 2025
1 parent 1894c23 commit 6701cca
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 13 deletions.
28 changes: 23 additions & 5 deletions packages/web/src/layouts/Layout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@
import Header from "../components/Header.astro";
import Footer from "../components/Footer.astro";
import TopArrow from "../components/TopArrow.astro";
import type { Routes } from "../routes/router";
import { routerLabels } from "../routes/router";
import { asArray } from "../lib/utils";
type Path = Routes<["footer-only", "disabled"]>;
interface Props {
title?: string;
path: Path | Path[];
}
let title = "Trampoline Intercité";
if (Astro.props.title) {
title += " | " + Astro.props.title;
}
const paths = asArray(Astro.props.path);
const labels = paths.reduce(
(acc, path) => ({ ...acc, [path]: routerLabels[path] }),
{} as Record<Path, string>,
);
const label = labels[paths[0]];
const title = "Trampoline Intercité" + (label ? ` | ${label}` : "");
---

<!doctype html>
Expand Down Expand Up @@ -94,3 +103,12 @@ if (Astro.props.title) {
}
});
</script>

<script is:inline define:vars={{ labels }}>
addEventListener("hashchange", function () {
const path = window.location.pathname + window.location.hash;
if (labels[path]) {
document.title = document.title.replace(/\| .*/, `| ${labels[path]}`);
}
});
</script>
3 changes: 3 additions & 0 deletions packages/web/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function asArray<T>(x: T | T[]) {
return Array.isArray(x) ? x : [x]
}
2 changes: 1 addition & 1 deletion packages/web/src/pages/404.astro
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Logo from "../components/Logo.astro";
import Layout from "../layouts/Layout.astro";
---

<Layout title="404">
<Layout path="/404">
<div
class="flex items-center flex-col mt-[30%] max-w-52 mx-auto sm:flex-row sm:max-w-96 sm:gap-8"
>
Expand Down
2 changes: 1 addition & 1 deletion packages/web/src/pages/horaire.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
import Layout from "../layouts/Layout.astro";
---

<Layout title="Horaire">horaire</Layout>
<Layout path="/horaire">horaire</Layout>
2 changes: 1 addition & 1 deletion packages/web/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
import Layout from "../layouts/Layout.astro";
---

<Layout title="Accueil">INDEX</Layout>
<Layout path="/">INDEX</Layout>
7 changes: 5 additions & 2 deletions packages/web/src/routes/router.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { FilterRoutes, ListRoutes, RouteArray, RouteModifer } from "./routes"
import { filter } from "./routes"
import { filter, labels } from "./routes"

export const router = [
{ path: "/", label: "Accueil", modifiers: ["footer-only"] },
Expand All @@ -26,10 +26,13 @@ export const router = [
]
},
{ path: "/horaire", label: "Horaire", icon: "date_range" },
{ path: "/contact", label: "Contact", icon: "call" }
{ path: "/contact", label: "Contact", icon: "call" },
{ path: "/404", label: "404", modifiers: ['disabled'] }
] as const
router satisfies RouteArray

export const headerRouter = filter(['header-only'], router)
export const footerRouter = filter(['footer-only'], router)
export type Routes<M extends RouteModifer[] = []> = ListRoutes<FilterRoutes<typeof router, M>>

export const routerLabels = labels(router)
13 changes: 10 additions & 3 deletions packages/web/src/routes/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,20 @@ export const filter = <R extends RouteArray, M extends readonly RouteModifer[]>(
.filter(route => (route.modifiers ?? []).every(modifier => allowed.includes(modifier)))
.map(route => "subroutes" in route ? ({ ...route, subroutes: filter(allowed, route.subroutes!) }) : route) as FilterRoutes<R, M>

export type Labels<R extends RouteArray> = { [key in ListRoutes<R>]: string }

export const labels = <R extends RouteArray>(routes: R): Labels<R> => {
return routes.reduce((acc, route) => ({
...acc,
[route.path]: route.label,
...('subroutes' in route ? labels(route.subroutes!) : {})
}), {} as Labels<R>)
}

export type ListRoutes<R extends RouteArray> =
R extends readonly [infer First extends Route, ...infer Rest extends RouteArray] ?
First['path'] |
ListRoutes<Rest> | (
First['subroutes'] extends RouteArray ?
ListRoutes<First['subroutes']> : never
) : never



0 comments on commit 6701cca

Please sign in to comment.