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

ThreadVisitWatermark.tsx

86 lines3.8 KB

1import { type ScopedThreadRef } from "@t3tools/contracts";
2import { resolveThreadLastVisitedAt } from "../Sidebar.logic";
3import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment";
4import { useEffect, useRef } from "react";
5import { useUiStateStore } from "../../uiStateStore";
6import { threadEnvironment } from "../../state/threads";
7import { useThreadShell } from "../../state/entities";
8import { useAtomCommand } from "../../state/use-atom-command";
9​
10// During an active turn the thread's updatedAt advances several times per
11// second, and every server-side visit is a full command dispatch plus a
12// broadcast to all shell subscribers.
13const VISIT_DISPATCH_THROTTLE_MS = 10_000;
14​
15// The visit watermark is the one part of the chat view that follows the
16// shell's updatedAt, which ticks while a run streams. Keeping it in a leaf lets
17// ChatView read the quiet shell.
18export function ThreadVisitWatermark(props: { readonly threadRef: ScopedThreadRef }) {
19 const serverThread = useThreadShell(props.threadRef);
20 const threadKey = scopedThreadKey(props.threadRef);
21 const markThreadVisited = useUiStateStore((store) => store.markThreadVisited);
22 const activeThreadLocalLastVisitedAt = useUiStateStore(
23 (store) => store.threadLastVisitedAtById[threadKey],
24 );
25 const visitThreadMutation = useAtomCommand(threadEnvironment.visit, { reportFailure: false });
26 const lastDispatchedVisitRef = useRef<string | null>(null);
27 const lastVisitDispatchAtRef = useRef(0);
28​
29 useEffect(() => {
30 if (!serverThread?.id) return;
31 const threadUpdatedAt = Date.parse(serverThread.updatedAt);
32 if (Number.isNaN(threadUpdatedAt)) return;
33 const effectiveLastVisitedAt = resolveThreadLastVisitedAt(
34 serverThread.lastVisitedAt,
35 activeThreadLocalLastVisitedAt,
36 );
37 const lastVisitedAt = effectiveLastVisitedAt ? Date.parse(effectiveLastVisitedAt) : NaN;
38 if (!Number.isNaN(lastVisitedAt) && lastVisitedAt >= threadUpdatedAt) return;
39​
40 if (serverThread.lastVisitedAt !== undefined) {
41 // Dedupe per watermark — the effect re-runs before the command's echo
42 // lands. The dedupe also keeps a mark-unread on the open thread sticky:
43 // the rewind leaves updatedAt untouched, so the already-dispatched key
44 // skips a fresh visit until new activity lands or the thread is reopened.
45 const dispatchKey = `${threadKey}:${serverThread.updatedAt}`;
46 if (lastDispatchedVisitRef.current === dispatchKey) return;
47 const dispatch = () => {
48 lastDispatchedVisitRef.current = dispatchKey;
49 lastVisitDispatchAtRef.current = Date.now();
50 void visitThreadMutation({
51 environmentId: serverThread.environmentId,
52 input: { threadId: serverThread.id, visitedAt: serverThread.updatedAt },
53 });
54 };
55 const latestRunCompletedAtMs = serverThread.latestRun?.completedAt
56 ? Date.parse(serverThread.latestRun.completedAt)
57 : NaN;
58 const hasUnseenCompletion =
59 !Number.isNaN(latestRunCompletedAtMs) &&
60 (Number.isNaN(lastVisitedAt) || latestRunCompletedAtMs > lastVisitedAt);
61 const elapsed = Date.now() - lastVisitDispatchAtRef.current;
62 if (hasUnseenCompletion || elapsed >= VISIT_DISPATCH_THROTTLE_MS) {
63 dispatch();
64 return;
65 }
66 const timer = setTimeout(dispatch, VISIT_DISPATCH_THROTTLE_MS - elapsed);
67 return () => clearTimeout(timer);
68 }
69​
70 markThreadVisited(
71 scopedThreadKey(scopeThreadRef(serverThread.environmentId, serverThread.id)),
72 serverThread.updatedAt,
73 );
74 }, [
75 activeThreadLocalLastVisitedAt,
76 markThreadVisited,
77 threadKey,
78 serverThread?.environmentId,
79 serverThread?.id,
80 serverThread?.lastVisitedAt,
81 serverThread?.latestRun?.completedAt,
82 serverThread?.updatedAt,
83 visitThreadMutation,
84 ]);
85 return null;