create-expo-updates-server 0.3.0 → 0.6.0
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/README.md +4 -2
- package/dist/template/Dockerfile +4 -6
- package/dist/template/README.md +5 -10
- package/dist/template/app/api/admin/device-favorites/route.test.ts +125 -0
- package/dist/template/app/api/admin/device-favorites/route.ts +91 -0
- package/dist/template/app/api/admin/distribution-history/restore/route.ts +39 -0
- package/dist/template/app/api/admin/distribution-history/route.test.ts +55 -0
- package/dist/template/app/api/admin/distribution-history/route.ts +24 -0
- package/dist/template/app/api/admin/updates/[encodedKey]/deactivate/route.ts +2 -1
- package/dist/template/app/api/admin/updates/[encodedKey]/promote/route.ts +2 -1
- package/dist/template/app/api/admin/updates/[encodedKey]/rollback/route.ts +2 -1
- package/dist/template/app/api/admin/updates/[encodedKey]/route.ts +2 -1
- package/dist/template/app/api/admin/updates/route.test.ts +21 -0
- package/dist/template/app/api/admin/updates/route.ts +12 -1
- package/dist/template/app/globals.css +65 -68
- package/dist/template/app/login/page.tsx +66 -42
- package/dist/template/app/updates/[encodedKey]/page.tsx +382 -248
- package/dist/template/app/updates/devices/page.tsx +187 -145
- package/dist/template/app/updates/embedded/page.tsx +80 -34
- package/dist/template/app/updates/emergency-redirects/page.tsx +138 -71
- package/dist/template/app/updates/layout.tsx +2 -1
- package/dist/template/app/updates/page.tsx +385 -274
- package/dist/template/app/updates/releases.test.tsx +156 -0
- package/dist/template/components/admin/admin-role.ts +5 -0
- package/dist/template/components/admin/admin-shell.tsx +166 -95
- package/dist/template/components/admin/confirm-action.tsx +123 -0
- package/dist/template/components/admin/language-switcher.tsx +27 -16
- package/dist/template/components/admin/release-badges.test.tsx +50 -0
- package/dist/template/components/admin/release-badges.tsx +101 -0
- package/dist/template/components/ui/alert.tsx +10 -3
- package/dist/template/components/ui/avatar.tsx +108 -0
- package/dist/template/components/ui/badge.tsx +2 -2
- package/dist/template/components/ui/breadcrumb.tsx +108 -0
- package/dist/template/components/ui/button.tsx +5 -5
- package/dist/template/components/ui/card.tsx +2 -2
- package/dist/template/components/ui/chart.tsx +1 -1
- package/dist/template/components/ui/collapsible.tsx +7 -0
- package/dist/template/components/ui/dialog.tsx +13 -4
- package/dist/template/components/ui/empty.tsx +103 -0
- package/dist/template/components/ui/field.tsx +248 -0
- package/dist/template/components/ui/input-group.tsx +170 -0
- package/dist/template/components/ui/input.tsx +1 -1
- package/dist/template/components/ui/native-select.tsx +1 -1
- package/dist/template/components/ui/radio-group.tsx +41 -0
- package/dist/template/components/ui/sheet.tsx +6 -2
- package/dist/template/components/ui/sidebar.tsx +62 -17
- package/dist/template/components/ui/status-tones.ts +15 -0
- package/dist/template/components/ui/table.tsx +1 -1
- package/dist/template/components/ui/textarea.tsx +1 -1
- package/dist/template/docs/admin-ui.md +22 -24
- package/dist/template/docs/device-targeting.md +6 -12
- package/dist/template/docs/doctor.md +2 -2
- package/dist/template/docs/expo-app-setup.md +9 -27
- package/dist/template/docs/getting-started.md +3 -4
- package/dist/template/docs/getting-started.ru.md +11 -544
- package/dist/template/docs/index.md +1 -1
- package/dist/template/docs/installation-telemetry.md +6 -12
- package/dist/template/docs/migrations/2026-10-16-manifest-identity.sql +5 -0
- package/dist/template/docs/migrations/2026-11-25-device-favorites.sql +53 -0
- package/dist/template/docs/migrations/2026-11-25-download-on-postpone.sql +5 -0
- package/dist/template/docs/migrations/2026-11-25-public-distribution-history.sql +283 -0
- package/dist/template/docs/migrations/schema.sql +349 -0
- package/dist/template/docs/npm-installer.md +3 -3
- package/dist/template/docs/operations-guide.md +9 -7
- package/dist/template/docs/ota-client-lifecycle.md +30 -68
- package/dist/template/docs/ota-emergency-recovery.md +34 -57
- package/dist/template/docs/ota-update-policy.md +9 -20
- package/dist/template/docs/ru/admin-ui.md +33 -0
- package/dist/template/docs/ru/device-targeting.md +59 -0
- package/dist/template/docs/ru/doctor.md +98 -0
- package/dist/template/docs/ru/expo-app-setup.md +497 -0
- package/dist/template/docs/ru/getting-started.md +391 -0
- package/dist/template/docs/ru/index.md +50 -0
- package/dist/template/docs/ru/installation-telemetry.md +125 -0
- package/dist/template/docs/ru/npm-installer.md +71 -0
- package/dist/template/docs/ru/operations-guide.md +322 -0
- package/dist/template/docs/ru/ota-client-lifecycle.md +162 -0
- package/dist/template/docs/ru/ota-emergency-recovery.md +108 -0
- package/dist/template/docs/ru/ota-update-policy.md +69 -0
- package/dist/template/docs/ru/publishing-npm-installer.md +49 -0
- package/dist/template/docs/ru/supabase-setup.md +120 -0
- package/dist/template/docs/uk/admin-ui.md +33 -0
- package/dist/template/docs/uk/device-targeting.md +59 -0
- package/dist/template/docs/uk/doctor.md +98 -0
- package/dist/template/docs/uk/expo-app-setup.md +497 -0
- package/dist/template/docs/uk/getting-started.md +391 -0
- package/dist/template/docs/uk/index.md +50 -0
- package/dist/template/docs/uk/installation-telemetry.md +125 -0
- package/dist/template/docs/uk/npm-installer.md +71 -0
- package/dist/template/docs/uk/operations-guide.md +322 -0
- package/dist/template/docs/uk/ota-client-lifecycle.md +162 -0
- package/dist/template/docs/uk/ota-emergency-recovery.md +108 -0
- package/dist/template/docs/uk/ota-update-policy.md +69 -0
- package/dist/template/docs/uk/publishing-npm-installer.md +49 -0
- package/dist/template/docs/uk/supabase-setup.md +120 -0
- package/dist/template/lib/audience-ids.ts +4 -0
- package/dist/template/package-lock.json +46 -0
- package/dist/template/package.json +4 -1
- package/dist/template/scripts/configure-expo-app.mjs +3 -3
- package/dist/template/scripts/create-access-token.mjs +1 -1
- package/dist/template/scripts/lib/doctor.mjs +10 -7
- package/dist/template/scripts/lib/doctor.test.mjs +7 -5
- package/dist/template/scripts/lib/embedded-update-registrar.test.ts +22 -7
- package/dist/template/scripts/lib/expo-app-configurator.mjs +25 -22
- package/dist/template/scripts/lib/expo-app-configurator.test.ts +37 -2
- package/dist/template/scripts/lib/ota-client-configurator.mjs +152 -55
- package/dist/template/scripts/lib/ota-client-configurator.test.ts +60 -20
- package/dist/template/scripts/lib/ota-client-package.mjs +25 -0
- package/dist/template/scripts/lib/ota-root-hook-patcher.mjs +90 -32
- package/dist/template/scripts/lib/ota-root-hook-patcher.test.ts +49 -6
- package/dist/template/scripts/tests/device-analytics-migrations.mjs +12 -1
- package/dist/template/scripts/tests/device-targeting-activation.mjs +2 -3
- package/dist/template/scripts/tests/e2e/client-lifecycle.mjs +161 -0
- package/dist/template/scripts/tests/e2e/client-lifecycle.scenario.jsx +465 -0
- package/dist/template/scripts/tests/e2e/client-vitest.mts +18 -0
- package/dist/template/scripts/tests/e2e/console.mjs +86 -0
- package/dist/template/scripts/tests/e2e/delivery.mjs +296 -0
- package/dist/template/scripts/tests/e2e/device-favorites.mjs +309 -0
- package/dist/template/scripts/tests/e2e/installations.mjs +132 -0
- package/dist/template/scripts/tests/e2e/policy.mjs +253 -0
- package/dist/template/scripts/tests/e2e/registrar-token.mjs +59 -0
- package/dist/template/scripts/tests/e2e/release-matrix.mjs +307 -0
- package/dist/template/scripts/tests/e2e/releases.mjs +162 -0
- package/dist/template/scripts/tests/e2e/run.mjs +224 -0
- package/dist/template/scripts/tests/manifest-identity.sql +7 -5
- package/dist/template/scripts/tests/public-distribution-history.mjs +102 -0
- package/dist/template/src/client/admin-api.ts +45 -10
- package/dist/template/src/client/admin-types.ts +35 -1
- package/dist/template/src/client/devices/analytics.tsx +169 -219
- package/dist/template/src/client/devices/cohorts.tsx +181 -0
- package/dist/template/src/client/devices/device-name.tsx +37 -21
- package/dist/template/src/client/devices/favorite-targets.tsx +381 -0
- package/dist/template/src/client/devices/registry.tsx +265 -233
- package/dist/template/src/client/devices/use-devices-report.test.tsx +98 -0
- package/dist/template/src/client/devices/use-devices-report.ts +53 -82
- package/dist/template/src/client/devices/version-adoption-chart.tsx +48 -38
- package/dist/template/src/client/distribution-control.tsx +98 -53
- package/dist/template/src/client/distribution-history.tsx +353 -0
- package/dist/template/src/client/guard-action-catalog.tsx +183 -0
- package/dist/template/src/client/guard-action-combobox.test.ts +16 -22
- package/dist/template/src/client/guard-action-combobox.tsx +134 -234
- package/dist/template/src/client/guard-action-options.ts +4 -6
- package/dist/template/src/client/i18n/en.json +335 -2
- package/dist/template/src/client/i18n/language-switching.test.tsx +233 -0
- package/dist/template/src/client/i18n/messages.test.ts +102 -19
- package/dist/template/src/client/i18n/messages.ts +31 -16
- package/dist/template/src/client/i18n/provider.tsx +6 -3
- package/dist/template/src/client/i18n/ru.json +619 -0
- package/dist/template/src/client/i18n/uk.json +619 -0
- package/dist/template/src/client/i18n/use-formatters.ts +5 -0
- package/dist/template/src/client/logout-button.tsx +3 -3
- package/dist/template/src/client/policy-draft.ts +138 -0
- package/dist/template/src/client/update-audience-editor.tsx +597 -199
- package/dist/template/src/client/update-policy-editor.test.ts +101 -29
- package/dist/template/src/client/update-policy-editor.tsx +466 -307
- package/dist/template/src/client/use-filtered-results.ts +55 -0
- package/dist/template/src/server/controllers/manifest.test.ts +23 -3
- package/dist/template/src/server/controllers/manifest.ts +15 -1
- package/dist/template/src/server/devices/audience.ts +9 -5
- package/dist/template/src/server/lib/admin-audience-activation.test.ts +37 -90
- package/dist/template/src/server/lib/admin-release-history.test.ts +138 -0
- package/dist/template/src/server/lib/admin-updates.ts +160 -534
- package/dist/template/src/server/lib/distribution-history.ts +97 -0
- package/dist/template/src/server/lib/supabase.js +24 -0
- package/dist/template/src/server/lib/supabase.test.ts +38 -1
- package/dist/template/src/server/lib/update-policy.test.ts +20 -0
- package/dist/template/src/server/lib/update-policy.ts +23 -10
- package/dist/template/src/server/manifest/manifest-repository.ts +11 -3
- package/dist/template/src/server/manifest/manifest-values.test.ts +2 -0
- package/dist/template/src/server/manifest/manifest-values.ts +21 -13
- package/dist/template/templates/expo-app/scripts/ota-publish/api/database-client.mjs +7 -30
- package/dist/template/templates/expo-app/scripts/ota-register-embedded/index.mjs +2 -1
- package/dist/template/tsconfig.json +2 -2
- package/dist/template-manifest.json +571 -271
- package/package.json +1 -1
- package/dist/template/scripts/lib/device-identity.test.ts +0 -29
- package/dist/template/scripts/lib/ota-client.test.ts +0 -55
- package/dist/template/scripts/lib/ota-update-controller.test.ts +0 -232
- package/dist/template/scripts/lib/ota-updates-ui.test.tsx +0 -154
- package/dist/template/scripts/tests/ota-client-types.mjs +0 -69
- package/dist/template/templates/expo-app/ota-client/README.md +0 -73
- package/dist/template/templates/expo-app/ota-client/controller.ts +0 -107
- package/dist/template/templates/expo-app/ota-client/device-identity.ts +0 -48
- package/dist/template/templates/expo-app/ota-client/docs/update-lifecycle.md +0 -61
- package/dist/template/templates/expo-app/ota-client/index.ts +0 -16
- package/dist/template/templates/expo-app/ota-client/model/hooks/useOtaUpdates.ts +0 -97
- package/dist/template/templates/expo-app/ota-client/model/update-controller.ts +0 -293
- package/dist/template/templates/expo-app/ota-client/runtime.ts +0 -153
- package/dist/template/templates/expo-app/ota-client/types.ts +0 -85
- package/dist/template/templates/expo-app/ota-client/ui/ota-update-gate.tsx +0 -25
- package/dist/template/templates/expo-app/ota-client/ui/ota-updates.tsx +0 -196
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useState, type FormEvent } from "react";
|
|
4
|
+
import { ChevronDown, Trash2 } from "lucide-react";
|
|
5
|
+
import { Button } from "@/components/ui/button";
|
|
6
|
+
import {
|
|
7
|
+
Collapsible,
|
|
8
|
+
CollapsibleContent,
|
|
9
|
+
CollapsibleTrigger,
|
|
10
|
+
} from "@/components/ui/collapsible";
|
|
11
|
+
import { Field, FieldLabel, FieldDescription } from "@/components/ui/field";
|
|
12
|
+
import { Input } from "@/components/ui/input";
|
|
13
|
+
import { useConfirmAction } from "@/components/admin/confirm-action";
|
|
14
|
+
import { createGuardAction, deleteGuardAction } from "./admin-api";
|
|
15
|
+
import type { GuardActionItem } from "./admin-types";
|
|
16
|
+
import {
|
|
17
|
+
filterGuardActionOptions,
|
|
18
|
+
normalizeCreatableGuardAction,
|
|
19
|
+
} from "./guard-action-options";
|
|
20
|
+
import { useI18n } from "./i18n/provider";
|
|
21
|
+
|
|
22
|
+
export default function GuardActionCatalog({
|
|
23
|
+
items,
|
|
24
|
+
writable,
|
|
25
|
+
onAdd,
|
|
26
|
+
onDelete,
|
|
27
|
+
}: {
|
|
28
|
+
items: GuardActionItem[];
|
|
29
|
+
writable: boolean;
|
|
30
|
+
onAdd: (item: GuardActionItem) => void;
|
|
31
|
+
onDelete: (id: string) => void;
|
|
32
|
+
}) {
|
|
33
|
+
const { t } = useI18n();
|
|
34
|
+
const confirm = useConfirmAction();
|
|
35
|
+
const [name, setName] = useState("");
|
|
36
|
+
const [query, setQuery] = useState("");
|
|
37
|
+
const [error, setError] = useState("");
|
|
38
|
+
const [busy, setBusy] = useState(false);
|
|
39
|
+
const filtered = filterGuardActionOptions(items, query);
|
|
40
|
+
async function add(event: FormEvent) {
|
|
41
|
+
event.preventDefault();
|
|
42
|
+
if (!writable || busy) return;
|
|
43
|
+
setError("");
|
|
44
|
+
setBusy(true);
|
|
45
|
+
setQuery("");
|
|
46
|
+
try {
|
|
47
|
+
onAdd(await createGuardAction(normalizeCreatableGuardAction(name)));
|
|
48
|
+
setName("");
|
|
49
|
+
} catch (cause) {
|
|
50
|
+
setError(
|
|
51
|
+
(cause as Error).message || "Failed to add action to the catalog.",
|
|
52
|
+
);
|
|
53
|
+
} finally {
|
|
54
|
+
setBusy(false);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
async function remove(item: GuardActionItem) {
|
|
58
|
+
if (
|
|
59
|
+
!writable ||
|
|
60
|
+
busy ||
|
|
61
|
+
!(await confirm(
|
|
62
|
+
t("Delete from catalog"),
|
|
63
|
+
t(
|
|
64
|
+
'Delete "{0}" from the action catalog? Existing policies are unaffected.',
|
|
65
|
+
[item.actionKey],
|
|
66
|
+
),
|
|
67
|
+
true,
|
|
68
|
+
))
|
|
69
|
+
)
|
|
70
|
+
return;
|
|
71
|
+
setError("");
|
|
72
|
+
setBusy(true);
|
|
73
|
+
try {
|
|
74
|
+
await deleteGuardAction(item.id);
|
|
75
|
+
onDelete(item.id);
|
|
76
|
+
} catch (cause) {
|
|
77
|
+
setError(
|
|
78
|
+
(cause as Error).message || "Failed to delete action from catalog",
|
|
79
|
+
);
|
|
80
|
+
} finally {
|
|
81
|
+
setBusy(false);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return (
|
|
85
|
+
<Collapsible className="border-t px-5 py-3">
|
|
86
|
+
<CollapsibleTrigger asChild>
|
|
87
|
+
<Button type="button" size="sm" variant="ghost" className="group -ml-2">
|
|
88
|
+
{t("Action catalog")}
|
|
89
|
+
<ChevronDown className="group-data-[state=open]:rotate-180" />
|
|
90
|
+
</Button>
|
|
91
|
+
</CollapsibleTrigger>
|
|
92
|
+
<CollapsibleContent className="mt-3 grid gap-4">
|
|
93
|
+
{writable && (
|
|
94
|
+
<form onSubmit={add}>
|
|
95
|
+
<Field className="gap-1.5">
|
|
96
|
+
<FieldLabel htmlFor="policy-catalog-new">
|
|
97
|
+
{t("Add action to catalog")}
|
|
98
|
+
</FieldLabel>
|
|
99
|
+
<div className="flex flex-wrap gap-2">
|
|
100
|
+
<Input
|
|
101
|
+
id="policy-catalog-new"
|
|
102
|
+
className="min-w-32 flex-1"
|
|
103
|
+
maxLength={100}
|
|
104
|
+
value={name}
|
|
105
|
+
disabled={busy}
|
|
106
|
+
aria-describedby="policy-catalog-help"
|
|
107
|
+
onChange={(e) => {
|
|
108
|
+
setName(e.target.value);
|
|
109
|
+
setError("");
|
|
110
|
+
}}
|
|
111
|
+
placeholder={t("Reusable action name")}
|
|
112
|
+
/>
|
|
113
|
+
<Button
|
|
114
|
+
type="submit"
|
|
115
|
+
size="sm"
|
|
116
|
+
variant="outline"
|
|
117
|
+
disabled={busy || !name.trim()}
|
|
118
|
+
>
|
|
119
|
+
{t("Add to catalog")}
|
|
120
|
+
</Button>
|
|
121
|
+
</div>
|
|
122
|
+
<FieldDescription id="policy-catalog-help">
|
|
123
|
+
{t(
|
|
124
|
+
"Adds a reusable catalog item without changing this policy.",
|
|
125
|
+
)}
|
|
126
|
+
</FieldDescription>
|
|
127
|
+
</Field>
|
|
128
|
+
</form>
|
|
129
|
+
)}
|
|
130
|
+
{items.length > 0 && (
|
|
131
|
+
<Input
|
|
132
|
+
type="search"
|
|
133
|
+
value={query}
|
|
134
|
+
onChange={(e) => setQuery(e.target.value)}
|
|
135
|
+
aria-label={t("Search action catalog")}
|
|
136
|
+
placeholder={t("Search action catalog")}
|
|
137
|
+
/>
|
|
138
|
+
)}
|
|
139
|
+
{filtered.length > 0 ? (
|
|
140
|
+
<ul
|
|
141
|
+
aria-label={t("Guard action catalog entries")}
|
|
142
|
+
className="max-h-44 divide-y overflow-auto rounded-md border"
|
|
143
|
+
>
|
|
144
|
+
{filtered.map((item) => (
|
|
145
|
+
<li
|
|
146
|
+
key={item.id}
|
|
147
|
+
className="flex items-center justify-between gap-2 px-3 py-1.5 text-sm"
|
|
148
|
+
>
|
|
149
|
+
<span className="min-w-0 break-all font-mono text-xs">
|
|
150
|
+
{item.actionKey}
|
|
151
|
+
</span>
|
|
152
|
+
{writable && (
|
|
153
|
+
<Button
|
|
154
|
+
type="button"
|
|
155
|
+
variant="ghost"
|
|
156
|
+
className="text-destructive hover:bg-destructive/10 hover:text-destructive"
|
|
157
|
+
size="icon-sm"
|
|
158
|
+
disabled={busy}
|
|
159
|
+
aria-label={t("Delete {0} from action catalog", [
|
|
160
|
+
item.actionKey,
|
|
161
|
+
])}
|
|
162
|
+
onClick={() => void remove(item)}
|
|
163
|
+
>
|
|
164
|
+
<Trash2 className="size-4" />
|
|
165
|
+
</Button>
|
|
166
|
+
)}
|
|
167
|
+
</li>
|
|
168
|
+
))}
|
|
169
|
+
</ul>
|
|
170
|
+
) : (
|
|
171
|
+
<p className="text-sm text-muted-foreground">
|
|
172
|
+
{t(query ? "No matching actions" : "No catalog actions found.")}
|
|
173
|
+
</p>
|
|
174
|
+
)}
|
|
175
|
+
{error && (
|
|
176
|
+
<p className="text-sm text-destructive" role="alert">
|
|
177
|
+
{t(error)}
|
|
178
|
+
</p>
|
|
179
|
+
)}
|
|
180
|
+
</CollapsibleContent>
|
|
181
|
+
</Collapsible>
|
|
182
|
+
);
|
|
183
|
+
}
|
|
@@ -1,26 +1,20 @@
|
|
|
1
|
-
import { readFileSync } from "node:fs";
|
|
2
1
|
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { mergeGuardActionOptions } from "./guard-action-options";
|
|
3
|
+
import { isBuiltInAction } from "./policy-draft";
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
expect(
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const optionRow = source.slice(
|
|
19
|
-
source.indexOf("filteredOptions.map"),
|
|
20
|
-
source.indexOf("{showCreate ?"),
|
|
21
|
-
);
|
|
22
|
-
expect(optionRow.match(/<button/g)).toHaveLength(2);
|
|
23
|
-
expect(optionRow).toContain("chooseOption(option)");
|
|
24
|
-
expect(optionRow).toContain("void onDelete(option)");
|
|
5
|
+
describe("custom action selection", () => {
|
|
6
|
+
it("offers catalog and orphaned action keys, with built-in actions in their own profiles", () => {
|
|
7
|
+
const catalog = [
|
|
8
|
+
{ id: "1", actionKey: "ota-force-update", createdAt: "" },
|
|
9
|
+
{ id: "2", actionKey: "app-check", createdAt: "" },
|
|
10
|
+
];
|
|
11
|
+
const custom = mergeGuardActionOptions(catalog, [
|
|
12
|
+
"missing-from-catalog",
|
|
13
|
+
"ota-force-store-update",
|
|
14
|
+
]).filter((option) => !isBuiltInAction(option.actionKey));
|
|
15
|
+
expect(custom.map((o) => [o.actionKey, o.persisted])).toEqual([
|
|
16
|
+
["app-check", true],
|
|
17
|
+
["missing-from-catalog", false],
|
|
18
|
+
]);
|
|
25
19
|
});
|
|
26
20
|
});
|
|
@@ -1,256 +1,156 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
|
|
2
|
+
import { useRef, useState } from "react";
|
|
3
|
+
import { Plus } from "lucide-react";
|
|
4
|
+
import { Button } from "@/components/ui/button";
|
|
3
5
|
import { Input } from "@/components/ui/input";
|
|
4
|
-
|
|
6
|
+
import { Label } from "@/components/ui/label";
|
|
7
|
+
import {
|
|
8
|
+
Select,
|
|
9
|
+
SelectTrigger,
|
|
10
|
+
SelectValue,
|
|
11
|
+
SelectContent,
|
|
12
|
+
SelectItem,
|
|
13
|
+
} from "@/components/ui/select";
|
|
14
|
+
import { useI18n } from "./i18n/provider";
|
|
5
15
|
import {
|
|
6
|
-
filterGuardActionOptions,
|
|
7
|
-
GuardActionOption,
|
|
8
16
|
normalizeCreatableGuardAction,
|
|
17
|
+
type GuardActionOption,
|
|
9
18
|
} from "./guard-action-options";
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
type Props = {
|
|
13
|
-
value: string;
|
|
14
|
-
options: GuardActionOption[];
|
|
15
|
-
selectionDisabled: boolean;
|
|
16
|
-
canMutateCatalog: boolean;
|
|
17
|
-
mutationError?: string;
|
|
18
|
-
onChange: (actionKey: string) => void;
|
|
19
|
-
onCreate: (actionKey: string) => Promise<void>;
|
|
20
|
-
onDelete: (option: GuardActionOption) => Promise<void>;
|
|
21
|
-
onRetry?: () => Promise<void>;
|
|
22
|
-
};
|
|
19
|
+
import { isBuiltInAction } from "./policy-draft";
|
|
23
20
|
|
|
24
|
-
export default function
|
|
21
|
+
export default function GuardActionPicker({
|
|
25
22
|
value,
|
|
26
23
|
options,
|
|
27
|
-
|
|
28
|
-
canMutateCatalog,
|
|
29
|
-
mutationError,
|
|
24
|
+
disabled,
|
|
30
25
|
onChange,
|
|
31
26
|
onCreate,
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const
|
|
40
|
-
const
|
|
41
|
-
const
|
|
42
|
-
const [
|
|
43
|
-
const
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
} catch {
|
|
52
|
-
// Validation feedback is shown when the user attempts to create.
|
|
27
|
+
}: {
|
|
28
|
+
value: string;
|
|
29
|
+
options: GuardActionOption[];
|
|
30
|
+
disabled: boolean;
|
|
31
|
+
onChange: (value: string) => void;
|
|
32
|
+
onCreate: (value: string) => Promise<void>;
|
|
33
|
+
}) {
|
|
34
|
+
const { t } = useI18n();
|
|
35
|
+
const selectRef = useRef<HTMLButtonElement>(null);
|
|
36
|
+
const newActionRef = useRef<HTMLButtonElement>(null);
|
|
37
|
+
const [creating, setCreating] = useState(false);
|
|
38
|
+
const [name, setName] = useState("");
|
|
39
|
+
const [error, setError] = useState("");
|
|
40
|
+
const choices = options.filter(
|
|
41
|
+
(option) => !isBuiltInAction(option.actionKey),
|
|
42
|
+
);
|
|
43
|
+
function cancelCreation() {
|
|
44
|
+
setCreating(false);
|
|
45
|
+
requestAnimationFrame(() => newActionRef.current?.focus());
|
|
53
46
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
: undefined;
|
|
57
|
-
const showCreate =
|
|
58
|
-
!selectionDisabled && Boolean(creatableValue) && !exactOption;
|
|
59
|
-
const rowCount = filteredOptions.length + (showCreate ? 1 : 0);
|
|
60
|
-
|
|
61
|
-
const choose = (actionKey: string) => {
|
|
62
|
-
onChange(actionKey);
|
|
63
|
-
setDraftValue(null);
|
|
64
|
-
setInputError("");
|
|
65
|
-
setOpen(false);
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
const chooseOption = (option: GuardActionOption) => {
|
|
69
|
-
choose(option.actionKey);
|
|
70
|
-
if (!option.persisted) {
|
|
71
|
-
void onCreate(option.actionKey);
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
const create = async () => {
|
|
76
|
-
let actionKey: string;
|
|
47
|
+
function add() {
|
|
48
|
+
let key: string;
|
|
77
49
|
try {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
50
|
+
key = normalizeCreatableGuardAction(name);
|
|
51
|
+
if (isBuiltInAction(key))
|
|
52
|
+
throw new Error(
|
|
53
|
+
"Choose the corresponding built-in behavior for this action.",
|
|
54
|
+
);
|
|
55
|
+
} catch (e) {
|
|
56
|
+
setError((e as Error).message);
|
|
81
57
|
return;
|
|
82
58
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
chooseOption(filteredOptions[index]);
|
|
91
|
-
} else if (showCreate) {
|
|
92
|
-
void create();
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
const handleKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {
|
|
97
|
-
if (selectionDisabled) {
|
|
98
|
-
if (event.key === "Escape") setOpen(false);
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
|
|
102
|
-
event.preventDefault();
|
|
103
|
-
setOpen(true);
|
|
104
|
-
const direction = event.key === "ArrowDown" ? 1 : -1;
|
|
105
|
-
setActiveIndex((current) => {
|
|
106
|
-
if (rowCount === 0) return -1;
|
|
107
|
-
if (current < 0) return direction > 0 ? 0 : rowCount - 1;
|
|
108
|
-
return (current + direction + rowCount) % rowCount;
|
|
109
|
-
});
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
if (event.key === "Enter") {
|
|
113
|
-
event.preventDefault();
|
|
114
|
-
if (open && activeIndex >= 0) {
|
|
115
|
-
activateRow(activeIndex);
|
|
116
|
-
} else if (exactOption) {
|
|
117
|
-
chooseOption(exactOption);
|
|
118
|
-
} else {
|
|
119
|
-
void create();
|
|
120
|
-
}
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
if (event.key === "Escape") {
|
|
124
|
-
setDraftValue(null);
|
|
125
|
-
setInputError("");
|
|
126
|
-
setOpen(false);
|
|
127
|
-
}
|
|
128
|
-
};
|
|
129
|
-
|
|
59
|
+
onChange(key);
|
|
60
|
+
setCreating(false);
|
|
61
|
+
setName("");
|
|
62
|
+
requestAnimationFrame(() => selectRef.current?.focus());
|
|
63
|
+
if (!choices.some((o) => o.actionKey === key && o.persisted))
|
|
64
|
+
void onCreate(key);
|
|
65
|
+
}
|
|
130
66
|
return (
|
|
131
|
-
<div
|
|
132
|
-
|
|
133
|
-
className="
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
role="combobox"
|
|
147
|
-
aria-label="Rule action"
|
|
148
|
-
aria-autocomplete="list"
|
|
149
|
-
aria-haspopup="dialog"
|
|
150
|
-
aria-expanded={open}
|
|
151
|
-
aria-controls={popupId}
|
|
152
|
-
aria-describedby={inputError || mutationError ? errorId : undefined}
|
|
153
|
-
onFocus={() => setOpen(true)}
|
|
154
|
-
onChange={(event) => {
|
|
155
|
-
setDraftValue(event.target.value);
|
|
156
|
-
setInputError("");
|
|
157
|
-
setOpen(true);
|
|
158
|
-
setActiveIndex(-1);
|
|
159
|
-
}}
|
|
160
|
-
onKeyDown={handleKeyDown}
|
|
161
|
-
className="w-full"
|
|
162
|
-
placeholder="Action (required)"
|
|
163
|
-
/>
|
|
164
|
-
{open ? (
|
|
165
|
-
<div
|
|
166
|
-
id={popupId}
|
|
167
|
-
role="dialog"
|
|
168
|
-
aria-label="Guard action catalog"
|
|
169
|
-
className="absolute z-20 mt-1 max-h-60 w-full overflow-auto rounded-lg border bg-popover p-1 text-popover-foreground shadow-md"
|
|
67
|
+
<div className="grid gap-2">
|
|
68
|
+
<Label htmlFor="policy-guard-action">{t("Rule action")}</Label>
|
|
69
|
+
<div className="flex flex-wrap gap-2">
|
|
70
|
+
<Select
|
|
71
|
+
value={value}
|
|
72
|
+
disabled={disabled || !choices.length}
|
|
73
|
+
onValueChange={(action) => {
|
|
74
|
+
onChange(action);
|
|
75
|
+
if (
|
|
76
|
+
!choices.some(
|
|
77
|
+
(option) => option.actionKey === action && option.persisted,
|
|
78
|
+
)
|
|
79
|
+
)
|
|
80
|
+
void onCreate(action);
|
|
81
|
+
}}
|
|
170
82
|
>
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
83
|
+
<SelectTrigger
|
|
84
|
+
ref={selectRef}
|
|
85
|
+
id="policy-guard-action"
|
|
86
|
+
className="min-w-40 flex-1"
|
|
87
|
+
>
|
|
88
|
+
<SelectValue placeholder={t("Select an action")} />
|
|
89
|
+
</SelectTrigger>
|
|
90
|
+
<SelectContent>
|
|
91
|
+
{choices.map((o) => (
|
|
92
|
+
<SelectItem key={o.actionKey} value={o.actionKey}>
|
|
93
|
+
{o.actionKey}
|
|
94
|
+
</SelectItem>
|
|
95
|
+
))}
|
|
96
|
+
</SelectContent>
|
|
97
|
+
</Select>
|
|
98
|
+
{!disabled && !creating && (
|
|
99
|
+
<Button
|
|
100
|
+
ref={newActionRef}
|
|
101
|
+
variant="outline"
|
|
102
|
+
size="sm"
|
|
103
|
+
onClick={() => {
|
|
104
|
+
setCreating(true);
|
|
105
|
+
setError("");
|
|
106
|
+
}}
|
|
107
|
+
>
|
|
108
|
+
<Plus />
|
|
109
|
+
{t("New action")}
|
|
110
|
+
</Button>
|
|
111
|
+
)}
|
|
112
|
+
</div>
|
|
113
|
+
{creating && !disabled && (
|
|
114
|
+
<div className="grid gap-2 rounded-md border p-3">
|
|
115
|
+
<Label htmlFor="policy-new-action">{t("Reusable action name")}</Label>
|
|
116
|
+
<Input
|
|
117
|
+
id="policy-new-action"
|
|
118
|
+
autoFocus
|
|
119
|
+
value={name}
|
|
120
|
+
aria-invalid={Boolean(error)}
|
|
121
|
+
aria-describedby={error ? "policy-action-error" : undefined}
|
|
122
|
+
onChange={(e) => {
|
|
123
|
+
setName(e.target.value);
|
|
124
|
+
setError("");
|
|
125
|
+
}}
|
|
126
|
+
onKeyDown={(e) => {
|
|
127
|
+
if (e.key === "Enter") {
|
|
128
|
+
e.preventDefault();
|
|
129
|
+
add();
|
|
130
|
+
} else if (e.key === "Escape") {
|
|
131
|
+
cancelCreation();
|
|
132
|
+
}
|
|
133
|
+
}}
|
|
134
|
+
/>
|
|
135
|
+
{error && (
|
|
136
|
+
<p
|
|
137
|
+
id="policy-action-error"
|
|
138
|
+
role="alert"
|
|
139
|
+
className="text-sm text-destructive"
|
|
225
140
|
>
|
|
226
|
-
|
|
227
|
-
</button>
|
|
228
|
-
) : null}
|
|
229
|
-
{filteredOptions.length === 0 && !showCreate ? (
|
|
230
|
-
<p className="px-3 py-2 text-sm text-muted-foreground">
|
|
231
|
-
No matching actions
|
|
141
|
+
{t(error)}
|
|
232
142
|
</p>
|
|
233
|
-
)
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
<span>{inputError || mutationError}</span>
|
|
243
|
-
{mutationError && onRetry ? (
|
|
244
|
-
<button
|
|
245
|
-
type="button"
|
|
246
|
-
onClick={() => void onRetry()}
|
|
247
|
-
className="underline"
|
|
248
|
-
>
|
|
249
|
-
Retry
|
|
250
|
-
</button>
|
|
251
|
-
) : null}
|
|
143
|
+
)}
|
|
144
|
+
<div className="flex gap-2">
|
|
145
|
+
<Button size="sm" onClick={add}>
|
|
146
|
+
{t("Use action")}
|
|
147
|
+
</Button>
|
|
148
|
+
<Button size="sm" variant="ghost" onClick={cancelCreation}>
|
|
149
|
+
{t("Cancel")}
|
|
150
|
+
</Button>
|
|
151
|
+
</div>
|
|
252
152
|
</div>
|
|
253
|
-
)
|
|
153
|
+
)}
|
|
254
154
|
</div>
|
|
255
155
|
);
|
|
256
156
|
}
|
|
@@ -31,9 +31,7 @@ export function mergeGuardActionOptions(
|
|
|
31
31
|
actionKey: item.actionKey,
|
|
32
32
|
persisted: true,
|
|
33
33
|
}))
|
|
34
|
-
.sort((left, right) =>
|
|
35
|
-
left.actionKey.localeCompare(right.actionKey, "en"),
|
|
36
|
-
);
|
|
34
|
+
.sort((left, right) => left.actionKey.localeCompare(right.actionKey, "en"));
|
|
37
35
|
const exactValues = new Set(options.map((option) => option.actionKey));
|
|
38
36
|
|
|
39
37
|
for (const actionKey of currentPolicyValues) {
|
|
@@ -47,10 +45,10 @@ export function mergeGuardActionOptions(
|
|
|
47
45
|
return options;
|
|
48
46
|
}
|
|
49
47
|
|
|
50
|
-
export function filterGuardActionOptions(
|
|
51
|
-
options:
|
|
48
|
+
export function filterGuardActionOptions<T extends { actionKey: string }>(
|
|
49
|
+
options: T[],
|
|
52
50
|
query: string,
|
|
53
|
-
):
|
|
51
|
+
): T[] {
|
|
54
52
|
const normalizedQuery = query.toLocaleLowerCase();
|
|
55
53
|
return options.filter((option) =>
|
|
56
54
|
option.actionKey.toLocaleLowerCase().includes(normalizedQuery),
|