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.
Files changed (191) hide show
  1. package/README.md +4 -2
  2. package/dist/template/Dockerfile +4 -6
  3. package/dist/template/README.md +5 -10
  4. package/dist/template/app/api/admin/device-favorites/route.test.ts +125 -0
  5. package/dist/template/app/api/admin/device-favorites/route.ts +91 -0
  6. package/dist/template/app/api/admin/distribution-history/restore/route.ts +39 -0
  7. package/dist/template/app/api/admin/distribution-history/route.test.ts +55 -0
  8. package/dist/template/app/api/admin/distribution-history/route.ts +24 -0
  9. package/dist/template/app/api/admin/updates/[encodedKey]/deactivate/route.ts +2 -1
  10. package/dist/template/app/api/admin/updates/[encodedKey]/promote/route.ts +2 -1
  11. package/dist/template/app/api/admin/updates/[encodedKey]/rollback/route.ts +2 -1
  12. package/dist/template/app/api/admin/updates/[encodedKey]/route.ts +2 -1
  13. package/dist/template/app/api/admin/updates/route.test.ts +21 -0
  14. package/dist/template/app/api/admin/updates/route.ts +12 -1
  15. package/dist/template/app/globals.css +65 -68
  16. package/dist/template/app/login/page.tsx +66 -42
  17. package/dist/template/app/updates/[encodedKey]/page.tsx +382 -248
  18. package/dist/template/app/updates/devices/page.tsx +187 -145
  19. package/dist/template/app/updates/embedded/page.tsx +80 -34
  20. package/dist/template/app/updates/emergency-redirects/page.tsx +138 -71
  21. package/dist/template/app/updates/layout.tsx +2 -1
  22. package/dist/template/app/updates/page.tsx +385 -274
  23. package/dist/template/app/updates/releases.test.tsx +156 -0
  24. package/dist/template/components/admin/admin-role.ts +5 -0
  25. package/dist/template/components/admin/admin-shell.tsx +166 -95
  26. package/dist/template/components/admin/confirm-action.tsx +123 -0
  27. package/dist/template/components/admin/language-switcher.tsx +27 -16
  28. package/dist/template/components/admin/release-badges.test.tsx +50 -0
  29. package/dist/template/components/admin/release-badges.tsx +101 -0
  30. package/dist/template/components/ui/alert.tsx +10 -3
  31. package/dist/template/components/ui/avatar.tsx +108 -0
  32. package/dist/template/components/ui/badge.tsx +2 -2
  33. package/dist/template/components/ui/breadcrumb.tsx +108 -0
  34. package/dist/template/components/ui/button.tsx +5 -5
  35. package/dist/template/components/ui/card.tsx +2 -2
  36. package/dist/template/components/ui/chart.tsx +1 -1
  37. package/dist/template/components/ui/collapsible.tsx +7 -0
  38. package/dist/template/components/ui/dialog.tsx +13 -4
  39. package/dist/template/components/ui/empty.tsx +103 -0
  40. package/dist/template/components/ui/field.tsx +248 -0
  41. package/dist/template/components/ui/input-group.tsx +170 -0
  42. package/dist/template/components/ui/input.tsx +1 -1
  43. package/dist/template/components/ui/native-select.tsx +1 -1
  44. package/dist/template/components/ui/radio-group.tsx +41 -0
  45. package/dist/template/components/ui/sheet.tsx +6 -2
  46. package/dist/template/components/ui/sidebar.tsx +62 -17
  47. package/dist/template/components/ui/status-tones.ts +15 -0
  48. package/dist/template/components/ui/table.tsx +1 -1
  49. package/dist/template/components/ui/textarea.tsx +1 -1
  50. package/dist/template/docs/admin-ui.md +22 -24
  51. package/dist/template/docs/device-targeting.md +6 -12
  52. package/dist/template/docs/doctor.md +2 -2
  53. package/dist/template/docs/expo-app-setup.md +9 -27
  54. package/dist/template/docs/getting-started.md +3 -4
  55. package/dist/template/docs/getting-started.ru.md +11 -544
  56. package/dist/template/docs/index.md +1 -1
  57. package/dist/template/docs/installation-telemetry.md +6 -12
  58. package/dist/template/docs/migrations/2026-10-16-manifest-identity.sql +5 -0
  59. package/dist/template/docs/migrations/2026-11-25-device-favorites.sql +53 -0
  60. package/dist/template/docs/migrations/2026-11-25-download-on-postpone.sql +5 -0
  61. package/dist/template/docs/migrations/2026-11-25-public-distribution-history.sql +283 -0
  62. package/dist/template/docs/migrations/schema.sql +349 -0
  63. package/dist/template/docs/npm-installer.md +3 -3
  64. package/dist/template/docs/operations-guide.md +9 -7
  65. package/dist/template/docs/ota-client-lifecycle.md +30 -68
  66. package/dist/template/docs/ota-emergency-recovery.md +34 -57
  67. package/dist/template/docs/ota-update-policy.md +9 -20
  68. package/dist/template/docs/ru/admin-ui.md +33 -0
  69. package/dist/template/docs/ru/device-targeting.md +59 -0
  70. package/dist/template/docs/ru/doctor.md +98 -0
  71. package/dist/template/docs/ru/expo-app-setup.md +497 -0
  72. package/dist/template/docs/ru/getting-started.md +391 -0
  73. package/dist/template/docs/ru/index.md +50 -0
  74. package/dist/template/docs/ru/installation-telemetry.md +125 -0
  75. package/dist/template/docs/ru/npm-installer.md +71 -0
  76. package/dist/template/docs/ru/operations-guide.md +322 -0
  77. package/dist/template/docs/ru/ota-client-lifecycle.md +162 -0
  78. package/dist/template/docs/ru/ota-emergency-recovery.md +108 -0
  79. package/dist/template/docs/ru/ota-update-policy.md +69 -0
  80. package/dist/template/docs/ru/publishing-npm-installer.md +49 -0
  81. package/dist/template/docs/ru/supabase-setup.md +120 -0
  82. package/dist/template/docs/uk/admin-ui.md +33 -0
  83. package/dist/template/docs/uk/device-targeting.md +59 -0
  84. package/dist/template/docs/uk/doctor.md +98 -0
  85. package/dist/template/docs/uk/expo-app-setup.md +497 -0
  86. package/dist/template/docs/uk/getting-started.md +391 -0
  87. package/dist/template/docs/uk/index.md +50 -0
  88. package/dist/template/docs/uk/installation-telemetry.md +125 -0
  89. package/dist/template/docs/uk/npm-installer.md +71 -0
  90. package/dist/template/docs/uk/operations-guide.md +322 -0
  91. package/dist/template/docs/uk/ota-client-lifecycle.md +162 -0
  92. package/dist/template/docs/uk/ota-emergency-recovery.md +108 -0
  93. package/dist/template/docs/uk/ota-update-policy.md +69 -0
  94. package/dist/template/docs/uk/publishing-npm-installer.md +49 -0
  95. package/dist/template/docs/uk/supabase-setup.md +120 -0
  96. package/dist/template/lib/audience-ids.ts +4 -0
  97. package/dist/template/package-lock.json +46 -0
  98. package/dist/template/package.json +4 -1
  99. package/dist/template/scripts/configure-expo-app.mjs +3 -3
  100. package/dist/template/scripts/create-access-token.mjs +1 -1
  101. package/dist/template/scripts/lib/doctor.mjs +10 -7
  102. package/dist/template/scripts/lib/doctor.test.mjs +7 -5
  103. package/dist/template/scripts/lib/embedded-update-registrar.test.ts +22 -7
  104. package/dist/template/scripts/lib/expo-app-configurator.mjs +25 -22
  105. package/dist/template/scripts/lib/expo-app-configurator.test.ts +37 -2
  106. package/dist/template/scripts/lib/ota-client-configurator.mjs +152 -55
  107. package/dist/template/scripts/lib/ota-client-configurator.test.ts +60 -20
  108. package/dist/template/scripts/lib/ota-client-package.mjs +25 -0
  109. package/dist/template/scripts/lib/ota-root-hook-patcher.mjs +90 -32
  110. package/dist/template/scripts/lib/ota-root-hook-patcher.test.ts +49 -6
  111. package/dist/template/scripts/tests/device-analytics-migrations.mjs +12 -1
  112. package/dist/template/scripts/tests/device-targeting-activation.mjs +2 -3
  113. package/dist/template/scripts/tests/e2e/client-lifecycle.mjs +161 -0
  114. package/dist/template/scripts/tests/e2e/client-lifecycle.scenario.jsx +465 -0
  115. package/dist/template/scripts/tests/e2e/client-vitest.mts +18 -0
  116. package/dist/template/scripts/tests/e2e/console.mjs +86 -0
  117. package/dist/template/scripts/tests/e2e/delivery.mjs +296 -0
  118. package/dist/template/scripts/tests/e2e/device-favorites.mjs +309 -0
  119. package/dist/template/scripts/tests/e2e/installations.mjs +132 -0
  120. package/dist/template/scripts/tests/e2e/policy.mjs +253 -0
  121. package/dist/template/scripts/tests/e2e/registrar-token.mjs +59 -0
  122. package/dist/template/scripts/tests/e2e/release-matrix.mjs +307 -0
  123. package/dist/template/scripts/tests/e2e/releases.mjs +162 -0
  124. package/dist/template/scripts/tests/e2e/run.mjs +224 -0
  125. package/dist/template/scripts/tests/manifest-identity.sql +7 -5
  126. package/dist/template/scripts/tests/public-distribution-history.mjs +102 -0
  127. package/dist/template/src/client/admin-api.ts +45 -10
  128. package/dist/template/src/client/admin-types.ts +35 -1
  129. package/dist/template/src/client/devices/analytics.tsx +169 -219
  130. package/dist/template/src/client/devices/cohorts.tsx +181 -0
  131. package/dist/template/src/client/devices/device-name.tsx +37 -21
  132. package/dist/template/src/client/devices/favorite-targets.tsx +381 -0
  133. package/dist/template/src/client/devices/registry.tsx +265 -233
  134. package/dist/template/src/client/devices/use-devices-report.test.tsx +98 -0
  135. package/dist/template/src/client/devices/use-devices-report.ts +53 -82
  136. package/dist/template/src/client/devices/version-adoption-chart.tsx +48 -38
  137. package/dist/template/src/client/distribution-control.tsx +98 -53
  138. package/dist/template/src/client/distribution-history.tsx +353 -0
  139. package/dist/template/src/client/guard-action-catalog.tsx +183 -0
  140. package/dist/template/src/client/guard-action-combobox.test.ts +16 -22
  141. package/dist/template/src/client/guard-action-combobox.tsx +134 -234
  142. package/dist/template/src/client/guard-action-options.ts +4 -6
  143. package/dist/template/src/client/i18n/en.json +335 -2
  144. package/dist/template/src/client/i18n/language-switching.test.tsx +233 -0
  145. package/dist/template/src/client/i18n/messages.test.ts +102 -19
  146. package/dist/template/src/client/i18n/messages.ts +31 -16
  147. package/dist/template/src/client/i18n/provider.tsx +6 -3
  148. package/dist/template/src/client/i18n/ru.json +619 -0
  149. package/dist/template/src/client/i18n/uk.json +619 -0
  150. package/dist/template/src/client/i18n/use-formatters.ts +5 -0
  151. package/dist/template/src/client/logout-button.tsx +3 -3
  152. package/dist/template/src/client/policy-draft.ts +138 -0
  153. package/dist/template/src/client/update-audience-editor.tsx +597 -199
  154. package/dist/template/src/client/update-policy-editor.test.ts +101 -29
  155. package/dist/template/src/client/update-policy-editor.tsx +466 -307
  156. package/dist/template/src/client/use-filtered-results.ts +55 -0
  157. package/dist/template/src/server/controllers/manifest.test.ts +23 -3
  158. package/dist/template/src/server/controllers/manifest.ts +15 -1
  159. package/dist/template/src/server/devices/audience.ts +9 -5
  160. package/dist/template/src/server/lib/admin-audience-activation.test.ts +37 -90
  161. package/dist/template/src/server/lib/admin-release-history.test.ts +138 -0
  162. package/dist/template/src/server/lib/admin-updates.ts +160 -534
  163. package/dist/template/src/server/lib/distribution-history.ts +97 -0
  164. package/dist/template/src/server/lib/supabase.js +24 -0
  165. package/dist/template/src/server/lib/supabase.test.ts +38 -1
  166. package/dist/template/src/server/lib/update-policy.test.ts +20 -0
  167. package/dist/template/src/server/lib/update-policy.ts +23 -10
  168. package/dist/template/src/server/manifest/manifest-repository.ts +11 -3
  169. package/dist/template/src/server/manifest/manifest-values.test.ts +2 -0
  170. package/dist/template/src/server/manifest/manifest-values.ts +21 -13
  171. package/dist/template/templates/expo-app/scripts/ota-publish/api/database-client.mjs +7 -30
  172. package/dist/template/templates/expo-app/scripts/ota-register-embedded/index.mjs +2 -1
  173. package/dist/template/tsconfig.json +2 -2
  174. package/dist/template-manifest.json +571 -271
  175. package/package.json +1 -1
  176. package/dist/template/scripts/lib/device-identity.test.ts +0 -29
  177. package/dist/template/scripts/lib/ota-client.test.ts +0 -55
  178. package/dist/template/scripts/lib/ota-update-controller.test.ts +0 -232
  179. package/dist/template/scripts/lib/ota-updates-ui.test.tsx +0 -154
  180. package/dist/template/scripts/tests/ota-client-types.mjs +0 -69
  181. package/dist/template/templates/expo-app/ota-client/README.md +0 -73
  182. package/dist/template/templates/expo-app/ota-client/controller.ts +0 -107
  183. package/dist/template/templates/expo-app/ota-client/device-identity.ts +0 -48
  184. package/dist/template/templates/expo-app/ota-client/docs/update-lifecycle.md +0 -61
  185. package/dist/template/templates/expo-app/ota-client/index.ts +0 -16
  186. package/dist/template/templates/expo-app/ota-client/model/hooks/useOtaUpdates.ts +0 -97
  187. package/dist/template/templates/expo-app/ota-client/model/update-controller.ts +0 -293
  188. package/dist/template/templates/expo-app/ota-client/runtime.ts +0 -153
  189. package/dist/template/templates/expo-app/ota-client/types.ts +0 -85
  190. package/dist/template/templates/expo-app/ota-client/ui/ota-update-gate.tsx +0 -25
  191. 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
- const source = readFileSync(
5
- new URL("./guard-action-combobox.tsx", import.meta.url),
6
- "utf8",
7
- );
8
-
9
- describe("Guard action combobox semantics", () => {
10
- it("uses a named dialog popup instead of interactive listbox options", () => {
11
- expect(source).toContain('aria-haspopup="dialog"');
12
- expect(source).toContain('role="dialog"');
13
- expect(source).toContain('aria-label="Guard action catalog"');
14
- expect(source).not.toMatch(/role="(?:listbox|option)"/);
15
- });
16
-
17
- it("keeps selection and catalog deletion as sibling buttons", () => {
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 { KeyboardEvent, useId, useRef, useState } from "react";
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 GuardActionCombobox({
21
+ export default function GuardActionPicker({
25
22
  value,
26
23
  options,
27
- selectionDisabled,
28
- canMutateCatalog,
29
- mutationError,
24
+ disabled,
30
25
  onChange,
31
26
  onCreate,
32
- onDelete,
33
- onRetry,
34
- }: Props) {
35
- const generatedId = useId();
36
- const popupId = `${generatedId}-popup`;
37
- const errorId = `${generatedId}-error`;
38
- const rootRef = useRef<HTMLDivElement>(null);
39
- const [draftValue, setDraftValue] = useState<string | null>(null);
40
- const [open, setOpen] = useState(false);
41
- const [activeIndex, setActiveIndex] = useState(-1);
42
- const [inputError, setInputError] = useState("");
43
- const inputValue = draftValue ?? value;
44
- const filteredOptions = selectionDisabled
45
- ? options
46
- : filterGuardActionOptions(options, inputValue);
47
-
48
- let creatableValue: string | null = null;
49
- try {
50
- creatableValue = normalizeCreatableGuardAction(inputValue);
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
- const exactOption = creatableValue
55
- ? options.find((option) => option.actionKey === creatableValue)
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
- actionKey = normalizeCreatableGuardAction(inputValue);
79
- } catch (error: unknown) {
80
- setInputError((error as Error).message);
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
- choose(actionKey);
85
- await onCreate(actionKey);
86
- };
87
-
88
- const activateRow = (index: number) => {
89
- if (index < filteredOptions.length) {
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
- ref={rootRef}
133
- className="relative min-w-56 flex-1"
134
- onBlur={(event) => {
135
- if (!rootRef.current?.contains(event.relatedTarget)) {
136
- setOpen(false);
137
- setDraftValue(null);
138
- }
139
- }}
140
- >
141
- <Input
142
- value={inputValue}
143
- maxLength={100}
144
- readOnly={selectionDisabled}
145
- aria-disabled={selectionDisabled}
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
- {filteredOptions.map((option, index) => (
172
- <div
173
- key={`${option.id || "policy"}:${option.actionKey}`}
174
- className={`flex items-center rounded ${
175
- activeIndex === index ? "bg-accent" : ""
176
- }`}
177
- >
178
- <button
179
- type="button"
180
- tabIndex={-1}
181
- disabled={selectionDisabled}
182
- aria-current={option.actionKey === value ? "true" : undefined}
183
- onMouseDown={(event) => event.preventDefault()}
184
- onClick={() => chooseOption(option)}
185
- className="min-w-0 flex-1 truncate px-3 py-2 text-left text-sm disabled:cursor-default"
186
- title={option.actionKey}
187
- >
188
- {option.actionKey}
189
- </button>
190
- {option.persisted && canMutateCatalog ? (
191
- <button
192
- type="button"
193
- aria-label={`Delete ${option.actionKey} from action catalog`}
194
- title="Delete from catalog"
195
- onMouseDown={(event) => event.preventDefault()}
196
- onClick={(event) => {
197
- event.stopPropagation();
198
- void onDelete(option);
199
- }}
200
- className="m-1 rounded p-2 text-destructive hover:bg-destructive/10"
201
- >
202
- <svg
203
- viewBox="0 0 24 24"
204
- width="16"
205
- height="16"
206
- fill="none"
207
- stroke="currentColor"
208
- strokeWidth="2"
209
- aria-hidden="true"
210
- >
211
- <path d="M3 6h18M8 6V4h8v2m-9 0 1 14h8l1-14M10 10v6m4-6v6" />
212
- </svg>
213
- </button>
214
- ) : null}
215
- </div>
216
- ))}
217
- {showCreate ? (
218
- <button
219
- type="button"
220
- onMouseDown={(event) => event.preventDefault()}
221
- onClick={() => void create()}
222
- className={`w-full rounded px-3 py-2 text-left text-sm font-medium text-primary ${
223
- activeIndex === filteredOptions.length ? "bg-accent" : ""
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
- Create “{creatableValue}”
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
- ) : null}
234
- </div>
235
- ) : null}
236
- {inputError || mutationError ? (
237
- <div
238
- id={errorId}
239
- role="status"
240
- className="mt-1 flex items-center gap-2 text-xs text-amber-700 dark:text-amber-300"
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
- ) : null}
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: GuardActionOption[],
48
+ export function filterGuardActionOptions<T extends { actionKey: string }>(
49
+ options: T[],
52
50
  query: string,
53
- ): GuardActionOption[] {
51
+ ): T[] {
54
52
  const normalizedQuery = query.toLocaleLowerCase();
55
53
  return options.filter((option) =>
56
54
  option.actionKey.toLocaleLowerCase().includes(normalizedQuery),