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