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

ThreadVisitWatermark.tsx

37 lines1.7 KB

1import { useAtomMount } from "@effect/atom-react";
2import { scopedThreadKey } from "@t3tools/client-runtime/environment";
3import { createThreadVisitTracker } from "@t3tools/client-runtime/state/thread-visits";
4import { type ScopedThreadRef } from "@t3tools/contracts";
5import { Atom } from "effect/reactivity";
6​
7import { environmentCatalog } from "../../connection/catalog";
8import { getDocumentActivity, subscribeDocumentActivity } from "../../lib/documentActivity";
9import { useUiStateStore } from "../../uiStateStore";
10import { environmentThreadShells, threadEnvironment } from "../../state/threads";
11​
12const isDocumentViewed = () => {
13 const activity = getDocumentActivity();
14 return activity.visible && activity.focused;
15};
16​
17const documentViewedAtom = Atom.make((get) => {
18 get.addFinalizer(subscribeDocumentActivity(() => get.setSelf(isDocumentViewed())));
19 return isDocumentViewed();
20}).pipe(Atom.withLabel("document-viewed"));
21​
22const threadVisitTracker = createThreadVisitTracker({
23 threadShellAtom: environmentThreadShells.threadShellAtom,
24 sessionGenerationAtom: environmentCatalog.sessionGenerationAtom,
25 visit: threadEnvironment.visit,
26 markLocalVisit: (ref, visitedAt) =>
27 useUiStateStore.getState().markThreadVisited(scopedThreadKey(ref), visitedAt),
28 viewingAtom: documentViewedAtom,
29});
30​
31// The visit watermark is the one part of the chat view that follows the
32// shell's updatedAt, which ticks while a run streams. The tracker subscribes to
33// it outside React, so ChatView keeps reading the quiet shell.
34export function ThreadVisitWatermark(props: { readonly threadRef: ScopedThreadRef }) {
35 useAtomMount(threadVisitTracker(props.threadRef));
36 return null;