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

usePullRequestFilesViewed.ts

85 lines3.8 KB

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