Open History to see the last commit
1import { parseStoryId } from "./comments";
2import { parseFeedPage } from "./types";
3
4export function liveRoute(href: string) {
5 const url = new URL(href, "https://hackernews-realtime.vercel.app");
6 if (!href.startsWith("/") || href.startsWith("//")) return null;
7 if (url.pathname === "/") {
8 const { feed, page } = parseFeedPage(
9 url.searchParams.get("feed"),
10 url.searchParams.get("page"),
11 );
12 return {
13 kind: "feed" as const,
14 key: `feed:${feed}:${page}`,
15 api: `/api/feed/live?feed=${feed}&page=${page}`,
16 };
17 }
18 if (url.pathname === "/item") {
19 const id = parseStoryId(url.searchParams.get("id"));
20 return {
21 kind: "thread" as const,
22 key: `thread:${id}`,
23 id,
24 api: `/api/thread/live?id=${id}`,
25 };
26 }
27 return null;
28}
29
30export class NavigationIntent {
31 private generation = 0;
32 begin() {
33 const generation = ++this.generation;
34 return () => generation === this.generation;
35 }
36 cancel() {
37 this.generation++;
38 }
39}