Open History to see the last commit
1import { errorDisplayMessage } from "@t3tools/client-runtime/errors";
2import { usePullRequestConnected } from "./usePullRequestConnected";
3import { RegistryContext, useAtomRefresh, useAtomValue } from "@effect/atom-react";
4import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
5import type { PullRequestDetail } from "@t3tools/contracts";
6import { msg } from "gt-react-native";
7import { use, useState } from "react";
8import {
9 composerPullRequests,
10 filesViewedReadsFamily,
11 filesViewedReadsKey,
12} from "../../state/pull-requests";
13import { useEnvironmentQuery } from "../../state/query";
14import { useAtomCommand } from "../../state/use-atom-command";
15import { pullRequestTarget, type PullRequestParams } from "./usePullRequestDiff";
16import { pullRequestReviewKey } from "./pullRequestReviewDrafts";
17import { viewedOverlay, type ViewedState } from "./pullRequestFilesViewed";
18import { m } from "../../i18n/messages";
19
20export function usePullRequestFilesViewed(params: PullRequestParams, pr: PullRequestDetail | null) {
21 const registry = use(RegistryContext);
22 const target = pullRequestTarget(params);
23 const scopeKey = pullRequestReviewKey(params);
24 const supported = Boolean(pr?.capabilities.viewedFiles);
25 const permission = useAtomValue(
26 composerPullRequests.setFilesViewed.permissionAtom(target.environmentId),
27 );
28 const connected = usePullRequestConnected(params.environmentId);
29 const allowed = connected && permission;
30 const query = useEnvironmentQuery(supported ? composerPullRequests.filesViewed(target) : null);
31 const refresh = useAtomRefresh(composerPullRequests.filesViewed(target));
32 const readsAtom = filesViewedReadsFamily(filesViewedReadsKey(target));
33 const reads = useAtomValue(readsAtom);
34 const write = useAtomCommand(composerPullRequests.setFilesViewed, { reportFailure: false });
35 const [state, setState] = useState<ViewedState>({ key: scopeKey, presses: {}, error: null });
36 // Kept apart from `presses`, which a failed write clears, so a file marked here stays listed.
37 const [marked, setMarked] = useState({ key: scopeKey, paths: new Set<string>() });
38 const markedPaths = marked.key === scopeKey ? marked.paths : null;
39 const presses = state.key === scopeKey ? state.presses : {};
40 const error = state.key === scopeKey ? state.error : null;
41 const overlay = viewedOverlay(presses, reads);
42 const update = (change: (current: ViewedState) => ViewedState) =>
43 setState((current) =>
44 change(current.key === scopeKey ? current : { key: scopeKey, presses: {}, error: null }),
45 );
46 const serverViewed = (path: string) =>
47 query.data?.files.some((file) => file.path === path && file.state === "viewed") ?? false;
48 const isViewed = (path: string) => overlay[path] ?? serverViewed(path);
49 const toggle = async (path: string) => {
50 if (!supported || !allowed || !query.data) return;
51 const viewed = !isViewed(path);
52 const version = (presses[path]?.version ?? 0) + 1;
53 setMarked((current) => ({
54 key: scopeKey,
55 paths: new Set(current.key === scopeKey ? current.paths : []).add(path),
56 }));
57 update((current) => ({
58 ...current,
59 error: null,
60 presses: { ...current.presses, [path]: { viewed, version, awaitingReadAfter: undefined } },
61 }));
62 const response = await write({
63 ...target,
64 input: { ...target.input, files: [{ path, viewed }] },
65 });
66 // A read completed by the time the write lands may predate it; the next one carries it.
67 const readsAtWrite = registry.get(readsAtom);
68 setState((current) => {
69 if (current.key !== scopeKey || current.presses[path]?.version !== version) return current;
70 if (response._tag === "Success")
71 return {
72 ...current,
73 presses: {
74 ...current.presses,
75 [path]: { viewed, version, awaitingReadAfter: readsAtWrite },
76 },
77 };
78 const rest = { ...current.presses };
79 delete rest[path];
80 const cause = squashAtomCommandFailure(response);
81 return {
82 ...current,
83 presses: rest,
84 error:
85 cause instanceof Error
86 ? (errorDisplayMessage(cause, m) ?? cause.message)
87 : m(msg("Could not update viewed files.")),
88 };
89 });
90 refresh();
91 };
92 return {
93 supported,
94 canToggle: supported && allowed && Boolean(query.data),
95 isViewed,
96 toggledHere: (path: string) => markedPaths?.has(path) === true,
97 toggle,
98 error: error ?? query.error,
99 truncated: query.data?.truncated,
100 };
101}