@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 72 lines 2.39 KB
Raw
1 import type { PullRequestFileViewedState, PullRequestFilesViewedResult } from "@t3tools/contracts";
2
3 export type FileViewedStates = ReadonlyMap<string, PullRequestFileViewedState>;
4
5 export type FileViewedOverlay = ReadonlyMap<string, boolean>;
6
7 export function toFileViewedStates(
8 result: PullRequestFilesViewedResult | null,
9 ): FileViewedStates | null {
10 if (result === null) return null;
11 return new Map(result.files.map((file) => [file.path, file.state]));
12 }
13
14 function isViewedState(state: PullRequestFileViewedState | undefined): boolean {
15 return state === "viewed";
16 }
17
18 export function isStaleViewedState(state: PullRequestFileViewedState | undefined): boolean {
19 return state === "dismissed";
20 }
21
22 export function isFileViewed(
23 path: string,
24 states: FileViewedStates | null,
25 overlay: FileViewedOverlay,
26 ): boolean {
27 const pressed = overlay.get(path);
28 return pressed ?? isViewedState(states?.get(path));
29 }
30
31 /** Retain presses until a read could have observed the write. */
32 export function settleFileViewedOverlay(
33 overlay: FileViewedOverlay,
34 states: FileViewedStates | null,
35 pending: ReadonlySet<string>,
36 answered: ReadonlySet<string>,
37 ): FileViewedOverlay {
38 if (states === null || overlay.size === 0) return overlay;
39 const next = new Map(overlay);
40 for (const [path, pressed] of overlay) {
41 if (pending.has(path)) continue;
42 if (answered.has(path) || isViewedState(states.get(path)) === pressed) next.delete(path);
43 }
44 return next.size === overlay.size ? overlay : next;
45 }
46
47 /** Revert only owned paths; later presses belong to other requests. */
48 export function revertFileViewedOverlay(
49 overlay: FileViewedOverlay,
50 batch: ReadonlyArray<{ readonly path: string; readonly viewed: boolean }>,
51 owned: ReadonlySet<string>,
52 ): FileViewedOverlay {
53 const next = new Map(overlay);
54 for (const { path, viewed } of batch) {
55 if (!owned.has(path)) continue;
56 if (next.get(path) === viewed) next.delete(path);
57 }
58 return next.size === overlay.size ? overlay : next;
59 }
60
61 export interface FileViewedPress {
62 readonly viewed: boolean;
63 readonly revision: string | undefined;
64 }
65
66 export function toFileViewedBatch(
67 presses: ReadonlyMap<string, FileViewedPress>,
68 ): ReadonlyArray<{ readonly path: string; readonly viewed: boolean; readonly revision?: string }> {
69 return [...presses].map(([path, { viewed, revision }]) =>
70 revision === undefined ? { path, viewed } : { path, viewed, revision },
71 );
72 }