Open History to see the last commit
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 { useThreadHtmlPreviewTheme } from "../../hooks/useHtmlRenderTheme";
9import { getDocumentActivity, subscribeDocumentActivity } from "../../lib/documentActivity";
10import { useUiStateStore } from "../../uiStateStore";
11import { environmentThreadShells, threadEnvironment } from "../../state/threads";
12
13const isDocumentViewed = () => {
14 const activity = getDocumentActivity();
15 return activity.visible && activity.focused;
16};
17
18const documentViewedAtom = Atom.make((get) => {
19 get.addFinalizer(subscribeDocumentActivity(() => get.setSelf(isDocumentViewed())));
20 return isDocumentViewed();
21}).pipe(Atom.withLabel("document-viewed"));
22
23const threadVisitTracker = createThreadVisitTracker({
24 threadShellAtom: environmentThreadShells.threadShellAtom,
25 sessionGenerationAtom: environmentCatalog.sessionGenerationAtom,
26 visit: threadEnvironment.visit,
27 markLocalVisit: (ref, visitedAt) =>
28 useUiStateStore.getState().markThreadVisited(scopedThreadKey(ref), visitedAt),
29 viewingAtom: documentViewedAtom,
30});
31
32// The visit watermark is the one part of the chat view that follows the
33// shell's updatedAt, which ticks while a run streams. The tracker subscribes to
34// it outside React, so ChatView keeps reading the quiet shell.
35export function ThreadVisitWatermark(props: { readonly threadRef: ScopedThreadRef }) {
36 useAtomMount(threadVisitTracker(props.threadRef));
37 useThreadHtmlPreviewTheme(props.threadRef);
38 return null;
39}