Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

useSettingsSectionPrefetch.ts

42 lines1.8 KB

1import { RegistryContext } from "@effect/atom-react";
2import { useRouter } from "@tanstack/react-router";
3import { useContext } from "react";
4​
5import { peerLinkEnvironment, usePeerLinkHosts } from "../../state/peerLinks";
6import { serverEnvironment } from "../../state/server";
7import { sourceControlEnvironment } from "../../state/sourceControl";
8import type { SettingsPath } from "./settingsSearch";
9import { useOptionalSettingsScope } from "./SettingsScopeContext";
10​
11/**
12 * Starts loading a settings section on hover or focus: its lazy route chunk, plus the slow
13 * queries it opens with. Each query keeps its result for its idle TTL, so mounting and
14 * releasing at once is enough for the page to find it warm.
15 */
16export function useSettingsSectionPrefetch() {
17 const router = useRouter();
18 const registry = useContext(RegistryContext);
19 const scope = useOptionalSettingsScope();
20 const environment = scope?.environment ?? null;
21 const peerLinkHosts = usePeerLinkHosts();
22 return (to: SettingsPath) => {
23 void router.preloadRoute({ to });
24 if (to === "/settings/source-control" && environment?.connection.phase === "connected") {
25 const input = { environmentId: environment.environmentId, input: {} };
26 registry.mount(sourceControlEnvironment.discovery(input))();
27 }
28 if (to === "/settings/scheduled-tasks") {
29 for (const entry of scope?.environments ?? []) {
30 if (entry.connection.phase !== "connected" || entry.serverConfig === null) continue;
31 const input = { environmentId: entry.environmentId, input: {} };
32 registry.mount(serverEnvironment.scheduledTasksLive(input))();
33 }
34 }
35 if (to === "/settings/connections") {
36 for (const host of peerLinkHosts) {
37 const input = { environmentId: host.environmentId, input: {} };
38 registry.mount(peerLinkEnvironment.list(input))();
39 }
40 }
41 };