Open History to see the last commit
1import type { ScopedThreadRef } from "@t3tools/contracts";
2import { useParams } from "@tanstack/react-router";
3
4import {
5 resolveThreadRouteRef,
6 resolveThreadRouteTarget,
7 type ThreadRouteTarget,
8} from "../threadRoutes";
9
10// The router re-runs selectors on every store update (preloads, location state,
11// same-route navigations) and compares with ===. Selecting the string params keeps
12// those updates from reaching consumers; the target is then built once per id change.
13function useThreadRouteIds() {
14 const environmentId = useParams({ strict: false, select: (params) => params.environmentId });
15 const threadId = useParams({ strict: false, select: (params) => params.threadId });
16 return { environmentId, threadId };
17}
18
19export function useThreadRouteTarget(): ThreadRouteTarget | null {
20 const { environmentId, threadId } = useThreadRouteIds();
21 const draftId = useParams({ strict: false, select: (params) => params.draftId });
22 return resolveThreadRouteTarget({ environmentId, threadId, draftId });
23}
24
25export function useThreadRouteRef(): ScopedThreadRef | null {
26 return resolveThreadRouteRef(useThreadRouteIds());
27}