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

reviewGuideState.ts

74 lines2.2 KB

1import type { PullRequestReviewGuideResult } from "@t3tools/contracts";
2​
3export function reviewGuideState(data: PullRequestReviewGuideResult | null) {
4 const guide = data?.guide ?? null;
5 return {
6 guide,
7 running: data?.run?._tag === "Running",
8 stale: guide !== null && data?.currentHeadSha != null && guide.headSha !== data.currentHeadSha,
9 failure:
10 data?.run?._tag === "Failed" && data.run.headSha === data.currentHeadSha
11 ? data.run.detail
12 : null,
13 };
15​
16export function guideNoticeState(state: {
17 readonly data: PullRequestReviewGuideResult | null;
18 readonly isPending: boolean;
19 readonly running: boolean;
20 readonly startError: string | null;
21 readonly error: string | null;
22}) {
23 const { guide, stale, failure } = reviewGuideState(state.data);
24 if (state.running) {
25 return {
26 kind: "writing" as const,
27 title: "Rewriting guide",
28 description: guide
29 ? "You can keep reading this guide while the agent writes the update."
30 : "An agent is reading the change and writing the tour. You can leave and come back when it’s ready.",
31 action: guide ? "Rewriting…" : "Writing guide…",
32 };
33 }
34 const error = state.startError ?? failure;
35 if (error) {
36 return {
37 kind: "failed" as const,
38 title: "Could not write guide",
39 description: error,
40 action: "Try again",
41 };
42 }
43 if (state.error) {
44 return {
45 kind: "unavailable" as const,
46 title: "Could not refresh guide",
47 description: state.error,
48 action: "Try again",
49 };
50 }
51 if (!state.data) {
52 return {
53 kind: "loading" as const,
54 title: "Review guide",
55 description: "Checking for a saved guide…",
56 action: "Loading guide…",
57 };
58 }
59 if (stale) {
60 return {
61 kind: "stale" as const,
62 title: "New changes since this guide",
63 description: "You can read the previous guide, or rewrite it for the latest changes.",
64 action: "Rewrite guide",
65 };
66 }
67 return {
68 kind: guide ? ("ready" as const) : ("empty" as const),
69 title: "Review guide",
70 description:
71 "Get a tour of the change, with code excerpts and notes on what needs a closer look.",
72 action: "Write guide",
73 };