@lukoweb/apitogo 0.1.1 → 0.1.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/dist/cli/cli.js +168 -57
- package/dist/declarations/lib/ui/Command.d.ts +1 -1
- package/dist/declarations/lib/util/MdxComponents.d.ts +1 -1
- package/package.json +2 -1
- package/src/app/main.tsx +150 -150
- package/src/config/loader.ts +245 -245
- package/src/index.ts +33 -33
- package/src/lib/authentication/components/CallbackHandler.tsx +45 -45
- package/src/lib/authentication/components/SignIn.tsx +46 -46
- package/src/lib/authentication/components/SignUp.tsx +48 -48
- package/src/lib/authentication/providers/clerk.tsx +224 -224
- package/src/lib/authentication/ui/AuthCard.tsx +7 -7
- package/src/lib/authentication/ui/EmailLinkCallbackUi.tsx +129 -125
- package/src/lib/authentication/ui/EmailLinkSentUi.tsx +105 -101
- package/src/lib/authentication/ui/EmailLinkSignInUi.tsx +100 -96
- package/src/lib/authentication/ui/EmailVerificationUi.tsx +128 -124
- package/src/lib/authentication/ui/ZudokuAuthUi.tsx +620 -616
- package/src/lib/components/AiAssistantMenuItems.tsx +102 -102
- package/src/lib/components/Autocomplete.tsx +123 -123
- package/src/lib/components/DeveloperHint.tsx +39 -35
- package/src/lib/components/Header.tsx +260 -260
- package/src/lib/components/Layout.tsx +53 -53
- package/src/lib/components/Main.tsx +51 -51
- package/src/lib/components/Mermaid.tsx +74 -70
- package/src/lib/components/MobileTopNavigation.tsx +276 -276
- package/src/lib/components/MultiSelect.tsx +86 -82
- package/src/lib/components/TopNavigation.tsx +103 -103
- package/src/lib/components/index.ts +26 -26
- package/src/lib/components/navigation/NavigationCategory.tsx +157 -157
- package/src/lib/components/navigation/NavigationFilterInput.tsx +35 -35
- package/src/lib/components/navigation/NavigationItem.tsx +181 -177
- package/src/lib/core/RouteGuard.tsx +193 -193
- package/src/lib/errors/ErrorMessage.tsx +50 -46
- package/src/lib/plugins/api-catalog/Catalog.tsx +80 -80
- package/src/lib/plugins/api-keys/CreateApiKey.tsx +128 -124
- package/src/lib/plugins/api-keys/CreateApiKeyDialog.tsx +49 -49
- package/src/lib/plugins/api-keys/SettingsApiKeys.tsx +72 -72
- package/src/lib/plugins/api-keys/settings/ApiKeyItem.tsx +342 -342
- package/src/lib/plugins/api-keys/settings/RevealApiKey.tsx +127 -127
- package/src/lib/plugins/markdown/MdxPage.tsx +285 -285
- package/src/lib/plugins/openapi/ApiHeader.tsx +96 -96
- package/src/lib/plugins/openapi/CollapsibleCode.tsx +88 -88
- package/src/lib/plugins/openapi/DownloadSchemaButton.tsx +94 -94
- package/src/lib/plugins/openapi/GeneratedExampleSidecarBox.tsx +52 -52
- package/src/lib/plugins/openapi/OperationListItem.tsx +183 -183
- package/src/lib/plugins/openapi/RequestBodySidecarBox.tsx +63 -63
- package/src/lib/plugins/openapi/ResponsesSidecarBox.tsx +120 -117
- package/src/lib/plugins/openapi/SchemaInfo.tsx +344 -344
- package/src/lib/plugins/openapi/SchemaList.tsx +113 -113
- package/src/lib/plugins/openapi/Sidecar.tsx +312 -309
- package/src/lib/plugins/openapi/SidecarExamples.tsx +177 -174
- package/src/lib/plugins/openapi/components/EnumValues.tsx +58 -58
- package/src/lib/plugins/openapi/components/ResponseContent.tsx +117 -114
- package/src/lib/plugins/openapi/playground/BodyPanel.tsx +274 -271
- package/src/lib/plugins/openapi/playground/CollapsibleHeader.tsx +51 -51
- package/src/lib/plugins/openapi/playground/ExamplesDropdown.tsx +60 -60
- package/src/lib/plugins/openapi/playground/Headers.tsx +188 -181
- package/src/lib/plugins/openapi/playground/IdentityDialog.tsx +75 -75
- package/src/lib/plugins/openapi/playground/IdentitySelector.tsx +38 -38
- package/src/lib/plugins/openapi/playground/ParamsGrid.tsx +45 -45
- package/src/lib/plugins/openapi/playground/Playground.tsx +602 -599
- package/src/lib/plugins/openapi/playground/PlaygroundDialog.tsx +56 -56
- package/src/lib/plugins/openapi/playground/QueryParams.tsx +151 -148
- package/src/lib/plugins/openapi/playground/RequestLoginDialog.tsx +70 -70
- package/src/lib/plugins/openapi/playground/request-panel/MultipartField.tsx +91 -91
- package/src/lib/plugins/openapi/playground/result-panel/AudioPlayer.tsx +50 -50
- package/src/lib/plugins/openapi/playground/result-panel/ResponseTab.tsx +355 -355
- package/src/lib/plugins/openapi/playground/result-panel/ResultPanel.tsx +106 -102
- package/src/lib/plugins/openapi/schema/SchemaPropertyItem.tsx +189 -184
- package/src/lib/plugins/openapi/schema/SchemaView.tsx +299 -299
- package/src/lib/plugins/search-pagefind/IndexingDialog.tsx +162 -162
- package/src/lib/plugins/search-pagefind/PagefindSearch.tsx +202 -202
- package/src/lib/plugins/search-pagefind/ResultList.tsx +122 -118
- package/src/lib/ui/ActionButton.tsx +21 -21
- package/src/lib/ui/Command.tsx +191 -191
- package/src/lib/util/MdxComponents.tsx +150 -150
- package/src/vite/config.ts +234 -231
- package/src/vite/dev-server.ts +291 -291
- package/src/vite/plugin-api-keys.ts +50 -50
- package/src/vite/plugin-api.ts +320 -318
- package/src/vite/plugin-auth.ts +38 -38
- package/src/vite/plugin-component.ts +45 -39
- package/src/vite/plugin-config.ts +75 -75
- package/src/vite/plugin-custom-pages.ts +36 -36
- package/src/vite/plugin-docs.ts +202 -202
- package/src/vite/plugin-markdown-export.ts +214 -214
- package/src/vite/plugin-mdx.ts +149 -149
- package/src/vite/plugin-navigation.ts +106 -106
- package/src/vite/plugin-search.ts +47 -47
- package/src/vite/plugin.ts +42 -42
- package/src/vite/prerender/prerender.ts +233 -233
|
@@ -1,342 +1,342 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
import {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
} from "
|
|
30
|
-
import {
|
|
31
|
-
import { useZudoku } from "../../../components/context/ZudokuContext.js";
|
|
32
|
-
import type { ZudokuContext } from "../../../core/ZudokuContext.js";
|
|
33
|
-
import { cn } from "../../../util/cn.js";
|
|
34
|
-
import type { ApiConsumer } from "../index.js";
|
|
35
|
-
import { RevealApiKey } from "./RevealApiKey.js";
|
|
36
|
-
|
|
37
|
-
const ApiKeyItem = ({
|
|
38
|
-
consumer,
|
|
39
|
-
onUpdate,
|
|
40
|
-
onRollKey,
|
|
41
|
-
onDeleteKey,
|
|
42
|
-
}: {
|
|
43
|
-
consumer: ApiConsumer;
|
|
44
|
-
onUpdate?: (
|
|
45
|
-
data: { label: string; id: string },
|
|
46
|
-
context: ZudokuContext,
|
|
47
|
-
) => Promise<void>;
|
|
48
|
-
onRollKey?: (consumerId: string, context: ZudokuContext) => Promise<void>;
|
|
49
|
-
onDeleteKey?: (
|
|
50
|
-
consumerId: string,
|
|
51
|
-
keyId: string,
|
|
52
|
-
context: ZudokuContext,
|
|
53
|
-
) => Promise<void>;
|
|
54
|
-
}) => {
|
|
55
|
-
const [isEditing, setIsEditing] = useState(false);
|
|
56
|
-
const [editingLabel, setEditingLabel] = useState(consumer.label);
|
|
57
|
-
const queryClient = useQueryClient();
|
|
58
|
-
const context = useZudoku();
|
|
59
|
-
|
|
60
|
-
const rollKeyMutation = useMutation({
|
|
61
|
-
mutationFn: async (id: string) => {
|
|
62
|
-
if (!onRollKey) {
|
|
63
|
-
throw new Error("rollKey not implemented");
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
return await onRollKey?.(id, context);
|
|
67
|
-
},
|
|
68
|
-
onSuccess: () =>
|
|
69
|
-
void queryClient.invalidateQueries({ queryKey: ["api-keys"] }),
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
const deleteKeyMutation = useMutation({
|
|
73
|
-
mutationFn: ({
|
|
74
|
-
consumerId,
|
|
75
|
-
keyId,
|
|
76
|
-
}: {
|
|
77
|
-
consumerId: string;
|
|
78
|
-
keyId: string;
|
|
79
|
-
}) => {
|
|
80
|
-
if (!onDeleteKey) {
|
|
81
|
-
throw new Error("deleteKey not implemented");
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
return onDeleteKey(consumerId, keyId, context);
|
|
85
|
-
},
|
|
86
|
-
onMutate: async ({ consumerId, keyId }) => {
|
|
87
|
-
await queryClient.cancelQueries({ queryKey: ["api-keys"] });
|
|
88
|
-
const previousData = queryClient.getQueryData<ApiConsumer[]>([
|
|
89
|
-
"api-keys",
|
|
90
|
-
]);
|
|
91
|
-
queryClient.setQueryData<ApiConsumer[]>(["api-keys"], (old) => {
|
|
92
|
-
if (!old) {
|
|
93
|
-
return old;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
return old.map((consumer) => {
|
|
97
|
-
if (consumer.id === consumerId) {
|
|
98
|
-
return {
|
|
99
|
-
...consumer,
|
|
100
|
-
apiKeys: consumer.apiKeys.filter((key) => key.id !== keyId),
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
return consumer;
|
|
104
|
-
});
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
return { previousData };
|
|
108
|
-
},
|
|
109
|
-
onError: (_err, _variables, context) => {
|
|
110
|
-
if (context?.previousData) {
|
|
111
|
-
queryClient.setQueryData(["api-keys"], context.previousData);
|
|
112
|
-
}
|
|
113
|
-
},
|
|
114
|
-
onSuccess: () => {
|
|
115
|
-
void queryClient.invalidateQueries({ queryKey: ["api-keys"] });
|
|
116
|
-
},
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
const updateConsumerMutation = useMutation({
|
|
120
|
-
mutationFn: ({
|
|
121
|
-
consumerId,
|
|
122
|
-
label,
|
|
123
|
-
}: {
|
|
124
|
-
consumerId: string;
|
|
125
|
-
label: string;
|
|
126
|
-
}) => {
|
|
127
|
-
if (!onUpdate) {
|
|
128
|
-
throw new Error("updateConsumer not implemented");
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
return onUpdate({ id: consumerId, label }, context);
|
|
132
|
-
},
|
|
133
|
-
onMutate: async ({ consumerId, label }) => {
|
|
134
|
-
await queryClient.cancelQueries({ queryKey: ["api-keys"] });
|
|
135
|
-
|
|
136
|
-
const previousData = queryClient.getQueryData(["api-keys"]);
|
|
137
|
-
queryClient.setQueryData<ApiConsumer[]>(["api-keys"], (old) => {
|
|
138
|
-
if (!old) {
|
|
139
|
-
return old;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
return old.map((consumer) => {
|
|
143
|
-
if (consumer.id === consumerId) {
|
|
144
|
-
return {
|
|
145
|
-
...consumer,
|
|
146
|
-
label,
|
|
147
|
-
};
|
|
148
|
-
}
|
|
149
|
-
return consumer;
|
|
150
|
-
});
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
return { previousData };
|
|
154
|
-
},
|
|
155
|
-
onError: (_err, _variables, context) => {
|
|
156
|
-
if (context?.previousData) {
|
|
157
|
-
queryClient.setQueryData(["api-keys"], context.previousData);
|
|
158
|
-
}
|
|
159
|
-
},
|
|
160
|
-
onSuccess: () => {
|
|
161
|
-
void queryClient.invalidateQueries({ queryKey: ["api-keys"] });
|
|
162
|
-
},
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
const handleStartEdit = () => {
|
|
166
|
-
setIsEditing(true);
|
|
167
|
-
setEditingLabel(consumer.label);
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
const handleSaveEdit = () => {
|
|
171
|
-
if (editingLabel.trim()) {
|
|
172
|
-
updateConsumerMutation.mutate({
|
|
173
|
-
label: editingLabel.trim(),
|
|
174
|
-
consumerId: consumer.id,
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
setIsEditing(false);
|
|
178
|
-
};
|
|
179
|
-
|
|
180
|
-
return (
|
|
181
|
-
<>
|
|
182
|
-
{rollKeyMutation.isError && (
|
|
183
|
-
<Alert variant="destructive" className="mb-4">
|
|
184
|
-
<CircleSlashIcon size={16} />
|
|
185
|
-
<AlertTitle>{rollKeyMutation.error.message}</AlertTitle>
|
|
186
|
-
</Alert>
|
|
187
|
-
)}
|
|
188
|
-
{updateConsumerMutation.isError && (
|
|
189
|
-
<Alert variant="destructive" className="mb-4">
|
|
190
|
-
<CircleSlashIcon size={16} />
|
|
191
|
-
<AlertTitle>{updateConsumerMutation.error.message}</AlertTitle>
|
|
192
|
-
</Alert>
|
|
193
|
-
)}
|
|
194
|
-
{deleteKeyMutation.isError && (
|
|
195
|
-
<Alert variant="destructive" className="mb-4">
|
|
196
|
-
<CircleSlashIcon size={16} />
|
|
197
|
-
<AlertTitle>{deleteKeyMutation.error.message}</AlertTitle>
|
|
198
|
-
</Alert>
|
|
199
|
-
)}
|
|
200
|
-
<Frame
|
|
201
|
-
className="grid grid-cols-subgrid col-span-full items-center mb-4 group"
|
|
202
|
-
key={consumer.id}
|
|
203
|
-
>
|
|
204
|
-
<FrameHeader className="col-span-full flex-row items-start justify-between gap-4">
|
|
205
|
-
<div className="flex flex-col gap-1">
|
|
206
|
-
{isEditing ? (
|
|
207
|
-
<div className="flex items-center gap-2">
|
|
208
|
-
<Input
|
|
209
|
-
maxLength={32}
|
|
210
|
-
value={editingLabel}
|
|
211
|
-
onChange={(e) => setEditingLabel(e.target.value)}
|
|
212
|
-
onKeyDown={(e) => {
|
|
213
|
-
if (e.key === "Enter") {
|
|
214
|
-
handleSaveEdit();
|
|
215
|
-
} else if (e.key === "Escape") {
|
|
216
|
-
setIsEditing(false);
|
|
217
|
-
}
|
|
218
|
-
}}
|
|
219
|
-
autoFocus
|
|
220
|
-
/>
|
|
221
|
-
<div className="flex items-center">
|
|
222
|
-
<Button
|
|
223
|
-
size="icon"
|
|
224
|
-
variant="ghost"
|
|
225
|
-
onClick={handleSaveEdit}
|
|
226
|
-
disabled={!editingLabel.trim()}
|
|
227
|
-
>
|
|
228
|
-
<CheckIcon size={16} />
|
|
229
|
-
</Button>
|
|
230
|
-
<Button
|
|
231
|
-
size="icon"
|
|
232
|
-
variant="ghost"
|
|
233
|
-
onClick={() => setIsEditing(false)}
|
|
234
|
-
>
|
|
235
|
-
<XIcon size={16} />
|
|
236
|
-
</Button>
|
|
237
|
-
</div>
|
|
238
|
-
</div>
|
|
239
|
-
) : (
|
|
240
|
-
<FrameTitle>{consumer.label}</FrameTitle>
|
|
241
|
-
)}
|
|
242
|
-
<FrameDescription>
|
|
243
|
-
{consumer.createdOn && (
|
|
244
|
-
<div>
|
|
245
|
-
Created on {new Date(consumer.createdOn).toLocaleDateString()}
|
|
246
|
-
</div>
|
|
247
|
-
)}
|
|
248
|
-
{consumer.expiresOn && (
|
|
249
|
-
<div>
|
|
250
|
-
Expires on {new Date(consumer.expiresOn).toLocaleDateString()}
|
|
251
|
-
</div>
|
|
252
|
-
)}
|
|
253
|
-
</FrameDescription>
|
|
254
|
-
</div>
|
|
255
|
-
|
|
256
|
-
<div className="flex gap-1">
|
|
257
|
-
{onUpdate && (
|
|
258
|
-
<Button
|
|
259
|
-
variant="ghost"
|
|
260
|
-
onClick={handleStartEdit}
|
|
261
|
-
className={cn(
|
|
262
|
-
"flex gap-2",
|
|
263
|
-
isEditing && "opacity-0! pointer-events-none",
|
|
264
|
-
)}
|
|
265
|
-
disabled={isEditing}
|
|
266
|
-
>
|
|
267
|
-
<PencilLineIcon size={16} />
|
|
268
|
-
<span className="hidden md:block">Edit label</span>
|
|
269
|
-
</Button>
|
|
270
|
-
)}
|
|
271
|
-
{onRollKey && (
|
|
272
|
-
<Dialog>
|
|
273
|
-
<DialogTrigger asChild>
|
|
274
|
-
<Button
|
|
275
|
-
title="Roll this key"
|
|
276
|
-
variant="ghost"
|
|
277
|
-
disabled={rollKeyMutation.isPending}
|
|
278
|
-
className="flex items-center gap-2"
|
|
279
|
-
>
|
|
280
|
-
<RefreshCwIcon
|
|
281
|
-
size={16}
|
|
282
|
-
className={
|
|
283
|
-
rollKeyMutation.isPending ? "animate-spin" : undefined
|
|
284
|
-
}
|
|
285
|
-
/>
|
|
286
|
-
<span className="hidden md:block">Roll key</span>
|
|
287
|
-
</Button>
|
|
288
|
-
</DialogTrigger>
|
|
289
|
-
<DialogContent>
|
|
290
|
-
<DialogHeader>
|
|
291
|
-
<DialogTitle>Roll API Key</DialogTitle>
|
|
292
|
-
<DialogDescription>
|
|
293
|
-
Are you sure you want to roll this API key?
|
|
294
|
-
</DialogDescription>
|
|
295
|
-
</DialogHeader>
|
|
296
|
-
<DialogFooter>
|
|
297
|
-
<DialogClose asChild>
|
|
298
|
-
<Button variant="outline">Cancel</Button>
|
|
299
|
-
</DialogClose>
|
|
300
|
-
<DialogClose asChild>
|
|
301
|
-
<Button
|
|
302
|
-
onClick={() => {
|
|
303
|
-
rollKeyMutation.mutate(consumer.id);
|
|
304
|
-
}}
|
|
305
|
-
>
|
|
306
|
-
Roll Key
|
|
307
|
-
</Button>
|
|
308
|
-
</DialogClose>
|
|
309
|
-
</DialogFooter>
|
|
310
|
-
</DialogContent>
|
|
311
|
-
</Dialog>
|
|
312
|
-
)}
|
|
313
|
-
</div>
|
|
314
|
-
</FrameHeader>
|
|
315
|
-
<FramePanel className="p-0 grid grid-cols-subgrid col-span-full divide-y divide-border">
|
|
316
|
-
<AnimatePresence>
|
|
317
|
-
{consumer.apiKeys.map((apiKey) => (
|
|
318
|
-
<RevealApiKey
|
|
319
|
-
key={apiKey.id}
|
|
320
|
-
apiKey={apiKey}
|
|
321
|
-
onDeleteKey={() => {
|
|
322
|
-
deleteKeyMutation.mutate({
|
|
323
|
-
consumerId: consumer.id,
|
|
324
|
-
keyId: apiKey.id,
|
|
325
|
-
});
|
|
326
|
-
}}
|
|
327
|
-
className={
|
|
328
|
-
deleteKeyMutation.variables?.keyId === apiKey.id &&
|
|
329
|
-
deleteKeyMutation.isPending
|
|
330
|
-
? "opacity-10!"
|
|
331
|
-
: undefined
|
|
332
|
-
}
|
|
333
|
-
/>
|
|
334
|
-
))}
|
|
335
|
-
</AnimatePresence>
|
|
336
|
-
</FramePanel>
|
|
337
|
-
</Frame>
|
|
338
|
-
</>
|
|
339
|
-
);
|
|
340
|
-
};
|
|
341
|
-
|
|
342
|
-
export default ApiKeyItem;
|
|
1
|
+
import { Alert, AlertTitle } from "@lukoweb/apitogo/ui/Alert.js";
|
|
2
|
+
import { Button } from "@lukoweb/apitogo/ui/Button.js";
|
|
3
|
+
import {
|
|
4
|
+
Dialog,
|
|
5
|
+
DialogClose,
|
|
6
|
+
DialogContent,
|
|
7
|
+
DialogDescription,
|
|
8
|
+
DialogFooter,
|
|
9
|
+
DialogHeader,
|
|
10
|
+
DialogTitle,
|
|
11
|
+
DialogTrigger,
|
|
12
|
+
} from "@lukoweb/apitogo/ui/Dialog.js";
|
|
13
|
+
import {
|
|
14
|
+
Frame,
|
|
15
|
+
FrameDescription,
|
|
16
|
+
FrameHeader,
|
|
17
|
+
FramePanel,
|
|
18
|
+
FrameTitle,
|
|
19
|
+
} from "@lukoweb/apitogo/ui/Frame.js";
|
|
20
|
+
import { Input } from "@lukoweb/apitogo/ui/Input.js";
|
|
21
|
+
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
|
22
|
+
import {
|
|
23
|
+
CheckIcon,
|
|
24
|
+
CircleSlashIcon,
|
|
25
|
+
PencilLineIcon,
|
|
26
|
+
RefreshCwIcon,
|
|
27
|
+
XIcon,
|
|
28
|
+
} from "lucide-react";
|
|
29
|
+
import { AnimatePresence } from "motion/react";
|
|
30
|
+
import { useState } from "react";
|
|
31
|
+
import { useZudoku } from "../../../components/context/ZudokuContext.js";
|
|
32
|
+
import type { ZudokuContext } from "../../../core/ZudokuContext.js";
|
|
33
|
+
import { cn } from "../../../util/cn.js";
|
|
34
|
+
import type { ApiConsumer } from "../index.js";
|
|
35
|
+
import { RevealApiKey } from "./RevealApiKey.js";
|
|
36
|
+
|
|
37
|
+
const ApiKeyItem = ({
|
|
38
|
+
consumer,
|
|
39
|
+
onUpdate,
|
|
40
|
+
onRollKey,
|
|
41
|
+
onDeleteKey,
|
|
42
|
+
}: {
|
|
43
|
+
consumer: ApiConsumer;
|
|
44
|
+
onUpdate?: (
|
|
45
|
+
data: { label: string; id: string },
|
|
46
|
+
context: ZudokuContext,
|
|
47
|
+
) => Promise<void>;
|
|
48
|
+
onRollKey?: (consumerId: string, context: ZudokuContext) => Promise<void>;
|
|
49
|
+
onDeleteKey?: (
|
|
50
|
+
consumerId: string,
|
|
51
|
+
keyId: string,
|
|
52
|
+
context: ZudokuContext,
|
|
53
|
+
) => Promise<void>;
|
|
54
|
+
}) => {
|
|
55
|
+
const [isEditing, setIsEditing] = useState(false);
|
|
56
|
+
const [editingLabel, setEditingLabel] = useState(consumer.label);
|
|
57
|
+
const queryClient = useQueryClient();
|
|
58
|
+
const context = useZudoku();
|
|
59
|
+
|
|
60
|
+
const rollKeyMutation = useMutation({
|
|
61
|
+
mutationFn: async (id: string) => {
|
|
62
|
+
if (!onRollKey) {
|
|
63
|
+
throw new Error("rollKey not implemented");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return await onRollKey?.(id, context);
|
|
67
|
+
},
|
|
68
|
+
onSuccess: () =>
|
|
69
|
+
void queryClient.invalidateQueries({ queryKey: ["api-keys"] }),
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const deleteKeyMutation = useMutation({
|
|
73
|
+
mutationFn: ({
|
|
74
|
+
consumerId,
|
|
75
|
+
keyId,
|
|
76
|
+
}: {
|
|
77
|
+
consumerId: string;
|
|
78
|
+
keyId: string;
|
|
79
|
+
}) => {
|
|
80
|
+
if (!onDeleteKey) {
|
|
81
|
+
throw new Error("deleteKey not implemented");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return onDeleteKey(consumerId, keyId, context);
|
|
85
|
+
},
|
|
86
|
+
onMutate: async ({ consumerId, keyId }) => {
|
|
87
|
+
await queryClient.cancelQueries({ queryKey: ["api-keys"] });
|
|
88
|
+
const previousData = queryClient.getQueryData<ApiConsumer[]>([
|
|
89
|
+
"api-keys",
|
|
90
|
+
]);
|
|
91
|
+
queryClient.setQueryData<ApiConsumer[]>(["api-keys"], (old) => {
|
|
92
|
+
if (!old) {
|
|
93
|
+
return old;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return old.map((consumer) => {
|
|
97
|
+
if (consumer.id === consumerId) {
|
|
98
|
+
return {
|
|
99
|
+
...consumer,
|
|
100
|
+
apiKeys: consumer.apiKeys.filter((key) => key.id !== keyId),
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
return consumer;
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
return { previousData };
|
|
108
|
+
},
|
|
109
|
+
onError: (_err, _variables, context) => {
|
|
110
|
+
if (context?.previousData) {
|
|
111
|
+
queryClient.setQueryData(["api-keys"], context.previousData);
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
onSuccess: () => {
|
|
115
|
+
void queryClient.invalidateQueries({ queryKey: ["api-keys"] });
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
const updateConsumerMutation = useMutation({
|
|
120
|
+
mutationFn: ({
|
|
121
|
+
consumerId,
|
|
122
|
+
label,
|
|
123
|
+
}: {
|
|
124
|
+
consumerId: string;
|
|
125
|
+
label: string;
|
|
126
|
+
}) => {
|
|
127
|
+
if (!onUpdate) {
|
|
128
|
+
throw new Error("updateConsumer not implemented");
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return onUpdate({ id: consumerId, label }, context);
|
|
132
|
+
},
|
|
133
|
+
onMutate: async ({ consumerId, label }) => {
|
|
134
|
+
await queryClient.cancelQueries({ queryKey: ["api-keys"] });
|
|
135
|
+
|
|
136
|
+
const previousData = queryClient.getQueryData(["api-keys"]);
|
|
137
|
+
queryClient.setQueryData<ApiConsumer[]>(["api-keys"], (old) => {
|
|
138
|
+
if (!old) {
|
|
139
|
+
return old;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return old.map((consumer) => {
|
|
143
|
+
if (consumer.id === consumerId) {
|
|
144
|
+
return {
|
|
145
|
+
...consumer,
|
|
146
|
+
label,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
return consumer;
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
return { previousData };
|
|
154
|
+
},
|
|
155
|
+
onError: (_err, _variables, context) => {
|
|
156
|
+
if (context?.previousData) {
|
|
157
|
+
queryClient.setQueryData(["api-keys"], context.previousData);
|
|
158
|
+
}
|
|
159
|
+
},
|
|
160
|
+
onSuccess: () => {
|
|
161
|
+
void queryClient.invalidateQueries({ queryKey: ["api-keys"] });
|
|
162
|
+
},
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
const handleStartEdit = () => {
|
|
166
|
+
setIsEditing(true);
|
|
167
|
+
setEditingLabel(consumer.label);
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const handleSaveEdit = () => {
|
|
171
|
+
if (editingLabel.trim()) {
|
|
172
|
+
updateConsumerMutation.mutate({
|
|
173
|
+
label: editingLabel.trim(),
|
|
174
|
+
consumerId: consumer.id,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
setIsEditing(false);
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
return (
|
|
181
|
+
<>
|
|
182
|
+
{rollKeyMutation.isError && (
|
|
183
|
+
<Alert variant="destructive" className="mb-4">
|
|
184
|
+
<CircleSlashIcon size={16} />
|
|
185
|
+
<AlertTitle>{rollKeyMutation.error.message}</AlertTitle>
|
|
186
|
+
</Alert>
|
|
187
|
+
)}
|
|
188
|
+
{updateConsumerMutation.isError && (
|
|
189
|
+
<Alert variant="destructive" className="mb-4">
|
|
190
|
+
<CircleSlashIcon size={16} />
|
|
191
|
+
<AlertTitle>{updateConsumerMutation.error.message}</AlertTitle>
|
|
192
|
+
</Alert>
|
|
193
|
+
)}
|
|
194
|
+
{deleteKeyMutation.isError && (
|
|
195
|
+
<Alert variant="destructive" className="mb-4">
|
|
196
|
+
<CircleSlashIcon size={16} />
|
|
197
|
+
<AlertTitle>{deleteKeyMutation.error.message}</AlertTitle>
|
|
198
|
+
</Alert>
|
|
199
|
+
)}
|
|
200
|
+
<Frame
|
|
201
|
+
className="grid grid-cols-subgrid col-span-full items-center mb-4 group"
|
|
202
|
+
key={consumer.id}
|
|
203
|
+
>
|
|
204
|
+
<FrameHeader className="col-span-full flex-row items-start justify-between gap-4">
|
|
205
|
+
<div className="flex flex-col gap-1">
|
|
206
|
+
{isEditing ? (
|
|
207
|
+
<div className="flex items-center gap-2">
|
|
208
|
+
<Input
|
|
209
|
+
maxLength={32}
|
|
210
|
+
value={editingLabel}
|
|
211
|
+
onChange={(e) => setEditingLabel(e.target.value)}
|
|
212
|
+
onKeyDown={(e) => {
|
|
213
|
+
if (e.key === "Enter") {
|
|
214
|
+
handleSaveEdit();
|
|
215
|
+
} else if (e.key === "Escape") {
|
|
216
|
+
setIsEditing(false);
|
|
217
|
+
}
|
|
218
|
+
}}
|
|
219
|
+
autoFocus
|
|
220
|
+
/>
|
|
221
|
+
<div className="flex items-center">
|
|
222
|
+
<Button
|
|
223
|
+
size="icon"
|
|
224
|
+
variant="ghost"
|
|
225
|
+
onClick={handleSaveEdit}
|
|
226
|
+
disabled={!editingLabel.trim()}
|
|
227
|
+
>
|
|
228
|
+
<CheckIcon size={16} />
|
|
229
|
+
</Button>
|
|
230
|
+
<Button
|
|
231
|
+
size="icon"
|
|
232
|
+
variant="ghost"
|
|
233
|
+
onClick={() => setIsEditing(false)}
|
|
234
|
+
>
|
|
235
|
+
<XIcon size={16} />
|
|
236
|
+
</Button>
|
|
237
|
+
</div>
|
|
238
|
+
</div>
|
|
239
|
+
) : (
|
|
240
|
+
<FrameTitle>{consumer.label}</FrameTitle>
|
|
241
|
+
)}
|
|
242
|
+
<FrameDescription>
|
|
243
|
+
{consumer.createdOn && (
|
|
244
|
+
<div>
|
|
245
|
+
Created on {new Date(consumer.createdOn).toLocaleDateString()}
|
|
246
|
+
</div>
|
|
247
|
+
)}
|
|
248
|
+
{consumer.expiresOn && (
|
|
249
|
+
<div>
|
|
250
|
+
Expires on {new Date(consumer.expiresOn).toLocaleDateString()}
|
|
251
|
+
</div>
|
|
252
|
+
)}
|
|
253
|
+
</FrameDescription>
|
|
254
|
+
</div>
|
|
255
|
+
|
|
256
|
+
<div className="flex gap-1">
|
|
257
|
+
{onUpdate && (
|
|
258
|
+
<Button
|
|
259
|
+
variant="ghost"
|
|
260
|
+
onClick={handleStartEdit}
|
|
261
|
+
className={cn(
|
|
262
|
+
"flex gap-2",
|
|
263
|
+
isEditing && "opacity-0! pointer-events-none",
|
|
264
|
+
)}
|
|
265
|
+
disabled={isEditing}
|
|
266
|
+
>
|
|
267
|
+
<PencilLineIcon size={16} />
|
|
268
|
+
<span className="hidden md:block">Edit label</span>
|
|
269
|
+
</Button>
|
|
270
|
+
)}
|
|
271
|
+
{onRollKey && (
|
|
272
|
+
<Dialog>
|
|
273
|
+
<DialogTrigger asChild>
|
|
274
|
+
<Button
|
|
275
|
+
title="Roll this key"
|
|
276
|
+
variant="ghost"
|
|
277
|
+
disabled={rollKeyMutation.isPending}
|
|
278
|
+
className="flex items-center gap-2"
|
|
279
|
+
>
|
|
280
|
+
<RefreshCwIcon
|
|
281
|
+
size={16}
|
|
282
|
+
className={
|
|
283
|
+
rollKeyMutation.isPending ? "animate-spin" : undefined
|
|
284
|
+
}
|
|
285
|
+
/>
|
|
286
|
+
<span className="hidden md:block">Roll key</span>
|
|
287
|
+
</Button>
|
|
288
|
+
</DialogTrigger>
|
|
289
|
+
<DialogContent>
|
|
290
|
+
<DialogHeader>
|
|
291
|
+
<DialogTitle>Roll API Key</DialogTitle>
|
|
292
|
+
<DialogDescription>
|
|
293
|
+
Are you sure you want to roll this API key?
|
|
294
|
+
</DialogDescription>
|
|
295
|
+
</DialogHeader>
|
|
296
|
+
<DialogFooter>
|
|
297
|
+
<DialogClose asChild>
|
|
298
|
+
<Button variant="outline">Cancel</Button>
|
|
299
|
+
</DialogClose>
|
|
300
|
+
<DialogClose asChild>
|
|
301
|
+
<Button
|
|
302
|
+
onClick={() => {
|
|
303
|
+
rollKeyMutation.mutate(consumer.id);
|
|
304
|
+
}}
|
|
305
|
+
>
|
|
306
|
+
Roll Key
|
|
307
|
+
</Button>
|
|
308
|
+
</DialogClose>
|
|
309
|
+
</DialogFooter>
|
|
310
|
+
</DialogContent>
|
|
311
|
+
</Dialog>
|
|
312
|
+
)}
|
|
313
|
+
</div>
|
|
314
|
+
</FrameHeader>
|
|
315
|
+
<FramePanel className="p-0 grid grid-cols-subgrid col-span-full divide-y divide-border">
|
|
316
|
+
<AnimatePresence>
|
|
317
|
+
{consumer.apiKeys.map((apiKey) => (
|
|
318
|
+
<RevealApiKey
|
|
319
|
+
key={apiKey.id}
|
|
320
|
+
apiKey={apiKey}
|
|
321
|
+
onDeleteKey={() => {
|
|
322
|
+
deleteKeyMutation.mutate({
|
|
323
|
+
consumerId: consumer.id,
|
|
324
|
+
keyId: apiKey.id,
|
|
325
|
+
});
|
|
326
|
+
}}
|
|
327
|
+
className={
|
|
328
|
+
deleteKeyMutation.variables?.keyId === apiKey.id &&
|
|
329
|
+
deleteKeyMutation.isPending
|
|
330
|
+
? "opacity-10!"
|
|
331
|
+
: undefined
|
|
332
|
+
}
|
|
333
|
+
/>
|
|
334
|
+
))}
|
|
335
|
+
</AnimatePresence>
|
|
336
|
+
</FramePanel>
|
|
337
|
+
</Frame>
|
|
338
|
+
</>
|
|
339
|
+
);
|
|
340
|
+
};
|
|
341
|
+
|
|
342
|
+
export default ApiKeyItem;
|