The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { errorDisplayMessage } from "@t3tools/client-runtime/errors"; |
| 2 | import { usePullRequestConnected } from "./usePullRequestConnected"; |
| 3 | import { RegistryContext, useAtomRefresh, useAtomValue } from "@effect/atom-react"; |
| 4 | import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; |
| 5 | import type { PullRequestDetail } from "@t3tools/contracts"; |
| 6 | import { msg } from "gt-react-native"; |
| 7 | import { use, useState } from "react"; |
| 8 | import { |
| 9 | composerPullRequests, |
| 10 | filesViewedReadsFamily, |
| 11 | filesViewedReadsKey, |
| 12 | } from "../../state/pull-requests"; |
| 13 | import { useEnvironmentQuery } from "../../state/query"; |
| 14 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 15 | import { pullRequestTarget, type PullRequestParams } from "./usePullRequestDiff"; |
| 16 | import { pullRequestReviewKey } from "./pullRequestReviewDrafts"; |
| 17 | import { viewedOverlay, type ViewedState } from "./pullRequestFilesViewed"; |
| 18 | import { m } from "../../i18n/messages"; |
| 19 | |
| 20 | export 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 | const presses = state.key === scopeKey ? state.presses : {}; |
| 37 | const error = state.key === scopeKey ? state.error : null; |
| 38 | const overlay = viewedOverlay(presses, reads); |
| 39 | const update = (change: (current: ViewedState) => ViewedState) => |
| 40 | setState((current) => |
| 41 | change(current.key === scopeKey ? current : { key: scopeKey, presses: {}, error: null }), |
| 42 | ); |
| 43 | const serverViewed = (path: string) => |
| 44 | query.data?.files.some((file) => file.path === path && file.state === "viewed") ?? false; |
| 45 | const isViewed = (path: string) => overlay[path] ?? serverViewed(path); |
| 46 | const toggle = async (path: string) => { |
| 47 | if (!supported || !allowed || !query.data) return; |
| 48 | const viewed = !isViewed(path); |
| 49 | const version = (presses[path]?.version ?? 0) + 1; |
| 50 | update((current) => ({ |
| 51 | ...current, |
| 52 | error: null, |
| 53 | presses: { ...current.presses, [path]: { viewed, version, awaitingReadAfter: undefined } }, |
| 54 | })); |
| 55 | const response = await write({ |
| 56 | ...target, |
| 57 | input: { ...target.input, files: [{ path, viewed }] }, |
| 58 | }); |
| 59 | // A read completed by the time the write lands may predate it; the next one carries it. |
| 60 | const readsAtWrite = registry.get(readsAtom); |
| 61 | setState((current) => { |
| 62 | if (current.key !== scopeKey || current.presses[path]?.version !== version) return current; |
| 63 | if (response._tag === "Success") |
| 64 | return { |
| 65 | ...current, |
| 66 | presses: { |
| 67 | ...current.presses, |
| 68 | [path]: { viewed, version, awaitingReadAfter: readsAtWrite }, |
| 69 | }, |
| 70 | }; |
| 71 | const rest = { ...current.presses }; |
| 72 | delete rest[path]; |
| 73 | const cause = squashAtomCommandFailure(response); |
| 74 | return { |
| 75 | ...current, |
| 76 | presses: rest, |
| 77 | error: |
| 78 | cause instanceof Error |
| 79 | ? (errorDisplayMessage(cause, m) ?? cause.message) |
| 80 | : m(msg("Could not update viewed files.")), |
| 81 | }; |
| 82 | }); |
| 83 | refresh(); |
| 84 | }; |
| 85 | return { |
| 86 | supported, |
| 87 | canToggle: supported && allowed && Boolean(query.data), |
| 88 | isViewed, |
| 89 | toggle, |
| 90 | error: error ?? query.error, |
| 91 | truncated: query.data?.truncated, |
| 92 | }; |
| 93 | } |