@constructive-io/schema-builder 0.2.0 → 0.4.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 (130) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/README.md +12 -4
  3. package/dist/card-widths-CmIxkRfh.cjs +28 -0
  4. package/dist/card-widths-CmIxkRfh.cjs.map +1 -0
  5. package/dist/card-widths-pI4Kw4Xz.js +22 -0
  6. package/dist/card-widths-pI4Kw4Xz.js.map +1 -0
  7. package/dist/core-CVxnNLs7.cjs +66 -0
  8. package/dist/core-CVxnNLs7.cjs.map +1 -0
  9. package/dist/core-DJ5kA4r-.js +49 -0
  10. package/dist/core-DJ5kA4r-.js.map +1 -0
  11. package/dist/core.cjs +14 -212
  12. package/dist/core.d.cts +3 -54
  13. package/dist/core.d.ts +3 -54
  14. package/dist/core.js +3 -200
  15. package/dist/create-table-card-B6SMrt_g.js +7691 -0
  16. package/dist/create-table-card-B6SMrt_g.js.map +1 -0
  17. package/dist/create-table-card-BrQ7pB_U.cjs +7732 -0
  18. package/dist/create-table-card-BrQ7pB_U.cjs.map +1 -0
  19. package/dist/delete-confirm-dialog-Ch4tWj6B.cjs +42 -0
  20. package/dist/delete-confirm-dialog-Ch4tWj6B.cjs.map +1 -0
  21. package/dist/delete-confirm-dialog-K0AWE643.js +37 -0
  22. package/dist/delete-confirm-dialog-K0AWE643.js.map +1 -0
  23. package/dist/empty-state-card-5t7LygTB.cjs +62 -0
  24. package/dist/empty-state-card-5t7LygTB.cjs.map +1 -0
  25. package/dist/empty-state-card-C-Nylatg.js +57 -0
  26. package/dist/empty-state-card-C-Nylatg.js.map +1 -0
  27. package/dist/fields.cjs +8 -7763
  28. package/dist/fields.d.cts +38 -39
  29. package/dist/fields.d.ts +38 -39
  30. package/dist/fields.js +2 -7735
  31. package/dist/generated-types-DZMUyNBH.d.cts +27 -0
  32. package/dist/generated-types-DZMUyNBH.d.ts +27 -0
  33. package/dist/index--9KTz1LB.d.cts +56 -0
  34. package/dist/index-10idAfdb.d.cts +7 -0
  35. package/dist/index-BcdFaKFS.d.ts +7 -0
  36. package/dist/index-euaSKMQK.d.ts +56 -0
  37. package/dist/index.cjs +392 -13730
  38. package/dist/index.cjs.map +1 -1
  39. package/dist/index.d.cts +4 -10
  40. package/dist/index.d.ts +4 -10
  41. package/dist/index.js +381 -13698
  42. package/dist/index.js.map +1 -1
  43. package/dist/indexes-view-CBzYpKcd.js +736 -0
  44. package/dist/indexes-view-CBzYpKcd.js.map +1 -0
  45. package/dist/indexes-view-D1K24ts1.cjs +759 -0
  46. package/dist/indexes-view-D1K24ts1.cjs.map +1 -0
  47. package/dist/indexes.cjs +6 -1041
  48. package/dist/indexes.d.cts +27 -27
  49. package/dist/indexes.d.ts +27 -27
  50. package/dist/indexes.js +2 -1016
  51. package/dist/invalidate-database-entities-BFCWf5Zf.js +2561 -0
  52. package/dist/invalidate-database-entities-BFCWf5Zf.js.map +1 -0
  53. package/dist/invalidate-database-entities-Gs5k8A-M.cjs +2864 -0
  54. package/dist/invalidate-database-entities-Gs5k8A-M.cjs.map +1 -0
  55. package/dist/keyboard-activation-BQiV0scm.js +13 -0
  56. package/dist/keyboard-activation-BQiV0scm.js.map +1 -0
  57. package/dist/keyboard-activation-D-L0IU6Y.cjs +19 -0
  58. package/dist/keyboard-activation-D-L0IU6Y.cjs.map +1 -0
  59. package/dist/policies-view-CkvGpJBo.cjs +1023 -0
  60. package/dist/policies-view-CkvGpJBo.cjs.map +1 -0
  61. package/dist/policies-view-DTfAhX7G.js +1018 -0
  62. package/dist/policies-view-DTfAhX7G.js.map +1 -0
  63. package/dist/policies.cjs +10 -5939
  64. package/dist/policies.d.cts +55 -50
  65. package/dist/policies.d.ts +55 -50
  66. package/dist/policies.js +4 -5911
  67. package/dist/policy-config-form-EEcQ9ywL.js +2502 -0
  68. package/dist/policy-config-form-EEcQ9ywL.js.map +1 -0
  69. package/dist/policy-config-form-RP9010zj.cjs +2629 -0
  70. package/dist/policy-config-form-RP9010zj.cjs.map +1 -0
  71. package/dist/policy-types-D_sYWb-b.d.cts +81 -0
  72. package/dist/policy-types-D_sYWb-b.d.ts +81 -0
  73. package/dist/query-BGtzUV7Y.cjs +233 -0
  74. package/dist/query-BGtzUV7Y.cjs.map +1 -0
  75. package/dist/query-r6j8IjQB.js +192 -0
  76. package/dist/query-r6j8IjQB.js.map +1 -0
  77. package/dist/relationships-view-C884EvGj.js +2417 -0
  78. package/dist/relationships-view-C884EvGj.js.map +1 -0
  79. package/dist/relationships-view-DlBs-gCF.cjs +2452 -0
  80. package/dist/relationships-view-DlBs-gCF.cjs.map +1 -0
  81. package/dist/relationships.cjs +9 -4328
  82. package/dist/relationships.d.cts +92 -92
  83. package/dist/relationships.d.ts +92 -92
  84. package/dist/relationships.js +2 -4300
  85. package/dist/rolldown-runtime-D_qnwXdH.cjs +35 -0
  86. package/dist/schema-builder-sidebar-DAgK7ZTu.js +654 -0
  87. package/dist/schema-builder-sidebar-DAgK7ZTu.js.map +1 -0
  88. package/dist/schema-builder-sidebar-L3TcQeQr.cjs +671 -0
  89. package/dist/schema-builder-sidebar-L3TcQeQr.cjs.map +1 -0
  90. package/dist/table-editor-B-5i1M79.js +1906 -0
  91. package/dist/table-editor-B-5i1M79.js.map +1 -0
  92. package/dist/table-editor-BdLdVZfh.cjs +1935 -0
  93. package/dist/table-editor-BdLdVZfh.cjs.map +1 -0
  94. package/dist/tables.cjs +6 -5643
  95. package/dist/tables.d.cts +22 -21
  96. package/dist/tables.d.ts +22 -21
  97. package/dist/tables.js +3 -5618
  98. package/dist/testing.cjs +63 -62
  99. package/dist/testing.cjs.map +1 -1
  100. package/dist/testing.d.cts +4 -4
  101. package/dist/testing.d.ts +4 -4
  102. package/dist/testing.js +62 -61
  103. package/dist/testing.js.map +1 -1
  104. package/dist/types-CXNRA22N.d.cts +450 -0
  105. package/dist/types-CXNRA22N.d.ts +450 -0
  106. package/dist/use-update-table-DlLX9fdE.js +244 -0
  107. package/dist/use-update-table-DlLX9fdE.js.map +1 -0
  108. package/dist/use-update-table-vcoHVolI.cjs +291 -0
  109. package/dist/use-update-table-vcoHVolI.cjs.map +1 -0
  110. package/package.json +8 -6
  111. package/dist/core.cjs.map +0 -1
  112. package/dist/core.js.map +0 -1
  113. package/dist/fields.cjs.map +0 -1
  114. package/dist/fields.js.map +0 -1
  115. package/dist/generated-types-CnAlqHU_.d.cts +0 -25
  116. package/dist/generated-types-CnAlqHU_.d.ts +0 -25
  117. package/dist/indexes.cjs.map +0 -1
  118. package/dist/indexes.js.map +0 -1
  119. package/dist/policies.cjs.map +0 -1
  120. package/dist/policies.js.map +0 -1
  121. package/dist/policy-types-CEYAR5TN.d.cts +0 -79
  122. package/dist/policy-types-CEYAR5TN.d.ts +0 -79
  123. package/dist/relationships.cjs.map +0 -1
  124. package/dist/relationships.js.map +0 -1
  125. package/dist/tables.cjs.map +0 -1
  126. package/dist/tables.js.map +0 -1
  127. package/dist/types-Du86atZ8.d.cts +0 -199
  128. package/dist/types-Du86atZ8.d.ts +0 -199
  129. package/dist/types-bX2dCrdl.d.cts +0 -52
  130. package/dist/types-bX2dCrdl.d.ts +0 -52
@@ -0,0 +1,1018 @@
1
+ 'use client';
2
+ import { D as useDeletePolicyMutation, F as useUpdatePolicyMutation, Y as cn, h as useSchemaBuilderRuntime, l as useSchemaBuilderSelectors, n as databasePoliciesQueryKeys, r as useDatabasePolicies } from "./invalidate-database-entities-BFCWf5Zf.js";
3
+ import { _ as injectSchemaFields, d as OPERATION_STYLES, f as PRIVILEGE_TO_OPERATION, o as usePolicyType, p as POLICY_TYPE_UI_CONFIG, s as usePolicyTypes, t as PolicyConfigForm, u as CRUD_OPERATIONS } from "./policy-config-form-EEcQ9ywL.js";
4
+ import { t as CARD_WIDTHS } from "./card-widths-pI4Kw4Xz.js";
5
+ import { t as EmptyStateCard } from "./empty-state-card-C-Nylatg.js";
6
+ import { a as mapConditionNodeToAst, i as PolicyDiagramByKey, o as AST_NODE_TO_POLICY_TYPE, r as CompositePolicyBuilder, s as fromBackendPolicyTypeId, t as CreateTableCard } from "./create-table-card-B6SMrt_g.js";
7
+ import { useCardStack } from "@constructive-io/ui/stack";
8
+ import { useCallback, useEffect, useMemo, useState } from "react";
9
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
10
+ import { ChevronDown, Eye, Loader2Icon, Lock, Pencil, Plus, PlusCircle, Search, Shield, ShieldCheck, Table2, Trash2 } from "lucide-react";
11
+ import { useQueryClient } from "@tanstack/react-query";
12
+ import { Button } from "@constructive-io/ui/button";
13
+ import { showErrorToast, showSuccessToast } from "@constructive-io/ui/toast";
14
+ import { ScrollArea } from "@constructive-io/ui/scroll-area";
15
+ import { ResponsiveDiagram } from "@constructive-io/ui/responsive-diagram";
16
+ import { Switch } from "@constructive-io/ui/switch";
17
+ import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@constructive-io/ui/collapsible";
18
+
19
+ //#region src/schema/schema-builder-core/components/policies/policy-diagram/diagram-themes.ts
20
+ const AMBIENT_LIGHT = {
21
+ bg: "#ffffff",
22
+ text: "#4B5563",
23
+ muted: "#9CA3AF"
24
+ };
25
+ const AMBIENT_DARK = {
26
+ bg: "#1e293b",
27
+ text: "#D1D5DB",
28
+ muted: "#6B7280"
29
+ };
30
+ /**
31
+ * Full theme definitions for each policy type (light + dark).
32
+ */
33
+ const DIAGRAM_THEMES = {
34
+ AuthzDirectOwner: {
35
+ light: {
36
+ primary: "#22C55E",
37
+ fill: "#DCFCE7",
38
+ border: "#86EFAC",
39
+ connector: "#22C55E",
40
+ connectorLight: "#BBF7D0",
41
+ ...AMBIENT_LIGHT
42
+ },
43
+ dark: {
44
+ primary: "#4ADE80",
45
+ fill: "#052E16",
46
+ border: "#166534",
47
+ connector: "#4ADE80",
48
+ connectorLight: "#15803D",
49
+ ...AMBIENT_DARK
50
+ }
51
+ },
52
+ AuthzDirectOwnerAny: {
53
+ light: {
54
+ primary: "#F97316",
55
+ fill: "#FFEDD5",
56
+ border: "#FDBA74",
57
+ connector: "#F97316",
58
+ connectorLight: "#FED7AA",
59
+ ...AMBIENT_LIGHT
60
+ },
61
+ dark: {
62
+ primary: "#FB923C",
63
+ fill: "#431407",
64
+ border: "#9A3412",
65
+ connector: "#FB923C",
66
+ connectorLight: "#C2410C",
67
+ ...AMBIENT_DARK
68
+ }
69
+ },
70
+ AuthzEntityMembership: {
71
+ light: {
72
+ primary: "#6366F1",
73
+ fill: "#E0E7FF",
74
+ border: "#A5B4FC",
75
+ connector: "#6366F1",
76
+ connectorLight: "#C7D2FE",
77
+ ...AMBIENT_LIGHT
78
+ },
79
+ dark: {
80
+ primary: "#818CF8",
81
+ fill: "#1E1B4B",
82
+ border: "#3730A3",
83
+ connector: "#818CF8",
84
+ connectorLight: "#4F46E5",
85
+ ...AMBIENT_DARK
86
+ }
87
+ },
88
+ AuthzAppMembership: {
89
+ light: {
90
+ primary: "#14B8A6",
91
+ fill: "#CCFBF1",
92
+ border: "#5EEAD4",
93
+ connector: "#14B8A6",
94
+ connectorLight: "#99F6E4",
95
+ ...AMBIENT_LIGHT
96
+ },
97
+ dark: {
98
+ primary: "#2DD4BF",
99
+ fill: "#042F2E",
100
+ border: "#115E59",
101
+ connector: "#2DD4BF",
102
+ connectorLight: "#0D9488",
103
+ ...AMBIENT_DARK
104
+ }
105
+ },
106
+ AuthzRelatedMemberList: {
107
+ light: {
108
+ primary: "#3B82F6",
109
+ fill: "#DBEAFE",
110
+ border: "#93C5FD",
111
+ connector: "#3B82F6",
112
+ connectorLight: "#BFDBFE",
113
+ ...AMBIENT_LIGHT
114
+ },
115
+ dark: {
116
+ primary: "#60A5FA",
117
+ fill: "#172554",
118
+ border: "#1E40AF",
119
+ connector: "#60A5FA",
120
+ connectorLight: "#2563EB",
121
+ ...AMBIENT_DARK
122
+ }
123
+ },
124
+ AuthzRelatedEntityMembership: {
125
+ light: {
126
+ primary: "#8B5CF6",
127
+ fill: "#EDE9FE",
128
+ border: "#C4B5FD",
129
+ connector: "#8B5CF6",
130
+ connectorLight: "#DDD6FE",
131
+ ...AMBIENT_LIGHT
132
+ },
133
+ dark: {
134
+ primary: "#A78BFA",
135
+ fill: "#2E1065",
136
+ border: "#5B21B6",
137
+ connector: "#A78BFA",
138
+ connectorLight: "#7C3AED",
139
+ ...AMBIENT_DARK
140
+ }
141
+ },
142
+ AuthzAllowAll: {
143
+ light: {
144
+ primary: "#10B981",
145
+ fill: "#D1FAE5",
146
+ border: "#6EE7B7",
147
+ connector: "#10B981",
148
+ connectorLight: "#A7F3D0",
149
+ ...AMBIENT_LIGHT
150
+ },
151
+ dark: {
152
+ primary: "#34D399",
153
+ fill: "#022C22",
154
+ border: "#065F46",
155
+ connector: "#34D399",
156
+ connectorLight: "#047857",
157
+ ...AMBIENT_DARK
158
+ }
159
+ },
160
+ AuthzDenyAll: {
161
+ light: {
162
+ primary: "#EF4444",
163
+ fill: "#FEE2E2",
164
+ border: "#FCA5A5",
165
+ connector: "#EF4444",
166
+ connectorLight: "#FECACA",
167
+ ...AMBIENT_LIGHT
168
+ },
169
+ dark: {
170
+ primary: "#F87171",
171
+ fill: "#450A0A",
172
+ border: "#991B1B",
173
+ connector: "#F87171",
174
+ connectorLight: "#B91C1C",
175
+ ...AMBIENT_DARK
176
+ }
177
+ },
178
+ AuthzPublishable: {
179
+ light: {
180
+ primary: "#8B5CF6",
181
+ fill: "#EDE9FE",
182
+ border: "#C4B5FD",
183
+ connector: "#8B5CF6",
184
+ connectorLight: "#DDD6FE",
185
+ ...AMBIENT_LIGHT
186
+ },
187
+ dark: {
188
+ primary: "#A78BFA",
189
+ fill: "#2E1065",
190
+ border: "#5B21B6",
191
+ connector: "#A78BFA",
192
+ connectorLight: "#7C3AED",
193
+ ...AMBIENT_DARK
194
+ }
195
+ },
196
+ AuthzMemberList: {
197
+ light: {
198
+ primary: "#0EA5E9",
199
+ fill: "#E0F2FE",
200
+ border: "#7DD3FC",
201
+ connector: "#0EA5E9",
202
+ connectorLight: "#BAE6FD",
203
+ ...AMBIENT_LIGHT
204
+ },
205
+ dark: {
206
+ primary: "#38BDF8",
207
+ fill: "#082F49",
208
+ border: "#075985",
209
+ connector: "#38BDF8",
210
+ connectorLight: "#0369A1",
211
+ ...AMBIENT_DARK
212
+ }
213
+ },
214
+ AuthzOrgHierarchy: {
215
+ light: {
216
+ primary: "#F59E0B",
217
+ fill: "#FEF3C7",
218
+ border: "#FCD34D",
219
+ connector: "#F59E0B",
220
+ connectorLight: "#FDE68A",
221
+ ...AMBIENT_LIGHT
222
+ },
223
+ dark: {
224
+ primary: "#FBBF24",
225
+ fill: "#451A03",
226
+ border: "#92400E",
227
+ connector: "#FBBF24",
228
+ connectorLight: "#B45309",
229
+ ...AMBIENT_DARK
230
+ }
231
+ },
232
+ AuthzTemporal: {
233
+ light: {
234
+ primary: "#EC4899",
235
+ fill: "#FCE7F3",
236
+ border: "#F9A8D4",
237
+ connector: "#EC4899",
238
+ connectorLight: "#FBCFE8",
239
+ ...AMBIENT_LIGHT
240
+ },
241
+ dark: {
242
+ primary: "#F472B6",
243
+ fill: "#500724",
244
+ border: "#9D174D",
245
+ connector: "#F472B6",
246
+ connectorLight: "#BE185D",
247
+ ...AMBIENT_DARK
248
+ }
249
+ },
250
+ AuthzComposite: {
251
+ light: {
252
+ primary: "#7C3AED",
253
+ fill: "#EDE9FE",
254
+ border: "#A78BFA",
255
+ connector: "#7C3AED",
256
+ connectorLight: "#DDD6FE",
257
+ ...AMBIENT_LIGHT
258
+ },
259
+ dark: {
260
+ primary: "#A78BFA",
261
+ fill: "#2E1065",
262
+ border: "#5B21B6",
263
+ connector: "#A78BFA",
264
+ connectorLight: "#7C3AED",
265
+ ...AMBIENT_DARK
266
+ }
267
+ }
268
+ };
269
+ /**
270
+ * Get resolved theme for a policy type in a given mode.
271
+ */
272
+ function getDiagramTheme(key, mode = "light") {
273
+ return DIAGRAM_THEMES[key]?.[mode] ?? DIAGRAM_THEMES.AuthzAllowAll[mode];
274
+ }
275
+
276
+ //#endregion
277
+ //#region src/schema/schema-builder-core/lib/policies/rls-parser.ts
278
+ var IdGenerator = class {
279
+ counter = 1;
280
+ next() {
281
+ return `c-${this.counter++}`;
282
+ }
283
+ };
284
+ var MembershipByFieldParser = class {
285
+ canParse(tag) {
286
+ return tag === "AuthzEntityMembership";
287
+ }
288
+ parse(node, tag, idGen) {
289
+ const data = node[tag];
290
+ const policyTypeId = fromBackendPolicyTypeId("AuthzEntityMembership", data) ?? "OwnedRecords";
291
+ return {
292
+ id: idGen.next(),
293
+ type: "condition",
294
+ data: {
295
+ policyType: policyTypeId,
296
+ data: { ...data }
297
+ }
298
+ };
299
+ }
300
+ };
301
+ var RlsNodeParser = class {
302
+ canParse(tag) {
303
+ return tag in AST_NODE_TO_POLICY_TYPE;
304
+ }
305
+ parse(node, tag, idGen) {
306
+ const policyTypeId = AST_NODE_TO_POLICY_TYPE[tag];
307
+ const data = node[tag];
308
+ return {
309
+ id: idGen.next(),
310
+ type: "condition",
311
+ data: {
312
+ policyType: policyTypeId,
313
+ data: { ...data }
314
+ }
315
+ };
316
+ }
317
+ };
318
+ var BoolExprParser = class {
319
+ transformer;
320
+ constructor(transformer) {
321
+ this.transformer = transformer;
322
+ }
323
+ canParse(tag) {
324
+ return tag === "BoolExpr";
325
+ }
326
+ parse(node, _tag, idGen) {
327
+ const data = node["BoolExpr"];
328
+ const operator = data.boolop === "AND_EXPR" ? "AND" : "OR";
329
+ const args = data.args || [];
330
+ const children = [];
331
+ for (const arg of args) {
332
+ const child = this.transformer.transformNode(arg, idGen);
333
+ if (child) children.push(child);
334
+ }
335
+ if (children.length === 0) return null;
336
+ if (children.length === 1) return children[0];
337
+ return {
338
+ id: idGen.next(),
339
+ type: "group",
340
+ operator,
341
+ children
342
+ };
343
+ }
344
+ };
345
+ var AstToConditionTransformer = class {
346
+ parsers;
347
+ constructor() {
348
+ this.parsers = [
349
+ new MembershipByFieldParser(),
350
+ new RlsNodeParser(),
351
+ new BoolExprParser(this)
352
+ ];
353
+ }
354
+ getNodeTag(node) {
355
+ const keys = Object.keys(node);
356
+ if (keys.length === 1 && /^[A-Z]/.test(keys[0])) return keys[0];
357
+ return null;
358
+ }
359
+ transformNode(node, idGen) {
360
+ const tag = this.getNodeTag(node);
361
+ if (!tag) return null;
362
+ for (const parser of this.parsers) if (parser.canParse(tag)) return parser.parse(node, tag, idGen);
363
+ return null;
364
+ }
365
+ transform(ast) {
366
+ const idGen = new IdGenerator();
367
+ const result = this.transformNode(ast, idGen);
368
+ if (!result) return {
369
+ id: "root",
370
+ type: "group",
371
+ operator: "AND",
372
+ children: []
373
+ };
374
+ if (result.type === "group") return {
375
+ ...result,
376
+ id: "root"
377
+ };
378
+ return {
379
+ id: "root",
380
+ type: "group",
381
+ operator: "AND",
382
+ children: [result]
383
+ };
384
+ }
385
+ };
386
+ const transformer = new AstToConditionTransformer();
387
+ function mapAstToConditionNode(ast) {
388
+ return transformer.transform(ast);
389
+ }
390
+
391
+ //#endregion
392
+ //#region src/schema/schema-builder-policies/components/table-editor/policies/use-policy-edit-state.ts
393
+ function getInitialState(policy) {
394
+ return {
395
+ roleName: policy.granteeName ?? "authenticated",
396
+ isPermissive: policy.permissive ?? true,
397
+ isEnabled: !(policy.disabled ?? false),
398
+ policyData: policy.data ?? {}
399
+ };
400
+ }
401
+ /**
402
+ * Hook for managing policy edit form state and change detection
403
+ */
404
+ function usePolicyEditState(policy) {
405
+ const [state, setState] = useState(() => getInitialState(policy));
406
+ const updateState = useCallback((updates) => {
407
+ setState((prev) => ({
408
+ ...prev,
409
+ ...updates
410
+ }));
411
+ }, []);
412
+ const updatePolicyData = useCallback((updates) => {
413
+ setState((prev) => ({
414
+ ...prev,
415
+ policyData: {
416
+ ...prev.policyData,
417
+ ...updates
418
+ }
419
+ }));
420
+ }, []);
421
+ const reset = useCallback(() => {
422
+ setState(getInitialState(policy));
423
+ }, [policy]);
424
+ return {
425
+ state,
426
+ updateState,
427
+ updatePolicyData,
428
+ hasChanges: useMemo(() => {
429
+ const original = getInitialState(policy);
430
+ return state.roleName !== original.roleName || state.isPermissive !== original.isPermissive || state.isEnabled !== original.isEnabled || JSON.stringify(state.policyData) !== JSON.stringify(original.policyData);
431
+ }, [policy, state]),
432
+ reset
433
+ };
434
+ }
435
+
436
+ //#endregion
437
+ //#region src/schema/schema-builder-policies/components/table-editor/policies/policy-edit-card.tsx
438
+ const PRIVILEGE_CONFIG = {
439
+ SELECT: {
440
+ label: "Read",
441
+ bg: "bg-sky-500/10",
442
+ text: "text-sky-600 dark:text-sky-400",
443
+ icon: Search
444
+ },
445
+ INSERT: {
446
+ label: "Create",
447
+ bg: "bg-emerald-500/10",
448
+ text: "text-emerald-600 dark:text-emerald-400",
449
+ icon: Plus
450
+ },
451
+ UPDATE: {
452
+ label: "Update",
453
+ bg: "bg-amber-500/10",
454
+ text: "text-amber-600 dark:text-amber-400",
455
+ icon: Pencil
456
+ },
457
+ DELETE: {
458
+ label: "Delete",
459
+ bg: "bg-rose-500/10",
460
+ text: "text-rose-600 dark:text-rose-400",
461
+ icon: Trash2
462
+ }
463
+ };
464
+ /**
465
+ * Card component for editing an existing policy.
466
+ * Shows policy header, diagram, settings form, and access rule config.
467
+ */
468
+ const PolicyEditCard = ({ policy, tableName, onSuccess, card }) => {
469
+ const { currentDatabase } = useSchemaBuilderSelectors();
470
+ const schemaId = currentDatabase?.schemaId ?? "";
471
+ const { policyType, isLoading: isPolicyTypeLoading } = usePolicyType(policy.policyType ?? "");
472
+ const { policyTypes } = usePolicyTypes();
473
+ const updatePolicyMutation = useUpdatePolicyMutation({ selection: { fields: { id: true } } });
474
+ const deletePolicyMutation = useDeletePolicyMutation({ selection: { fields: { id: true } } });
475
+ const isCompositePolicy = policy.policyType === "AuthzComposite";
476
+ const { state, updateState, updatePolicyData, hasChanges } = usePolicyEditState(policy);
477
+ const [compositeData, setCompositeData] = useState(useMemo(() => {
478
+ if (!isCompositePolicy || !policy.data) return null;
479
+ try {
480
+ return mapAstToConditionNode(policy.data);
481
+ } catch {
482
+ return null;
483
+ }
484
+ }, [isCompositePolicy, policy.data]));
485
+ const [compositeDataChanged, setCompositeDataChanged] = useState(false);
486
+ const handleCompositeChange = (data) => {
487
+ setCompositeData(data);
488
+ setCompositeDataChanged(true);
489
+ };
490
+ const handleSubmit = async () => {
491
+ if (!policyType || !schemaId) return;
492
+ try {
493
+ const dataToSubmit = isCompositePolicy && compositeData ? mapConditionNodeToAst(compositeData) ?? {} : injectSchemaFields(state.policyData, schemaId, policyType.name);
494
+ await updatePolicyMutation.mutateAsync({
495
+ id: policy.id,
496
+ policyPatch: {
497
+ permissive: state.isPermissive,
498
+ disabled: !state.isEnabled,
499
+ data: dataToSubmit
500
+ }
501
+ });
502
+ showSuccessToast({
503
+ message: "Policy updated",
504
+ description: `Policy "${policy.name}" has been updated successfully.`
505
+ });
506
+ onSuccess?.();
507
+ card.close();
508
+ } catch (error) {
509
+ showErrorToast({
510
+ message: "Failed to update policy",
511
+ description: error instanceof Error ? error.message : "An unexpected error occurred."
512
+ });
513
+ }
514
+ };
515
+ const handleFormSubmit = (e) => {
516
+ e.preventDefault();
517
+ const canSubmit = isCompositePolicy ? compositeDataChanged || hasChanges : hasChanges;
518
+ if (!updatePolicyMutation.isPending && canSubmit) handleSubmit();
519
+ };
520
+ const handleDelete = async () => {
521
+ try {
522
+ await deletePolicyMutation.mutateAsync({ id: policy.id });
523
+ showSuccessToast({
524
+ message: "Policy deleted",
525
+ description: `Policy "${policy.name}" has been deleted.`
526
+ });
527
+ onSuccess?.();
528
+ card.close();
529
+ } catch (error) {
530
+ showErrorToast({
531
+ message: "Failed to delete policy",
532
+ description: error instanceof Error ? error.message : "An unexpected error occurred."
533
+ });
534
+ }
535
+ };
536
+ const hasAnyChanges = isCompositePolicy ? compositeDataChanged || hasChanges : hasChanges;
537
+ const isLoading = updatePolicyMutation.isPending || deletePolicyMutation.isPending;
538
+ if (isPolicyTypeLoading) return /* @__PURE__ */ jsx("div", {
539
+ className: "flex h-full items-center justify-center",
540
+ children: /* @__PURE__ */ jsx(Loader2Icon, { className: "text-muted-foreground h-6 w-6 animate-spin" })
541
+ });
542
+ if (!policyType) return /* @__PURE__ */ jsx("div", {
543
+ className: "flex h-full items-center justify-center p-6",
544
+ children: /* @__PURE__ */ jsx("p", {
545
+ className: "text-muted-foreground text-sm",
546
+ children: "Policy type not found"
547
+ })
548
+ });
549
+ const PolicyIcon = policyType.icon;
550
+ const privilege = policy.privilege ?? "SELECT";
551
+ const privilegeConfig = PRIVILEGE_CONFIG[privilege] ?? PRIVILEGE_CONFIG.SELECT;
552
+ const PrivilegeIcon = privilegeConfig.icon;
553
+ return /* @__PURE__ */ jsxs("form", {
554
+ onSubmit: handleFormSubmit,
555
+ "data-chat-component": "policy-edit-card",
556
+ "data-chat-policy-type": policy.policyType ?? "",
557
+ "data-chat-privilege": policy.privilege ?? "",
558
+ "data-chat-table-name": tableName,
559
+ "data-chat-enabled": String(state.isEnabled),
560
+ "data-chat-permissive": String(state.isPermissive),
561
+ className: "flex h-full flex-col",
562
+ children: [/* @__PURE__ */ jsx(ScrollArea, {
563
+ className: "min-h-0 flex-1",
564
+ children: /* @__PURE__ */ jsxs("div", {
565
+ className: "space-y-6 p-6",
566
+ children: [
567
+ /* @__PURE__ */ jsxs("div", {
568
+ className: "space-y-4",
569
+ children: [/* @__PURE__ */ jsxs("div", {
570
+ className: "flex items-start gap-3",
571
+ children: [/* @__PURE__ */ jsx("div", {
572
+ className: "bg-primary/10 flex size-10 shrink-0 items-center justify-center rounded-lg",
573
+ children: /* @__PURE__ */ jsx(PolicyIcon, { className: "text-primary size-5" })
574
+ }), /* @__PURE__ */ jsxs("div", {
575
+ className: "min-w-0 flex-1",
576
+ children: [/* @__PURE__ */ jsxs("div", {
577
+ className: "flex items-center gap-2",
578
+ children: [/* @__PURE__ */ jsx("h3", {
579
+ className: "text-balance font-semibold",
580
+ children: policyType.title
581
+ }), /* @__PURE__ */ jsxs("span", {
582
+ className: `inline-flex items-center gap-1 rounded-full px-2 py-0.5 text-xs font-medium
583
+ ${privilegeConfig.bg} ${privilegeConfig.text}`,
584
+ children: [/* @__PURE__ */ jsx(PrivilegeIcon, { className: "size-3" }), privilegeConfig.label]
585
+ })]
586
+ }), /* @__PURE__ */ jsx("p", {
587
+ className: "text-muted-foreground text-sm",
588
+ children: policyType.description
589
+ })]
590
+ })]
591
+ }), /* @__PURE__ */ jsx(ResponsiveDiagram, { children: /* @__PURE__ */ jsx(PolicyDiagramByKey, {
592
+ diagramKey: policyType.diagramKey ?? "AuthzAllowAll",
593
+ tableName,
594
+ config: state.policyData
595
+ }) })]
596
+ }),
597
+ /* @__PURE__ */ jsxs("div", {
598
+ className: "space-y-3",
599
+ children: [/* @__PURE__ */ jsx("p", {
600
+ className: "text-muted-foreground text-xs font-semibold tracking-wide uppercase",
601
+ children: "Settings"
602
+ }), /* @__PURE__ */ jsxs("div", {
603
+ className: "flex items-center justify-between",
604
+ children: [/* @__PURE__ */ jsx("span", {
605
+ className: "text-sm font-medium",
606
+ children: "Policy Status"
607
+ }), /* @__PURE__ */ jsxs("div", {
608
+ className: "flex items-center gap-2",
609
+ children: [/* @__PURE__ */ jsx(Switch, {
610
+ id: "policy-enabled",
611
+ checked: state.isEnabled,
612
+ onCheckedChange: (checked) => updateState({ isEnabled: checked }),
613
+ disabled: updatePolicyMutation.isPending
614
+ }), /* @__PURE__ */ jsx("span", {
615
+ className: "text-muted-foreground w-16 text-sm",
616
+ children: state.isEnabled ? "Enabled" : "Disabled"
617
+ })]
618
+ })]
619
+ })]
620
+ }),
621
+ /* @__PURE__ */ jsxs("div", {
622
+ className: "space-y-3",
623
+ children: [/* @__PURE__ */ jsx("p", {
624
+ className: "text-muted-foreground text-xs font-semibold tracking-wide uppercase",
625
+ children: "Access Rule Config"
626
+ }), isCompositePolicy ? /* @__PURE__ */ jsx(CompositePolicyBuilder, {
627
+ value: compositeData,
628
+ onChange: handleCompositeChange,
629
+ policyTypes,
630
+ disabled: updatePolicyMutation.isPending
631
+ }) : /* @__PURE__ */ jsx(PolicyConfigForm, {
632
+ policyType,
633
+ value: state.policyData,
634
+ onChange: updatePolicyData,
635
+ disabled: updatePolicyMutation.isPending,
636
+ hideAutoGeneratedInfo: true
637
+ })]
638
+ })
639
+ ]
640
+ })
641
+ }), /* @__PURE__ */ jsxs("div", {
642
+ className: "flex justify-between border-t px-4 py-3",
643
+ children: [/* @__PURE__ */ jsxs(Button, {
644
+ type: "button",
645
+ variant: "ghost",
646
+ onClick: handleDelete,
647
+ disabled: isLoading,
648
+ className: "text-destructive hover:text-destructive hover:bg-destructive/10",
649
+ children: [deletePolicyMutation.isPending ? /* @__PURE__ */ jsx(Loader2Icon, { className: "mr-1.5 size-4 animate-spin" }) : /* @__PURE__ */ jsx(Trash2, { className: "mr-1.5 size-4" }), deletePolicyMutation.isPending ? "Deleting..." : "Delete"]
650
+ }), /* @__PURE__ */ jsxs("div", {
651
+ className: "flex gap-2",
652
+ children: [/* @__PURE__ */ jsx(Button, {
653
+ type: "button",
654
+ variant: "outline",
655
+ onClick: () => card.close(),
656
+ disabled: isLoading,
657
+ children: "Cancel"
658
+ }), /* @__PURE__ */ jsxs(Button, {
659
+ type: "submit",
660
+ disabled: isLoading || !hasAnyChanges,
661
+ children: [updatePolicyMutation.isPending && /* @__PURE__ */ jsx(Loader2Icon, { className: "mr-2 h-4 w-4 animate-spin" }), updatePolicyMutation.isPending ? "Saving..." : "Save"]
662
+ })]
663
+ })]
664
+ })]
665
+ });
666
+ };
667
+
668
+ //#endregion
669
+ //#region src/schema/schema-builder-policies/components/table-editor/policies/policy-pill.tsx
670
+ function getPolicyThemeKey$1(policyType) {
671
+ if (!policyType) return "AuthzAllowAll";
672
+ return policyType in POLICY_TYPE_UI_CONFIG ? policyType : "AuthzAllowAll";
673
+ }
674
+ function PolicyPill({ policy, onClick }) {
675
+ const config = policy.policyType ? POLICY_TYPE_UI_CONFIG[policy.policyType] : null;
676
+ const title = config?.fallbackTitle ?? policy.policyType ?? policy.name ?? "Policy";
677
+ const isDisabled = policy.disabled === true;
678
+ const Icon = config?.icon ?? Shield;
679
+ const { colorMode: resolvedTheme } = useSchemaBuilderRuntime();
680
+ const mode = resolvedTheme === "dark" ? "dark" : "light";
681
+ const theme = getDiagramTheme(getPolicyThemeKey$1(policy.policyType), mode);
682
+ const lighterBg = theme.fill + "80";
683
+ return /* @__PURE__ */ jsxs("button", {
684
+ type: "button",
685
+ onClick,
686
+ className: cn("group inline-flex min-h-10 items-center gap-1 rounded-full px-3 py-1", "border text-xs font-semibold", `cursor-pointer transition-[scale,box-shadow] duration-150 ease-out
687
+ motion-safe:active:scale-[0.96]`, "hover:scale-[1.02] hover:shadow-sm", "focus-visible:ring-ring focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:outline-none", isDisabled && "opacity-60"),
688
+ style: {
689
+ backgroundColor: lighterBg,
690
+ borderColor: theme.border,
691
+ color: theme.primary
692
+ },
693
+ children: [
694
+ /* @__PURE__ */ jsx(Icon, {
695
+ className: "size-3 group-hover:hidden",
696
+ style: { color: theme.primary }
697
+ }),
698
+ /* @__PURE__ */ jsx(Pencil, {
699
+ className: "hidden size-3 group-hover:block",
700
+ style: { color: theme.primary }
701
+ }),
702
+ /* @__PURE__ */ jsx("span", { children: title })
703
+ ]
704
+ });
705
+ }
706
+
707
+ //#endregion
708
+ //#region src/schema/schema-builder-policies/components/table-editor/policies/policy-operation-group.tsx
709
+ const OPERATION_ICONS = {
710
+ Plus: PlusCircle,
711
+ Search: Eye,
712
+ Pencil,
713
+ Trash2
714
+ };
715
+ const OPERATION_DISPLAY_NAMES = {
716
+ read: "Read",
717
+ create: "Create",
718
+ update: "Update",
719
+ delete: "Delete"
720
+ };
721
+ function getDiagramKey(policyType) {
722
+ if (!policyType || !(policyType in POLICY_TYPE_UI_CONFIG)) return null;
723
+ return policyType;
724
+ }
725
+ function getPolicyThemeKey(policyType) {
726
+ if (!policyType) return "AuthzAllowAll";
727
+ return policyType in POLICY_TYPE_UI_CONFIG ? policyType : "AuthzAllowAll";
728
+ }
729
+ function PolicyOperationGroup({ operation, policies, tableName, onPolicyClick, onAddClick }) {
730
+ const [isOpen, setIsOpen] = useState(false);
731
+ const { colorMode: resolvedTheme } = useSchemaBuilderRuntime();
732
+ const mode = resolvedTheme === "dark" ? "dark" : "light";
733
+ const style = OPERATION_STYLES[operation];
734
+ const Icon = OPERATION_ICONS[style.iconName];
735
+ const visiblePolicies = policies.filter((p) => !POLICY_TYPE_UI_CONFIG[p.policyType ?? ""]?.disabled);
736
+ const hasPolicies = visiblePolicies.length > 0;
737
+ useEffect(() => {
738
+ if (!hasPolicies) setIsOpen(false);
739
+ }, [hasPolicies]);
740
+ return /* @__PURE__ */ jsx(Collapsible, {
741
+ open: isOpen,
742
+ onOpenChange: hasPolicies ? setIsOpen : void 0,
743
+ children: /* @__PURE__ */ jsxs("div", {
744
+ "data-chat-component": "policy-operation-group",
745
+ "data-chat-operation": operation,
746
+ "data-chat-policy-count": String(visiblePolicies.length),
747
+ "data-chat-policy-types": visiblePolicies.map((p) => {
748
+ return `${POLICY_TYPE_UI_CONFIG[p.policyType ?? ""]?.fallbackTitle ?? p.policyType ?? "unknown"} (${p.policyType ?? "unknown"})`;
749
+ }).join(", "),
750
+ className: "border-border/60 bg-card overflow-hidden rounded-lg border",
751
+ children: [/* @__PURE__ */ jsxs("div", {
752
+ className: "p-4",
753
+ children: [/* @__PURE__ */ jsxs("div", {
754
+ className: "flex items-center gap-3",
755
+ children: [
756
+ /* @__PURE__ */ jsx("div", {
757
+ className: "bg-muted/60 flex size-8 shrink-0 items-center justify-center rounded-lg",
758
+ children: /* @__PURE__ */ jsx(Icon, { className: "text-muted-foreground size-4" })
759
+ }),
760
+ /* @__PURE__ */ jsx("h3", {
761
+ className: "min-w-0 flex-1 text-balance text-sm font-semibold",
762
+ children: OPERATION_DISPLAY_NAMES[operation]
763
+ }),
764
+ /* @__PURE__ */ jsxs("div", {
765
+ className: "flex shrink-0 items-center gap-2",
766
+ children: [/* @__PURE__ */ jsxs(Button, {
767
+ variant: "outline",
768
+ size: "sm",
769
+ onClick: onAddClick,
770
+ className: "px-3",
771
+ children: [/* @__PURE__ */ jsx(Plus, { className: "size-4" }), "Add Policy"]
772
+ }), hasPolicies ? /* @__PURE__ */ jsx(CollapsibleTrigger, {
773
+ asChild: true,
774
+ children: /* @__PURE__ */ jsx("button", {
775
+ type: "button",
776
+ "aria-label": isOpen ? "Collapse policies" : "Expand policies",
777
+ className: "text-muted-foreground hover:text-foreground hover:bg-muted/60 grid size-10 place-items-center rounded-lg\n transition-[background-color,color,scale] duration-150 ease-out motion-safe:active:scale-[0.96] focus-visible:outline-none",
778
+ children: /* @__PURE__ */ jsx(ChevronDown, { className: cn("size-4 transition-transform duration-200", isOpen && "rotate-180") })
779
+ })
780
+ }) : /* @__PURE__ */ jsx("div", { className: "size-8" })]
781
+ })
782
+ ]
783
+ }), /* @__PURE__ */ jsx("div", {
784
+ className: cn("mt-2 flex flex-wrap items-center gap-2 pl-11 transition-opacity duration-150", isOpen && "pointer-events-none h-0 mt-0 overflow-hidden opacity-0"),
785
+ children: hasPolicies ? visiblePolicies.map((policy) => /* @__PURE__ */ jsx(PolicyPill, {
786
+ policy,
787
+ onClick: () => onPolicyClick(policy)
788
+ }, policy.id)) : /* @__PURE__ */ jsx("span", {
789
+ className: "text-muted-foreground text-sm italic",
790
+ children: "No active policies."
791
+ })
792
+ })]
793
+ }), hasPolicies && /* @__PURE__ */ jsx(CollapsibleContent, {
794
+ innerClassName: "px-4 pb-4 pt-0",
795
+ children: /* @__PURE__ */ jsx("div", {
796
+ className: "space-y-3",
797
+ children: visiblePolicies.map((policy) => {
798
+ const diagramKey = getDiagramKey(policy.policyType);
799
+ const theme = getDiagramTheme(getPolicyThemeKey(policy.policyType), mode);
800
+ const config = POLICY_TYPE_UI_CONFIG[policy.policyType ?? ""];
801
+ const title = config?.fallbackTitle ?? policy.policyType ?? policy.name ?? "Policy";
802
+ const PolicyIcon = config?.icon ?? Shield;
803
+ const description = config?.description;
804
+ return /* @__PURE__ */ jsxs("div", {
805
+ className: cn("group/diagram flex w-full items-stretch rounded-lg border text-left", "transition-[border-color,box-shadow,opacity] duration-150 ease-out", "hover:shadow-sm", `focus-visible:ring-ring focus-visible:ring-2 focus-visible:ring-offset-2
806
+ focus-visible:outline-none`, policy.disabled === true && "opacity-60"),
807
+ style: { borderColor: theme.border },
808
+ children: [/* @__PURE__ */ jsx("div", {
809
+ className: "flex w-1/2 items-center justify-center rounded-l-lg p-3",
810
+ style: { backgroundColor: theme.fill + "30" },
811
+ children: diagramKey ? /* @__PURE__ */ jsx(PolicyDiagramByKey, {
812
+ diagramKey,
813
+ tableName,
814
+ config: policy.data ?? {}
815
+ }) : /* @__PURE__ */ jsx("span", {
816
+ className: "text-muted-foreground text-sm",
817
+ children: title
818
+ })
819
+ }), /* @__PURE__ */ jsxs("div", {
820
+ className: "flex w-1/2 flex-col justify-center gap-2 p-4 pl-6",
821
+ children: [/* @__PURE__ */ jsxs("div", {
822
+ className: "flex items-center justify-between gap-2",
823
+ children: [/* @__PURE__ */ jsxs("div", {
824
+ className: "flex items-center gap-2",
825
+ children: [/* @__PURE__ */ jsx("div", {
826
+ className: "flex size-7 shrink-0 items-center justify-center rounded-full",
827
+ style: {
828
+ backgroundColor: theme.fill,
829
+ color: theme.primary
830
+ },
831
+ children: /* @__PURE__ */ jsx(PolicyIcon, { className: "size-4" })
832
+ }), /* @__PURE__ */ jsx("span", {
833
+ className: "text-sm font-semibold",
834
+ style: { color: theme.primary },
835
+ children: title
836
+ })]
837
+ }), /* @__PURE__ */ jsxs("button", {
838
+ type: "button",
839
+ onClick: () => onPolicyClick(policy),
840
+ className: "flex min-h-10 shrink-0 cursor-pointer items-center gap-1 rounded-full px-3 text-xs font-medium opacity-100\n transition-[opacity,scale] duration-150 ease-out motion-safe:active:scale-[0.96] sm:opacity-0\n pointer-coarse:opacity-100 group-hover/diagram:opacity-100 focus-visible:opacity-100",
841
+ style: { color: theme.primary },
842
+ children: [/* @__PURE__ */ jsx(Pencil, { className: "size-3" }), "Edit"]
843
+ })]
844
+ }), description && /* @__PURE__ */ jsx("p", {
845
+ className: "text-muted-foreground text-xs leading-relaxed",
846
+ children: description
847
+ })]
848
+ })]
849
+ }, policy.id);
850
+ })
851
+ })
852
+ })]
853
+ })
854
+ });
855
+ }
856
+
857
+ //#endregion
858
+ //#region src/schema/schema-builder-policies/components/table-editor/policies/policies-empty-state.tsx
859
+ function PoliciesEmptyState({ onCreateClick, tableName }) {
860
+ return /* @__PURE__ */ jsx(EmptyStateCard, {
861
+ icon: Shield,
862
+ title: tableName ? /* @__PURE__ */ jsxs(Fragment, { children: ["No policies for ", tableName] }) : "Create your first policy",
863
+ description: tableName ? `Add a row-level security policy to control access to ${tableName}. Policies determine who can read, create, update, or delete rows.` : "Create policies to control data access. Row-level security ensures users only see the data they are authorized to access.",
864
+ actionLabel: "Create Policy",
865
+ actionIcon: Shield,
866
+ onAction: onCreateClick,
867
+ features: [
868
+ {
869
+ icon: ShieldCheck,
870
+ title: "Row-Level Access",
871
+ description: "Control who can see each row"
872
+ },
873
+ {
874
+ icon: Lock,
875
+ title: "Secure by Default",
876
+ description: "Deny access unless explicitly allowed"
877
+ },
878
+ {
879
+ icon: Shield,
880
+ title: "Role-Based Rules",
881
+ description: "Different access for different roles"
882
+ }
883
+ ]
884
+ });
885
+ }
886
+
887
+ //#endregion
888
+ //#region src/schema/schema-builder-policies/components/table-editor/policies/policies-view.tsx
889
+ function NoTableSelectedState() {
890
+ return /* @__PURE__ */ jsxs("div", {
891
+ className: "flex min-h-0 flex-1 flex-col items-center justify-center p-6",
892
+ children: [
893
+ /* @__PURE__ */ jsx("div", {
894
+ className: "bg-muted/50 mb-4 flex h-16 w-16 items-center justify-center rounded-full",
895
+ children: /* @__PURE__ */ jsx(Table2, { className: "text-muted-foreground h-8 w-8" })
896
+ }),
897
+ /* @__PURE__ */ jsx("h3", {
898
+ className: "mb-1 text-balance text-lg font-semibold",
899
+ children: "No table selected"
900
+ }),
901
+ /* @__PURE__ */ jsx("p", {
902
+ className: "text-muted-foreground max-w-sm text-center text-sm",
903
+ children: "Select a table from the sidebar to view and manage its security policies."
904
+ })
905
+ ]
906
+ });
907
+ }
908
+ function PoliciesView() {
909
+ const stack = useCardStack();
910
+ const queryClient = useQueryClient();
911
+ const { currentTable, currentDatabase } = useSchemaBuilderSelectors();
912
+ const databaseId = currentDatabase?.databaseId ?? "";
913
+ const { data: tablesData = [], isLoading } = useDatabasePolicies(databaseId, { enabled: Boolean(databaseId) });
914
+ const currentTablePolicies = useMemo(() => {
915
+ if (!currentTable) return null;
916
+ return tablesData.find((t) => t.id === currentTable.id) ?? null;
917
+ }, [tablesData, currentTable])?.policies ?? [];
918
+ const groupedPolicies = useMemo(() => {
919
+ const groups = {
920
+ read: [],
921
+ create: [],
922
+ update: [],
923
+ delete: []
924
+ };
925
+ for (const policy of currentTablePolicies) {
926
+ const op = policy.privilege ? PRIVILEGE_TO_OPERATION[policy.privilege] : void 0;
927
+ if (op && op in groups) groups[op].push(policy);
928
+ }
929
+ return groups;
930
+ }, [currentTablePolicies]);
931
+ if (!currentTable) return /* @__PURE__ */ jsx(NoTableSelectedState, {});
932
+ const hasPolicies = currentTablePolicies.length > 0;
933
+ const handleOpenCreate = (operation) => {
934
+ stack.push({
935
+ id: `policy-create-${currentTable.id}${operation ? `-${operation}` : ""}`,
936
+ title: operation ? `Add ${operation.toUpperCase()} Policy` : "Create New Policies",
937
+ description: `Add security policies to ${currentTable.name}`,
938
+ Component: CreateTableCard,
939
+ props: {
940
+ mode: "add-policies",
941
+ tableId: currentTable.id,
942
+ tableName: currentTable.name,
943
+ preSelectedOperation: operation,
944
+ onPoliciesCreated: () => {
945
+ if (databaseId) queryClient.invalidateQueries({ queryKey: databasePoliciesQueryKeys.byDatabase(databaseId) });
946
+ }
947
+ },
948
+ width: CARD_WIDTHS.extraWide
949
+ });
950
+ };
951
+ const handleOpenEdit = (policy) => {
952
+ stack.push({
953
+ id: `policy-edit-${policy.id}`,
954
+ title: "Edit Policy",
955
+ description: "Update policy settings",
956
+ Component: PolicyEditCard,
957
+ props: {
958
+ policy: {
959
+ ...policy,
960
+ tableId: currentTable.id
961
+ },
962
+ tableName: currentTable.name,
963
+ onSuccess: () => {
964
+ if (databaseId) queryClient.invalidateQueries({ queryKey: databasePoliciesQueryKeys.byDatabase(databaseId) });
965
+ }
966
+ },
967
+ width: 500
968
+ });
969
+ };
970
+ return /* @__PURE__ */ jsx("div", {
971
+ "data-chat-component": "policies-view",
972
+ "data-chat-table-name": currentTable.name,
973
+ "data-chat-policy-count": String(currentTablePolicies.length),
974
+ className: "flex min-h-0 flex-1 flex-col overflow-auto p-6",
975
+ children: isLoading ? /* @__PURE__ */ jsx("div", {
976
+ className: "flex items-center justify-center py-16",
977
+ children: /* @__PURE__ */ jsx("p", {
978
+ className: "text-muted-foreground text-sm",
979
+ children: "Loading policies..."
980
+ })
981
+ }) : !hasPolicies ? /* @__PURE__ */ jsx(PoliciesEmptyState, {
982
+ onCreateClick: () => handleOpenCreate(),
983
+ tableName: currentTable.name
984
+ }) : /* @__PURE__ */ jsxs("div", {
985
+ className: "mx-auto w-full max-w-4xl space-y-6",
986
+ children: [/* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("h2", {
987
+ className: "text-balance text-xl font-semibold tracking-tight",
988
+ children: "Policies"
989
+ }), /* @__PURE__ */ jsxs("p", {
990
+ className: "text-muted-foreground mt-1 text-sm",
991
+ children: [
992
+ currentTablePolicies.length,
993
+ " polic",
994
+ currentTablePolicies.length !== 1 ? "ies" : "y",
995
+ " protecting",
996
+ " ",
997
+ /* @__PURE__ */ jsx("code", {
998
+ className: "bg-muted rounded px-1.5 py-0.5 font-mono text-xs",
999
+ children: currentTable.name
1000
+ })
1001
+ ]
1002
+ })] }), /* @__PURE__ */ jsx("div", {
1003
+ className: "space-y-3",
1004
+ children: CRUD_OPERATIONS.map((op) => /* @__PURE__ */ jsx(PolicyOperationGroup, {
1005
+ operation: op,
1006
+ policies: groupedPolicies[op],
1007
+ tableName: currentTable.name,
1008
+ onPolicyClick: handleOpenEdit,
1009
+ onAddClick: () => handleOpenCreate(op)
1010
+ }, op))
1011
+ })]
1012
+ })
1013
+ });
1014
+ }
1015
+
1016
+ //#endregion
1017
+ export { PoliciesView as t };
1018
+ //# sourceMappingURL=policies-view-DTfAhX7G.js.map