import type { ViewDestination } from "@berry/api/web.ts"; let guard: ((destination: ViewDestination) => boolean | Promise) | null = null; export function guardNavigation(check: NonNullable) { guard = check; return () => { if (guard === check) guard = null; }; } export function beforeNavigate(route: string): boolean | Promise { const [tid, extension, ...parts] = route.split("/"); const decode = (part: string) => { try { return decodeURIComponent(part); } catch { return part; } }; return guard?.({ tid: /^[a-z2-7]{13}$/.test(tid) ? tid : null, extension: extension ?? null, route: parts.map(decode).join("/"), }) ?? true; }