SPB Git

Repository Public

router.tsx 3.2 KB 72 lines tsx

1/**2 * =============================================================================3 *                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : MacLustr Social Engine & Remote Navigator8 * File             : apps/social-web/src/router.tsx9 * Module           : social-web10 * Repository       : https://git.spboucher.ai/maclustr-social11 * Project Website  : https://social.spboucher.ai12 * Created          : 2026-12-0113 * Last Modified    : 2026-12-0114 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin   : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License          : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Tiny history router. The server answers every non-API GET with index.html, so plain paths work.43import { useSyncExternalStore, type AnchorHTMLAttributes, type MouseEvent } from 'react';4445const listeners = new Set<() => void>();46const emit = () => listeners.forEach((l) => l());47window.addEventListener('popstate', emit);4849const subscribe = (l: () => void) => { listeners.add(l); return () => { listeners.delete(l); }; };50const snapshot = () => window.location.pathname + window.location.search;5152export function navigate(to: string, opts: { replace?: boolean } = {}): void {53  if (to === snapshot()) return;54  if (opts.replace) window.history.replaceState(null, '', to); else window.history.pushState(null, '', to);55  emit();56  window.scrollTo(0, 0);57}58/** Current location, split into path segments and query. */59export function useRoute(): { path: string; seg: string[]; query: URLSearchParams } {60  const loc = useSyncExternalStore(subscribe, snapshot);61  const [path, search = ''] = loc.split('?');62  return { path, seg: path.split('/').filter(Boolean).map((s) => { try { return decodeURIComponent(s); } catch { return s; } }), query: new URLSearchParams(search) };63}64export function Link({ to, onClick, ...rest }: { to: string } & AnchorHTMLAttributes<HTMLAnchorElement>) {65  const click = (e: MouseEvent<HTMLAnchorElement>) => {66    onClick?.(e);67    if (e.defaultPrevented || e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return;68    e.preventDefault(); navigate(to);69  };70  return <a href={to} onClick={click} {...rest} />;71}72
Files main
Menu

Theme