@lukstei/dsh-client-review-git 0.0.0-stage → 0.2.2-rc.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +144 -2
- package/cordis.patch.yml +4 -0
- package/dist/client.js +3378 -0
- package/dist/host.js +1059 -0
- package/lib/typert.host.d.ts +3 -0
- package/lib/typert.host.js +611 -0
- package/lib/typert.remote-client.d.ts +37 -0
- package/lib/typert.remote-client.d.ts.map +1 -0
- package/lib/typert.remote-client.js +505 -0
- package/package.json +156 -3
- package/src/client/FileInspector.tsx +162 -0
- package/src/client/GitDiffPane.tsx +252 -0
- package/src/client/GitReviewCard.tsx +202 -0
- package/src/client/GitReviewRow.tsx +191 -0
- package/src/client/ReviewToolbar.tsx +470 -0
- package/src/client/contract.ts +27 -0
- package/src/client/css.d.ts +5 -0
- package/src/client/index.tsx +230 -0
- package/src/client/review-turn.ts +193 -0
- package/src/client/reviewSelection.ts +14 -0
- package/src/client/useLoad.ts +38 -0
- package/src/client/useReviewViewState.ts +244 -0
- package/src/git-service.ts +845 -0
- package/src/index.ts +67 -0
- package/src/review-tool.ts +85 -0
- package/src/workspace-review-service.ts +400 -0
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import type { Context } from "@deepseek-ai/cordis";
|
|
2
|
+
import type {} from "@deepseek-ai/dsh-api-gateway/client";
|
|
3
|
+
import type {} from "@deepseek-ai/dsh-client-ui-chat/client";
|
|
4
|
+
import {
|
|
5
|
+
Button,
|
|
6
|
+
IconBranchOutlineRegular,
|
|
7
|
+
Tooltip,
|
|
8
|
+
} from "@deepseek-ai/dsh-client-ui-primitives";
|
|
9
|
+
import type {} from "@deepseek-ai/dsh-client-ui-renderer/client";
|
|
10
|
+
import type {} from "@deepseek-ai/dsh-client-ui-sidebar-right/client";
|
|
11
|
+
import type {
|
|
12
|
+
PropsRenderFactories,
|
|
13
|
+
PropsRuntime,
|
|
14
|
+
SnapshotSelectorHook,
|
|
15
|
+
} from "@deepseek-ai/dsh-client-ui-slots";
|
|
16
|
+
import type { SessionId } from "@deepseek-ai/dsh-session/types";
|
|
17
|
+
import type { SplitState } from "@lukstei/dsh-client-split-view/client-contract";
|
|
18
|
+
import { branchLabel, GIT_REVIEW_TOOL } from "@lukstei/util-review-ui/review";
|
|
19
|
+
import type { BranchReviewScope } from "@lukstei/util-review-ui/types";
|
|
20
|
+
import contribution from "../../lib/typert.remote-client.js";
|
|
21
|
+
import "./contract.ts";
|
|
22
|
+
import { GitDiffPane } from "./GitDiffPane.tsx";
|
|
23
|
+
import { GitReviewCard } from "./GitReviewCard.tsx";
|
|
24
|
+
import { GitReviewRow } from "./GitReviewRow.tsx";
|
|
25
|
+
import {
|
|
26
|
+
type GitReviewScopeOrTarget,
|
|
27
|
+
gitReviewDefinition,
|
|
28
|
+
isBranchReviewScope,
|
|
29
|
+
} from "./review-turn.ts";
|
|
30
|
+
|
|
31
|
+
export const inject = [
|
|
32
|
+
"sidebarRightTabs",
|
|
33
|
+
"slots",
|
|
34
|
+
"remote",
|
|
35
|
+
"uiConversation",
|
|
36
|
+
] as const;
|
|
37
|
+
|
|
38
|
+
function ReviewTabBody({ renderFactorySlot }: PropsRenderFactories) {
|
|
39
|
+
return renderFactorySlot("gitdiff.review", {});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function ReviewHeaderAction({
|
|
43
|
+
sessionId,
|
|
44
|
+
useSplit,
|
|
45
|
+
toggle,
|
|
46
|
+
}: PropsRuntime<"split.pane.header.actions"> & {
|
|
47
|
+
useSplit: SnapshotSelectorHook<SplitState>;
|
|
48
|
+
toggle: () => void;
|
|
49
|
+
}) {
|
|
50
|
+
const pressed = useSplit((state) => {
|
|
51
|
+
const overlay = state.overlays.get(sessionId);
|
|
52
|
+
return (
|
|
53
|
+
overlay !== undefined &&
|
|
54
|
+
overlay.kind === "slot" &&
|
|
55
|
+
overlay.slotName === "gitdiff.review"
|
|
56
|
+
);
|
|
57
|
+
});
|
|
58
|
+
const label = pressed ? "Close git review" : "Show git review";
|
|
59
|
+
return (
|
|
60
|
+
<Tooltip label={label} portal>
|
|
61
|
+
<Button
|
|
62
|
+
size="sm"
|
|
63
|
+
variant="ghost"
|
|
64
|
+
className={`dsh-split-icon-btn${pressed ? " is-pressed" : ""}`}
|
|
65
|
+
aria-label={label}
|
|
66
|
+
aria-pressed={pressed}
|
|
67
|
+
onClick={toggle}
|
|
68
|
+
icon={<IconBranchOutlineRegular size={14} />}
|
|
69
|
+
/>
|
|
70
|
+
</Tooltip>
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export async function apply(ctx: Context): Promise<void> {
|
|
75
|
+
const dispose = await ctx.remote.$mount(contribution);
|
|
76
|
+
ctx.effect(() => dispose);
|
|
77
|
+
|
|
78
|
+
let opener:
|
|
79
|
+
| ((sessionId: SessionId, target: GitReviewScopeOrTarget) => Promise<void>)
|
|
80
|
+
| undefined;
|
|
81
|
+
|
|
82
|
+
const reviewInjected = () => ({
|
|
83
|
+
openReview: (sessionId: SessionId, target: GitReviewScopeOrTarget) => {
|
|
84
|
+
if (opener) {
|
|
85
|
+
void opener(sessionId, target).catch((error) => {
|
|
86
|
+
console.error("[GitReview] Open review failed:", error);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
canOpen: () => opener !== undefined,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
ctx.effect(
|
|
94
|
+
() => ctx.uiConversation.events.register(gitReviewDefinition),
|
|
95
|
+
"review: git-review turn data",
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
ctx.effect(
|
|
99
|
+
() =>
|
|
100
|
+
ctx.slots.inject("tool.call.toolview", () =>
|
|
101
|
+
ctx.slots.register(
|
|
102
|
+
{
|
|
103
|
+
name: "tool.call.toolview",
|
|
104
|
+
key: GIT_REVIEW_TOOL,
|
|
105
|
+
inject: reviewInjected,
|
|
106
|
+
},
|
|
107
|
+
GitReviewRow,
|
|
108
|
+
),
|
|
109
|
+
),
|
|
110
|
+
"review: review tool view",
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
ctx.effect(
|
|
114
|
+
() =>
|
|
115
|
+
ctx.slots.inject("conversation.chat.turnTail", () =>
|
|
116
|
+
ctx.slots.register(
|
|
117
|
+
{
|
|
118
|
+
name: "conversation.chat.turnTail",
|
|
119
|
+
id: "git-review",
|
|
120
|
+
inject: reviewInjected,
|
|
121
|
+
},
|
|
122
|
+
GitReviewCard,
|
|
123
|
+
),
|
|
124
|
+
),
|
|
125
|
+
"review: review cards",
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
ctx.plugin({
|
|
129
|
+
inject: ["splitView", "remote.workspaceReview"],
|
|
130
|
+
apply(splitCtx) {
|
|
131
|
+
opener = async (sessionId, targetOrScope) => {
|
|
132
|
+
let scope: BranchReviewScope;
|
|
133
|
+
if (isBranchReviewScope(targetOrScope)) {
|
|
134
|
+
scope = targetOrScope;
|
|
135
|
+
} else {
|
|
136
|
+
const res = await splitCtx.remote.workspaceReview.resolve(
|
|
137
|
+
sessionId,
|
|
138
|
+
targetOrScope,
|
|
139
|
+
new AbortController().signal,
|
|
140
|
+
);
|
|
141
|
+
if (!res.ok) {
|
|
142
|
+
console.error("[GitReview] Resolution failed:", res.error.message);
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
scope = res.value;
|
|
146
|
+
}
|
|
147
|
+
splitCtx.splitView.openSlotOverlay(
|
|
148
|
+
sessionId,
|
|
149
|
+
"gitdiff.review",
|
|
150
|
+
`Review ${branchLabel(scope.ref)}`,
|
|
151
|
+
{ scope },
|
|
152
|
+
);
|
|
153
|
+
};
|
|
154
|
+
splitCtx.effect(() => () => {
|
|
155
|
+
opener = undefined;
|
|
156
|
+
});
|
|
157
|
+
splitCtx.effect(
|
|
158
|
+
() =>
|
|
159
|
+
splitCtx.slots.inject("split.pane.header.actions", () =>
|
|
160
|
+
splitCtx.slots.register(
|
|
161
|
+
{
|
|
162
|
+
name: "split.pane.header.actions",
|
|
163
|
+
id: "git-review",
|
|
164
|
+
order: 12,
|
|
165
|
+
inject: (sessionId) => ({
|
|
166
|
+
hooks: { split: splitCtx.splitView },
|
|
167
|
+
toggle: () => {
|
|
168
|
+
const overlay = splitCtx.splitView
|
|
169
|
+
.getSnapshot()
|
|
170
|
+
.overlays.get(sessionId);
|
|
171
|
+
if (
|
|
172
|
+
overlay !== undefined &&
|
|
173
|
+
overlay.kind === "slot" &&
|
|
174
|
+
overlay.slotName === "gitdiff.review"
|
|
175
|
+
)
|
|
176
|
+
splitCtx.splitView.closeOverlay(sessionId);
|
|
177
|
+
else
|
|
178
|
+
splitCtx.splitView.openSlotOverlay(
|
|
179
|
+
sessionId,
|
|
180
|
+
"gitdiff.review",
|
|
181
|
+
"Review",
|
|
182
|
+
{},
|
|
183
|
+
);
|
|
184
|
+
},
|
|
185
|
+
}),
|
|
186
|
+
},
|
|
187
|
+
ReviewHeaderAction,
|
|
188
|
+
),
|
|
189
|
+
),
|
|
190
|
+
"review: split pane action",
|
|
191
|
+
);
|
|
192
|
+
},
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
ctx.inject(["remote.workspaceReview"], (ctx) => {
|
|
196
|
+
ctx.effect(() =>
|
|
197
|
+
ctx.sidebarRightTabs.register({
|
|
198
|
+
id: "dsh-client-git-diff",
|
|
199
|
+
kind: "gitbutler",
|
|
200
|
+
title: () => "Workspace Review",
|
|
201
|
+
guide: [
|
|
202
|
+
{
|
|
203
|
+
id: "open",
|
|
204
|
+
order: 50,
|
|
205
|
+
title: () => "Review",
|
|
206
|
+
description: () => "Browse Git changes",
|
|
207
|
+
},
|
|
208
|
+
],
|
|
209
|
+
}),
|
|
210
|
+
);
|
|
211
|
+
ctx.effect(() =>
|
|
212
|
+
ctx.slots.registerFactory(
|
|
213
|
+
{
|
|
214
|
+
name: "gitdiff.review",
|
|
215
|
+
scope: "session",
|
|
216
|
+
inject: () => ({ api: ctx.remote.workspaceReview }),
|
|
217
|
+
},
|
|
218
|
+
GitDiffPane,
|
|
219
|
+
),
|
|
220
|
+
);
|
|
221
|
+
ctx.effect(() =>
|
|
222
|
+
ctx.slots.inject("sidebar.right.pane.tab", () =>
|
|
223
|
+
ctx.slots.register(
|
|
224
|
+
{ name: "sidebar.right.pane.tab", key: "dsh-client-git-diff" },
|
|
225
|
+
ReviewTabBody,
|
|
226
|
+
),
|
|
227
|
+
),
|
|
228
|
+
);
|
|
229
|
+
});
|
|
230
|
+
}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import type { ConversationNodeDefinition } from "@deepseek-ai/dsh-client-ui-conversation/client";
|
|
2
|
+
import type {} from "@deepseek-ai/dsh-session/types";
|
|
3
|
+
import {
|
|
4
|
+
branchReviewScopeSchema,
|
|
5
|
+
GIT_REVIEW_TOOL,
|
|
6
|
+
reviewTargetSchema,
|
|
7
|
+
} from "@lukstei/util-review-ui/review";
|
|
8
|
+
import type {
|
|
9
|
+
BranchReviewScope,
|
|
10
|
+
ReviewTarget,
|
|
11
|
+
} from "@lukstei/util-review-ui/types";
|
|
12
|
+
import { z } from "zod";
|
|
13
|
+
import type {} from "../review-tool.ts";
|
|
14
|
+
|
|
15
|
+
export const reviewScopeOrTargetSchema = z.union([
|
|
16
|
+
branchReviewScopeSchema,
|
|
17
|
+
reviewTargetSchema,
|
|
18
|
+
]);
|
|
19
|
+
|
|
20
|
+
export type GitReviewScopeOrTarget = BranchReviewScope | ReviewTarget;
|
|
21
|
+
|
|
22
|
+
export function isBranchReviewScope(
|
|
23
|
+
scope: GitReviewScopeOrTarget,
|
|
24
|
+
): scope is BranchReviewScope {
|
|
25
|
+
return "kind" in scope && scope.kind === "branch";
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** One successfully presented review target. */
|
|
29
|
+
export interface GitReviewEntry {
|
|
30
|
+
readonly id: string;
|
|
31
|
+
readonly scope: GitReviewScopeOrTarget;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Turn-scoped review cards published by the presentation tool. */
|
|
35
|
+
export interface GitReviewTurnData {
|
|
36
|
+
readonly reviews: readonly GitReviewEntry[];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
declare module "@deepseek-ai/dsh-client-ui-conversation/client" {
|
|
40
|
+
interface ConversationTurnDataMap {
|
|
41
|
+
/** Successful `git_review_present` calls of this Turn, in call order. */
|
|
42
|
+
"git-review": GitReviewTurnData;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
interface State {
|
|
47
|
+
readonly turn: number;
|
|
48
|
+
readonly calls: ReadonlyMap<string, ReviewTarget | null>;
|
|
49
|
+
readonly reviews: readonly GitReviewEntry[];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function parseTargetArgs(argsRaw: string): ReviewTarget | null {
|
|
53
|
+
try {
|
|
54
|
+
const parsed = JSON.parse(argsRaw);
|
|
55
|
+
const target = reviewTargetSchema.safeParse(parsed);
|
|
56
|
+
return target.success ? target.data : null;
|
|
57
|
+
} catch {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Turn-local accumulator for presentation calls; it publishes no view Node.
|
|
64
|
+
* The tool persists its target or scope on the successful `tool/result`, so a
|
|
65
|
+
* resumed or replayed session rebuilds the same cards, deferring review scope
|
|
66
|
+
* resolution to click time.
|
|
67
|
+
*/
|
|
68
|
+
export const gitReviewDefinition: ConversationNodeDefinition<State> = {
|
|
69
|
+
kind: "git-review",
|
|
70
|
+
match: (event) => {
|
|
71
|
+
if (event.type === "turn/start")
|
|
72
|
+
return { id: String(event.data.turn), role: "start" };
|
|
73
|
+
if (event.type === "command/run" && event.data.name === "git-review") {
|
|
74
|
+
try {
|
|
75
|
+
const data = JSON.parse(event.data.args ?? "{}");
|
|
76
|
+
return typeof data === "object" &&
|
|
77
|
+
data !== null &&
|
|
78
|
+
"turn" in data &&
|
|
79
|
+
typeof data.turn === "number"
|
|
80
|
+
? { id: String(data.turn), role: "update" }
|
|
81
|
+
: null;
|
|
82
|
+
} catch {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if ((event as { type: string }).type === "git-review/presented") {
|
|
87
|
+
const data = event.data as Record<string, unknown> | null;
|
|
88
|
+
return typeof data === "object" &&
|
|
89
|
+
data !== null &&
|
|
90
|
+
"turn" in data &&
|
|
91
|
+
typeof data.turn === "number"
|
|
92
|
+
? { id: String(data.turn), role: "update" }
|
|
93
|
+
: null;
|
|
94
|
+
}
|
|
95
|
+
if (event.type === "tool/call")
|
|
96
|
+
return event.data.name === GIT_REVIEW_TOOL
|
|
97
|
+
? { id: String(event.data.turn), role: "update" }
|
|
98
|
+
: null;
|
|
99
|
+
if (event.type === "tool/result")
|
|
100
|
+
return { id: String(event.data.turn), role: "update" };
|
|
101
|
+
return null;
|
|
102
|
+
},
|
|
103
|
+
start: (_context, match) => {
|
|
104
|
+
if (match.event.type !== "turn/start")
|
|
105
|
+
throw new Error("git-review start requires turn/start");
|
|
106
|
+
return { turn: match.event.data.turn, calls: new Map(), reviews: [] };
|
|
107
|
+
},
|
|
108
|
+
update: (context, match) => {
|
|
109
|
+
const event = match.event;
|
|
110
|
+
if (event.type === "command/run" && event.data.name === "git-review") {
|
|
111
|
+
try {
|
|
112
|
+
const data = JSON.parse(event.data.args ?? "{}") as {
|
|
113
|
+
callId?: string;
|
|
114
|
+
target?: unknown;
|
|
115
|
+
};
|
|
116
|
+
const parsed = reviewScopeOrTargetSchema.safeParse(data.target);
|
|
117
|
+
if (parsed.success) {
|
|
118
|
+
const id = data.callId ? String(data.callId) : `review-${event.seq}`;
|
|
119
|
+
if (context.state.reviews.some((r) => r.id === id)) {
|
|
120
|
+
return context.state;
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
...context.state,
|
|
124
|
+
reviews: [...context.state.reviews, { id, scope: parsed.data }],
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
} catch {}
|
|
128
|
+
return context.state;
|
|
129
|
+
}
|
|
130
|
+
if ((event as { type: string }).type === "git-review/presented") {
|
|
131
|
+
const data = event.data as {
|
|
132
|
+
callId?: string;
|
|
133
|
+
target?: unknown;
|
|
134
|
+
};
|
|
135
|
+
const parsed = reviewScopeOrTargetSchema.safeParse(data.target);
|
|
136
|
+
if (parsed.success) {
|
|
137
|
+
const id = data.callId ? String(data.callId) : `review-${event.seq}`;
|
|
138
|
+
if (context.state.reviews.some((r) => r.id === id)) {
|
|
139
|
+
return context.state;
|
|
140
|
+
}
|
|
141
|
+
return {
|
|
142
|
+
...context.state,
|
|
143
|
+
reviews: [...context.state.reviews, { id, scope: parsed.data }],
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
return context.state;
|
|
147
|
+
}
|
|
148
|
+
if (event.type === "tool/call") {
|
|
149
|
+
const calls = new Map(context.state.calls);
|
|
150
|
+
calls.set(
|
|
151
|
+
String(event.data.callId),
|
|
152
|
+
parseTargetArgs(event.data.arguments),
|
|
153
|
+
);
|
|
154
|
+
return { ...context.state, calls };
|
|
155
|
+
}
|
|
156
|
+
if (event.type !== "tool/result") return context.state;
|
|
157
|
+
const callId = String(event.data.message.source.callId);
|
|
158
|
+
if (!context.state.calls.has(callId)) return context.state;
|
|
159
|
+
const calls = new Map(context.state.calls);
|
|
160
|
+
const argumentTarget = calls.get(callId);
|
|
161
|
+
calls.delete(callId);
|
|
162
|
+
if (event.data.message.isError) return { ...context.state, calls };
|
|
163
|
+
if (context.state.reviews.some((r) => r.id === callId)) {
|
|
164
|
+
return { ...context.state, calls };
|
|
165
|
+
}
|
|
166
|
+
const parsed = reviewScopeOrTargetSchema.safeParse(event.data.meta);
|
|
167
|
+
const scope = parsed.success ? parsed.data : argumentTarget;
|
|
168
|
+
if (scope === null || scope === undefined)
|
|
169
|
+
return { ...context.state, calls };
|
|
170
|
+
return {
|
|
171
|
+
...context.state,
|
|
172
|
+
calls,
|
|
173
|
+
reviews: [...context.state.reviews, { id: callId, scope }],
|
|
174
|
+
};
|
|
175
|
+
},
|
|
176
|
+
buildLocationData: (context, scope, previous) => {
|
|
177
|
+
const state = context.state;
|
|
178
|
+
if (scope !== "turn" || state === undefined) return null;
|
|
179
|
+
if (
|
|
180
|
+
previous?.kind === "turn" &&
|
|
181
|
+
previous.turn === state.turn &&
|
|
182
|
+
previous.key === "git-review" &&
|
|
183
|
+
previous.value.reviews === state.reviews
|
|
184
|
+
)
|
|
185
|
+
return previous;
|
|
186
|
+
return {
|
|
187
|
+
kind: "turn",
|
|
188
|
+
turn: state.turn,
|
|
189
|
+
key: "git-review",
|
|
190
|
+
value: { reviews: state.reviews },
|
|
191
|
+
};
|
|
192
|
+
},
|
|
193
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ReviewCatalog, ReviewScope } from "@lukstei/util-review-ui/types";
|
|
2
|
+
|
|
3
|
+
export function resolveReviewBaseline(
|
|
4
|
+
scope: ReviewScope | null,
|
|
5
|
+
catalog: ReviewCatalog | null,
|
|
6
|
+
): string {
|
|
7
|
+
if (scope === null || scope.kind !== "branch") return "";
|
|
8
|
+
if (scope.base.kind === "manual") return scope.base.ref;
|
|
9
|
+
if (catalog === null || catalog.kind !== "git") return "";
|
|
10
|
+
const repo = catalog.repositories.find((r) => r.cwd === scope.repository);
|
|
11
|
+
if (repo === undefined) return "";
|
|
12
|
+
const branch = repo.branches.find((branch) => branch.ref === scope.ref);
|
|
13
|
+
return branch === undefined || branch.base === null ? "" : branch.base;
|
|
14
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { RemoteResult } from "@deepseek-ai/dsh-typert-protocol";
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
|
|
4
|
+
export function useLoad<T>(
|
|
5
|
+
load: (signal: AbortSignal) => Promise<RemoteResult<T>>,
|
|
6
|
+
):
|
|
7
|
+
| { status: "loading" }
|
|
8
|
+
| { status: "failed"; message: string }
|
|
9
|
+
| { status: "ready"; value: T } {
|
|
10
|
+
const [result, setResult] = useState<
|
|
11
|
+
| { status: "loading" }
|
|
12
|
+
| { status: "failed"; message: string }
|
|
13
|
+
| { status: "ready"; value: T }
|
|
14
|
+
>({ status: "loading" });
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
const controller = new AbortController();
|
|
17
|
+
setResult({ status: "loading" });
|
|
18
|
+
load(controller.signal).then(
|
|
19
|
+
(result) => {
|
|
20
|
+
if (!controller.signal.aborted)
|
|
21
|
+
setResult(
|
|
22
|
+
result.ok
|
|
23
|
+
? { status: "ready", value: result.value }
|
|
24
|
+
: { status: "failed", message: result.error.message },
|
|
25
|
+
);
|
|
26
|
+
},
|
|
27
|
+
(error: unknown) => {
|
|
28
|
+
if (!controller.signal.aborted)
|
|
29
|
+
setResult({
|
|
30
|
+
status: "failed",
|
|
31
|
+
message: error instanceof Error ? error.message : String(error),
|
|
32
|
+
});
|
|
33
|
+
},
|
|
34
|
+
);
|
|
35
|
+
return () => controller.abort();
|
|
36
|
+
}, [load]);
|
|
37
|
+
return result;
|
|
38
|
+
}
|