Download code/web/src/lib/route.ts from MiniMaxAI/oss-ecosystem-data: direct link, hf CLI and curl.
- Browser
- Download file 912 Bytes
-
https://huggingface.co/datasets/MiniMaxAI/oss-ecosystem-data/resolve/c814034796f28656d6d7699a04f90ef1318b2038/code/web/src/lib/route.ts
- Command line
-
hf download hf://datasets/MiniMaxAI/oss-ecosystem-data@c814034796f28656d6d7699a04f90ef1318b2038/code/web/src/lib/route.ts
-
curl -L -o route.ts https://huggingface.co/datasets/MiniMaxAI/oss-ecosystem-data/resolve/c814034796f28656d6d7699a04f90ef1318b2038/code/web/src/lib/route.ts
912 Bytes
| import { useEffect, useState } from "react" | |
| /** Hash routes, because a static Space serves only index.html at the root: | |
| * #/ the landing page | |
| * #/language a category, its first release selected | |
| * #/language/m2.7 */ | |
| export type Route = { cat: string | null; rel: string | null } | |
| const parse = (): Route => { | |
| const [cat, rel] = location.hash.replace(/^#\/?/, "").split("/").map((s) => decodeURIComponent(s)) | |
| return { cat: cat || null, rel: rel || null } | |
| } | |
| export function useRoute(): Route { | |
| const [r, setR] = useState(parse) | |
| useEffect(() => { | |
| const on = () => { | |
| setR(parse()) | |
| window.scrollTo({ top: 0 }) | |
| } | |
| addEventListener("hashchange", on) | |
| return () => removeEventListener("hashchange", on) | |
| }, []) | |
| return r | |
| } | |
| export const href = (cat?: string | null, rel?: string | null) => | |
| !cat ? "#/" : rel ? `#/${cat}/${encodeURIComponent(rel)}` : `#/${cat}` | |