@langwatch/server 3.1.0-beta.4 → 3.1.0-beta.5

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 (118) hide show
  1. package/langwatch/ee/admin/ImpersonationBanner.tsx +61 -0
  2. package/langwatch/ee/admin/ImpersonationSwitchBackMenuItem.tsx +30 -0
  3. package/langwatch/ee/admin/__tests__/ImpersonationBanner.integration.test.tsx +82 -0
  4. package/langwatch/ee/admin/__tests__/impersonation.service.unit.test.ts +231 -0
  5. package/langwatch/ee/admin/backoffice/BackofficeTable.tsx +200 -0
  6. package/langwatch/ee/admin/backoffice/__tests__/adminClient.unit.test.ts +147 -0
  7. package/langwatch/ee/admin/backoffice/__tests__/dateInputToISO.unit.test.ts +42 -0
  8. package/langwatch/ee/admin/backoffice/__tests__/userVisibility.unit.test.ts +174 -0
  9. package/langwatch/ee/admin/backoffice/adminClient.ts +114 -0
  10. package/langwatch/ee/admin/backoffice/resources/OrganizationFeaturesView.tsx +373 -0
  11. package/langwatch/ee/admin/backoffice/resources/OrganizationsView.tsx +677 -0
  12. package/langwatch/ee/admin/backoffice/resources/ProjectsView.tsx +647 -0
  13. package/langwatch/ee/admin/backoffice/resources/SubscriptionsView.tsx +628 -0
  14. package/langwatch/ee/admin/backoffice/resources/UsersView.tsx +556 -0
  15. package/langwatch/ee/admin/backoffice/useAdminResource.ts +104 -0
  16. package/langwatch/ee/admin/backoffice/userVisibility.ts +105 -0
  17. package/langwatch/ee/admin/impersonation.service.ts +170 -0
  18. package/langwatch/ee/admin/isAdmin.ts +9 -0
  19. package/langwatch/ee/admin/routes/admin.ts +531 -0
  20. package/langwatch/ee/admin/safeSelects.ts +64 -0
  21. package/langwatch/ee/billing/__tests__/customerService.unit.test.ts +183 -0
  22. package/langwatch/ee/billing/__tests__/growthSeatEvent.unit.test.ts +213 -0
  23. package/langwatch/ee/billing/__tests__/licensePurchaseHandler.unit.test.ts +250 -0
  24. package/langwatch/ee/billing/__tests__/notification.service.unit.test.ts +768 -0
  25. package/langwatch/ee/billing/__tests__/planLimits.unit.test.ts +19 -0
  26. package/langwatch/ee/billing/__tests__/planProvider.unit.test.ts +440 -0
  27. package/langwatch/ee/billing/__tests__/planTypes.unit.test.ts +32 -0
  28. package/langwatch/ee/billing/__tests__/pricing.unit.test.ts +54 -0
  29. package/langwatch/ee/billing/__tests__/seatEventSubscription.unit.test.ts +717 -0
  30. package/langwatch/ee/billing/__tests__/stripe.integration.test.ts +344 -0
  31. package/langwatch/ee/billing/__tests__/stripePrices.types.unit.test.ts +27 -0
  32. package/langwatch/ee/billing/__tests__/stripePricesLoader.unit.test.ts +72 -0
  33. package/langwatch/ee/billing/__tests__/subscription.service.unit.test.ts +730 -0
  34. package/langwatch/ee/billing/__tests__/subscriptionItemCalculator.unit.test.ts +176 -0
  35. package/langwatch/ee/billing/__tests__/usage-limit.service.unit.test.ts +706 -0
  36. package/langwatch/ee/billing/__tests__/usageReportingService.integration.test.ts +421 -0
  37. package/langwatch/ee/billing/__tests__/usageReportingService.unit.test.ts +515 -0
  38. package/langwatch/ee/billing/__tests__/webhookService.unit.test.ts +958 -0
  39. package/langwatch/ee/billing/currencyRouter.ts +18 -0
  40. package/langwatch/ee/billing/errors.ts +129 -0
  41. package/langwatch/ee/billing/index.ts +67 -0
  42. package/langwatch/ee/billing/middleware.ts +17 -0
  43. package/langwatch/ee/billing/notifications/notification.service.ts +651 -0
  44. package/langwatch/ee/billing/notifications/repositories/notification.repository.ts +51 -0
  45. package/langwatch/ee/billing/notifications/types.ts +9 -0
  46. package/langwatch/ee/billing/notifications/usage-limit.service.ts +622 -0
  47. package/langwatch/ee/billing/nurturing/hooks/activityTracking.ts +90 -0
  48. package/langwatch/ee/billing/nurturing/hooks/activityTracking.unit.test.ts +237 -0
  49. package/langwatch/ee/billing/nurturing/hooks/featureAdoption.ts +120 -0
  50. package/langwatch/ee/billing/nurturing/hooks/featureAdoption.unit.test.ts +324 -0
  51. package/langwatch/ee/billing/nurturing/hooks/index.ts +17 -0
  52. package/langwatch/ee/billing/nurturing/hooks/productInterest.ts +68 -0
  53. package/langwatch/ee/billing/nurturing/hooks/productInterest.unit.test.ts +138 -0
  54. package/langwatch/ee/billing/nurturing/hooks/promptCreation.integration.test.ts +288 -0
  55. package/langwatch/ee/billing/nurturing/hooks/promptCreation.ts +131 -0
  56. package/langwatch/ee/billing/nurturing/hooks/promptCreation.unit.test.ts +134 -0
  57. package/langwatch/ee/billing/nurturing/hooks/signupIdentification.ts +101 -0
  58. package/langwatch/ee/billing/nurturing/hooks/signupIdentification.unit.test.ts +317 -0
  59. package/langwatch/ee/billing/nurturing/hooks/subscriptionSync.ts +49 -0
  60. package/langwatch/ee/billing/nurturing/hooks/subscriptionSync.unit.test.ts +170 -0
  61. package/langwatch/ee/billing/nurturing/hooks/userSync.ts +143 -0
  62. package/langwatch/ee/billing/nurturing/hooks/userSync.unit.test.ts +307 -0
  63. package/langwatch/ee/billing/nurturing/nurturing.service.ts +179 -0
  64. package/langwatch/ee/billing/nurturing/nurturing.service.unit.test.ts +266 -0
  65. package/langwatch/ee/billing/nurturing/nurturing.service.wiring.unit.test.ts +109 -0
  66. package/langwatch/ee/billing/nurturing/types.ts +125 -0
  67. package/langwatch/ee/billing/planLimits.ts +190 -0
  68. package/langwatch/ee/billing/planProvider.ts +119 -0
  69. package/langwatch/ee/billing/planTypes.ts +53 -0
  70. package/langwatch/ee/billing/pricing.ts +57 -0
  71. package/langwatch/ee/billing/services/billableEventsQuery.ts +248 -0
  72. package/langwatch/ee/billing/services/customerService.ts +90 -0
  73. package/langwatch/ee/billing/services/licensePurchaseHandler.ts +73 -0
  74. package/langwatch/ee/billing/services/seatEventSubscription.ts +351 -0
  75. package/langwatch/ee/billing/services/seatSyncService.ts +34 -0
  76. package/langwatch/ee/billing/services/subscription.repository.ts +203 -0
  77. package/langwatch/ee/billing/services/subscription.service.ts +549 -0
  78. package/langwatch/ee/billing/services/subscriptionItemCalculator.ts +249 -0
  79. package/langwatch/ee/billing/services/usageReportingService.ts +301 -0
  80. package/langwatch/ee/billing/services/webhookService.ts +858 -0
  81. package/langwatch/ee/billing/stripe/stripeCatalog.json +1781 -0
  82. package/langwatch/ee/billing/stripe/stripeClient.ts +13 -0
  83. package/langwatch/ee/billing/stripe/stripePriceCatalog.ts +70 -0
  84. package/langwatch/ee/billing/stripe/stripePrices.types.ts +118 -0
  85. package/langwatch/ee/billing/subscriptionRouter.ts +202 -0
  86. package/langwatch/ee/billing/types.ts +102 -0
  87. package/langwatch/ee/billing/utils/currency.ts +99 -0
  88. package/langwatch/ee/billing/utils/growthSeatEvent.ts +137 -0
  89. package/langwatch/ee/licensing/__tests__/constants.unit.test.ts +56 -0
  90. package/langwatch/ee/licensing/__tests__/defaults.unit.test.ts +192 -0
  91. package/langwatch/ee/licensing/__tests__/fixtures/testKeys.ts +86 -0
  92. package/langwatch/ee/licensing/__tests__/fixtures/testLicenses.ts +92 -0
  93. package/langwatch/ee/licensing/__tests__/licenseGenerationService.unit.test.ts +232 -0
  94. package/langwatch/ee/licensing/__tests__/licenseHandler.integration.test.ts +419 -0
  95. package/langwatch/ee/licensing/__tests__/planMapping.unit.test.ts +195 -0
  96. package/langwatch/ee/licensing/__tests__/planTemplates.unit.test.ts +193 -0
  97. package/langwatch/ee/licensing/__tests__/signing.unit.test.ts +164 -0
  98. package/langwatch/ee/licensing/__tests__/validation.unit.test.ts +281 -0
  99. package/langwatch/ee/licensing/client.ts +29 -0
  100. package/langwatch/ee/licensing/constants.ts +195 -0
  101. package/langwatch/ee/licensing/defaults.ts +55 -0
  102. package/langwatch/ee/licensing/errors.ts +11 -0
  103. package/langwatch/ee/licensing/index.ts +52 -0
  104. package/langwatch/ee/licensing/licenseGenerationService.ts +87 -0
  105. package/langwatch/ee/licensing/licenseHandler.ts +308 -0
  106. package/langwatch/ee/licensing/planInfo.ts +46 -0
  107. package/langwatch/ee/licensing/planMapping.ts +37 -0
  108. package/langwatch/ee/licensing/planTemplates.ts +109 -0
  109. package/langwatch/ee/licensing/server.ts +43 -0
  110. package/langwatch/ee/licensing/signing.ts +43 -0
  111. package/langwatch/ee/licensing/types.ts +156 -0
  112. package/langwatch/ee/licensing/validation.ts +107 -0
  113. package/langwatch/ee/managed-providers/ManagedModelProviderAlert.tsx +29 -0
  114. package/langwatch/ee/managed-providers/managedBedrockConfig.ts +226 -0
  115. package/langwatch/ee/saas/ExtraFooterComponents.tsx +169 -0
  116. package/langwatch/package.json +1 -1
  117. package/package.json +1 -1
  118. package/packages/server/dist/cli.cjs +1 -1
@@ -0,0 +1,677 @@
1
+ import {
2
+ Box,
3
+ Button,
4
+ Field,
5
+ Heading,
6
+ HStack,
7
+ Input,
8
+ NativeSelect,
9
+ Separator,
10
+ Spacer,
11
+ Table,
12
+ Text,
13
+ Textarea,
14
+ VStack,
15
+ } from "@chakra-ui/react";
16
+ import { Currency, PricingModel } from "@prisma/client";
17
+ import { MoreVertical, Pencil } from "lucide-react";
18
+ import { useEffect, useRef, useState } from "react";
19
+ import { useDebounce } from "use-debounce";
20
+ import { useRouter } from "~/utils/compat/next-router";
21
+ import { Drawer } from "~/components/ui/drawer";
22
+ import { Menu } from "~/components/ui/menu";
23
+ import { Switch } from "~/components/ui/switch";
24
+ import { toaster } from "~/components/ui/toaster";
25
+ import {
26
+ BackofficeTable,
27
+ EmptyCell,
28
+ dateInputToISO,
29
+ formatDate,
30
+ } from "../BackofficeTable";
31
+ import {
32
+ useAdminList,
33
+ useAdminUpdate,
34
+ } from "../useAdminResource";
35
+
36
+ /**
37
+ * Read-facing Organization shape — intentionally does NOT include
38
+ * elasticsearchNodeUrl / elasticsearchApiKey / s3Endpoint / s3AccessKeyId /
39
+ * s3SecretAccessKey / s3Bucket. Those are credentials and the admin Hono
40
+ * route strips them from every list / getOne response (see
41
+ * ee/admin/safeSelects.ts). The edit drawer still accepts *new* values for
42
+ * those fields, write-only.
43
+ */
44
+ interface AdminOrganization {
45
+ id: string;
46
+ name: string;
47
+ slug: string;
48
+ phoneNumber: string | null;
49
+ ssoDomain: string | null;
50
+ ssoProvider: string | null;
51
+ usageSpendingMaxLimit: number | null;
52
+ signedDPA: boolean;
53
+ promoCode: string | null;
54
+ stripeCustomerId: string | null;
55
+ currency: Currency;
56
+ pricingModel: PricingModel;
57
+ license: string | null;
58
+ licenseExpiresAt: string | null;
59
+ useCustomElasticsearch: boolean;
60
+ useCustomS3: boolean;
61
+ createdAt: string;
62
+ }
63
+
64
+ const PAGE_SIZE = 25;
65
+
66
+ export default function OrganizationsView() {
67
+ const router = useRouter();
68
+ // `q` deep-links from other Backoffice pages (e.g. clicking an org chip on
69
+ // the Users table lands here with ?q=<orgId>). Seed once on mount so the
70
+ // list comes up pre-filtered without fighting the user's subsequent typing.
71
+ const initialQueryRef = useRef<string>(
72
+ typeof router.query.q === "string" ? router.query.q : "",
73
+ );
74
+ const [page, setPage] = useState(1);
75
+ const [search, setSearch] = useState(initialQueryRef.current);
76
+ const [debouncedSearch] = useDebounce(search, 300);
77
+ const [editing, setEditing] = useState<AdminOrganization | null>(null);
78
+
79
+ const list = useAdminList<AdminOrganization>("organization", {
80
+ pagination: { page, perPage: PAGE_SIZE },
81
+ sort: { field: "createdAt", order: "DESC" },
82
+ filter: debouncedSearch ? { query: debouncedSearch } : {},
83
+ });
84
+
85
+ return (
86
+ <>
87
+ <BackofficeTable
88
+ title="Organizations"
89
+ searchValue={search}
90
+ onSearchChange={(v) => {
91
+ setSearch(v);
92
+ setPage(1);
93
+ }}
94
+ searchPlaceholder="Search by ID, name, or slug"
95
+ isLoading={list.isLoading}
96
+ isFetching={list.isFetching}
97
+ error={list.error}
98
+ pagination={{
99
+ page,
100
+ perPage: PAGE_SIZE,
101
+ total: list.data?.total ?? 0,
102
+ onPageChange: setPage,
103
+ }}
104
+ >
105
+ <Table.Root variant="line" size="md" width="full">
106
+ <Table.Header>
107
+ <Table.Row>
108
+ <Table.ColumnHeader>ID</Table.ColumnHeader>
109
+ <Table.ColumnHeader>Name</Table.ColumnHeader>
110
+ <Table.ColumnHeader>Slug</Table.ColumnHeader>
111
+ <Table.ColumnHeader>Plan model</Table.ColumnHeader>
112
+ <Table.ColumnHeader>Currency</Table.ColumnHeader>
113
+ <Table.ColumnHeader>SSO domain</Table.ColumnHeader>
114
+ <Table.ColumnHeader>Created</Table.ColumnHeader>
115
+ <Table.ColumnHeader width="60px" textAlign="right" />
116
+ </Table.Row>
117
+ </Table.Header>
118
+ <Table.Body>
119
+ {list.data?.data.length === 0 && (
120
+ <Table.Row>
121
+ <Table.Cell colSpan={8}>
122
+ <Text color="fg.muted" textAlign="center" paddingY={6}>
123
+ No organizations match your search.
124
+ </Text>
125
+ </Table.Cell>
126
+ </Table.Row>
127
+ )}
128
+ {list.data?.data.map((org) => (
129
+ <Table.Row key={org.id}>
130
+ <Table.Cell fontSize="xs" color="fg.muted">
131
+ {org.id}
132
+ </Table.Cell>
133
+ <Table.Cell>{org.name}</Table.Cell>
134
+ <Table.Cell>{org.slug}</Table.Cell>
135
+ <Table.Cell>{org.pricingModel}</Table.Cell>
136
+ <Table.Cell>{org.currency}</Table.Cell>
137
+ <Table.Cell>
138
+ {org.ssoDomain ?? <EmptyCell />}
139
+ </Table.Cell>
140
+ <Table.Cell>{formatDate(org.createdAt)}</Table.Cell>
141
+ <Table.Cell textAlign="right">
142
+ <Box
143
+ width="full"
144
+ height="full"
145
+ display="flex"
146
+ justifyContent="end"
147
+ >
148
+ <Menu.Root>
149
+ <Menu.Trigger>
150
+ <MoreVertical size={16} />
151
+ </Menu.Trigger>
152
+ <Menu.Content>
153
+ <Menu.Item
154
+ value="edit"
155
+ onClick={() => setEditing(org)}
156
+ >
157
+ <Pencil size={16} />
158
+ Edit
159
+ </Menu.Item>
160
+ </Menu.Content>
161
+ </Menu.Root>
162
+ </Box>
163
+ </Table.Cell>
164
+ </Table.Row>
165
+ ))}
166
+ </Table.Body>
167
+ </Table.Root>
168
+ </BackofficeTable>
169
+
170
+ <OrganizationEditDrawer
171
+ organization={editing}
172
+ onClose={() => setEditing(null)}
173
+ />
174
+ </>
175
+ );
176
+ }
177
+
178
+ interface FormState {
179
+ name: string;
180
+ slug: string;
181
+ phoneNumber: string;
182
+ ssoDomain: string;
183
+ ssoProvider: string;
184
+ usageSpendingMaxLimit: string;
185
+ signedDPA: boolean;
186
+ promoCode: string;
187
+ stripeCustomerId: string;
188
+ currency: Currency;
189
+ pricingModel: PricingModel;
190
+ license: string;
191
+ licenseExpiresAt: string;
192
+ useCustomElasticsearch: boolean;
193
+ elasticsearchNodeUrl: string;
194
+ elasticsearchApiKey: string;
195
+ useCustomS3: boolean;
196
+ s3Endpoint: string;
197
+ s3AccessKeyId: string;
198
+ s3SecretAccessKey: string;
199
+ s3Bucket: string;
200
+ }
201
+
202
+ function toDateInputValue(value: string | null | undefined): string {
203
+ if (!value) return "";
204
+ const d = new Date(value);
205
+ if (Number.isNaN(d.getTime())) return "";
206
+ return d.toISOString().slice(0, 10);
207
+ }
208
+
209
+ function numOrNull(raw: string): number | null {
210
+ if (raw === "") return null;
211
+ const n = Number(raw);
212
+ return Number.isFinite(n) ? n : null;
213
+ }
214
+
215
+ function nullIfEmpty(raw: string): string | null {
216
+ return raw.trim() === "" ? null : raw;
217
+ }
218
+
219
+ function OrganizationEditDrawer({
220
+ organization,
221
+ onClose,
222
+ }: {
223
+ organization: AdminOrganization | null;
224
+ onClose: () => void;
225
+ }) {
226
+ const update = useAdminUpdate<AdminOrganization>("organization");
227
+ const [form, setForm] = useState<FormState | null>(null);
228
+
229
+ useEffect(() => {
230
+ if (!organization) return;
231
+ setForm({
232
+ name: organization.name ?? "",
233
+ slug: organization.slug ?? "",
234
+ phoneNumber: organization.phoneNumber ?? "",
235
+ ssoDomain: organization.ssoDomain ?? "",
236
+ ssoProvider: organization.ssoProvider ?? "",
237
+ usageSpendingMaxLimit:
238
+ organization.usageSpendingMaxLimit?.toString() ?? "",
239
+ signedDPA: !!organization.signedDPA,
240
+ promoCode: organization.promoCode ?? "",
241
+ stripeCustomerId: organization.stripeCustomerId ?? "",
242
+ currency: organization.currency,
243
+ pricingModel: organization.pricingModel,
244
+ license: organization.license ?? "",
245
+ licenseExpiresAt: toDateInputValue(organization.licenseExpiresAt),
246
+ useCustomElasticsearch: !!organization.useCustomElasticsearch,
247
+ useCustomS3: !!organization.useCustomS3,
248
+ // Credentials are write-only: the server doesn't echo them back in
249
+ // list/getOne responses (see ee/admin/safeSelects.ts), so the form
250
+ // always starts empty. A non-empty value on save is the user typing
251
+ // a *new* secret; an empty value is left unchanged.
252
+ elasticsearchNodeUrl: "",
253
+ elasticsearchApiKey: "",
254
+ s3Endpoint: "",
255
+ s3AccessKeyId: "",
256
+ s3SecretAccessKey: "",
257
+ s3Bucket: "",
258
+ });
259
+ }, [organization]);
260
+
261
+ const setField = <K extends keyof FormState>(key: K, value: FormState[K]) =>
262
+ setForm((prev) => (prev ? { ...prev, [key]: value } : prev));
263
+
264
+ const handleSave = () => {
265
+ if (!organization || !form) return;
266
+ const data: Record<string, unknown> = {};
267
+ if (form.name !== organization.name) data.name = form.name;
268
+ if (form.slug !== organization.slug) data.slug = form.slug;
269
+ if (form.phoneNumber !== (organization.phoneNumber ?? ""))
270
+ data.phoneNumber = nullIfEmpty(form.phoneNumber);
271
+ if (form.ssoDomain !== (organization.ssoDomain ?? ""))
272
+ data.ssoDomain = nullIfEmpty(form.ssoDomain);
273
+ if (form.ssoProvider !== (organization.ssoProvider ?? ""))
274
+ data.ssoProvider = nullIfEmpty(form.ssoProvider);
275
+ const nextLimit = numOrNull(form.usageSpendingMaxLimit);
276
+ if (nextLimit !== organization.usageSpendingMaxLimit) {
277
+ data.usageSpendingMaxLimit = nextLimit;
278
+ }
279
+ if (form.signedDPA !== !!organization.signedDPA)
280
+ data.signedDPA = form.signedDPA;
281
+ if (form.promoCode !== (organization.promoCode ?? ""))
282
+ data.promoCode = nullIfEmpty(form.promoCode);
283
+ if (form.stripeCustomerId !== (organization.stripeCustomerId ?? ""))
284
+ data.stripeCustomerId = nullIfEmpty(form.stripeCustomerId);
285
+ if (form.currency !== organization.currency) data.currency = form.currency;
286
+ if (form.pricingModel !== organization.pricingModel)
287
+ data.pricingModel = form.pricingModel;
288
+ if (form.license !== (organization.license ?? ""))
289
+ data.license = nullIfEmpty(form.license);
290
+ const nextExpires = dateInputToISO(form.licenseExpiresAt);
291
+ if (nextExpires !== organization.licenseExpiresAt) {
292
+ data.licenseExpiresAt = nextExpires;
293
+ }
294
+ if (
295
+ form.useCustomElasticsearch !== !!organization.useCustomElasticsearch
296
+ ) {
297
+ data.useCustomElasticsearch = form.useCustomElasticsearch;
298
+ }
299
+ if (form.useCustomS3 !== !!organization.useCustomS3)
300
+ data.useCustomS3 = form.useCustomS3;
301
+ // Credentials are write-only — the form starts empty and the server
302
+ // never echoes the stored value. Only forward fields the user typed
303
+ // into; an empty input is treated as "leave the stored secret alone".
304
+ if (form.elasticsearchNodeUrl.trim() !== "") {
305
+ data.elasticsearchNodeUrl = form.elasticsearchNodeUrl;
306
+ }
307
+ if (form.elasticsearchApiKey.trim() !== "") {
308
+ data.elasticsearchApiKey = form.elasticsearchApiKey;
309
+ }
310
+ if (form.s3Endpoint.trim() !== "") data.s3Endpoint = form.s3Endpoint;
311
+ if (form.s3AccessKeyId.trim() !== "")
312
+ data.s3AccessKeyId = form.s3AccessKeyId;
313
+ if (form.s3SecretAccessKey.trim() !== "")
314
+ data.s3SecretAccessKey = form.s3SecretAccessKey;
315
+ if (form.s3Bucket.trim() !== "") data.s3Bucket = form.s3Bucket;
316
+
317
+ if (Object.keys(data).length === 0) {
318
+ onClose();
319
+ return;
320
+ }
321
+ update.mutate(
322
+ { id: organization.id, data },
323
+ {
324
+ onSuccess: () => {
325
+ toaster.create({
326
+ title: "Organization updated",
327
+ type: "success",
328
+ duration: 3000,
329
+ meta: { closable: true },
330
+ });
331
+ onClose();
332
+ },
333
+ onError: (err) =>
334
+ toaster.create({
335
+ title: "Update failed",
336
+ description: err.message,
337
+ type: "error",
338
+ duration: 5000,
339
+ meta: { closable: true },
340
+ }),
341
+ },
342
+ );
343
+ };
344
+
345
+ return (
346
+ <Drawer.Root
347
+ open={!!organization}
348
+ onOpenChange={({ open }) => {
349
+ if (!open) onClose();
350
+ }}
351
+ size="md"
352
+ >
353
+ <Drawer.Content>
354
+ <Drawer.Header>
355
+ <Drawer.Title>Edit Organization</Drawer.Title>
356
+ </Drawer.Header>
357
+ <Drawer.CloseTrigger />
358
+ <Drawer.Body>
359
+ {organization && form && (
360
+ <VStack gap={4} align="stretch">
361
+ <SectionHeading>Identity</SectionHeading>
362
+ <Field.Root>
363
+ <Field.Label>Name</Field.Label>
364
+ <Input
365
+ value={form.name}
366
+ onChange={(e) => setField("name", e.target.value)}
367
+ />
368
+ </Field.Root>
369
+ <Field.Root>
370
+ <Field.Label>Slug</Field.Label>
371
+ <Input
372
+ value={form.slug}
373
+ onChange={(e) => setField("slug", e.target.value)}
374
+ />
375
+ <Field.HelperText>
376
+ URL-safe identifier. Changing this can break existing links.
377
+ </Field.HelperText>
378
+ </Field.Root>
379
+ <Field.Root>
380
+ <Field.Label>Phone number</Field.Label>
381
+ <Input
382
+ type="tel"
383
+ value={form.phoneNumber}
384
+ onChange={(e) => setField("phoneNumber", e.target.value)}
385
+ />
386
+ </Field.Root>
387
+
388
+ <SectionHeading>Billing</SectionHeading>
389
+ <HStack gap={3} align="start">
390
+ <Field.Root>
391
+ <Field.Label>Currency</Field.Label>
392
+ <EnumSelect
393
+ value={form.currency}
394
+ options={Object.values(Currency)}
395
+ onChange={(v) => setField("currency", v as Currency)}
396
+ />
397
+ </Field.Root>
398
+ <Field.Root>
399
+ <Field.Label>Pricing model</Field.Label>
400
+ <EnumSelect
401
+ value={form.pricingModel}
402
+ options={Object.values(PricingModel)}
403
+ onChange={(v) =>
404
+ setField("pricingModel", v as PricingModel)
405
+ }
406
+ />
407
+ </Field.Root>
408
+ </HStack>
409
+ <Field.Root>
410
+ <Field.Label>Stripe customer ID</Field.Label>
411
+ <Input
412
+ value={form.stripeCustomerId}
413
+ onChange={(e) =>
414
+ setField("stripeCustomerId", e.target.value)
415
+ }
416
+ />
417
+ </Field.Root>
418
+ <Field.Root>
419
+ <Field.Label>Promo code</Field.Label>
420
+ <Input
421
+ value={form.promoCode}
422
+ onChange={(e) => setField("promoCode", e.target.value)}
423
+ />
424
+ </Field.Root>
425
+ <Field.Root>
426
+ <Field.Label>Usage spending max limit</Field.Label>
427
+ <Input
428
+ type="number"
429
+ value={form.usageSpendingMaxLimit}
430
+ onChange={(e) =>
431
+ setField("usageSpendingMaxLimit", e.target.value)
432
+ }
433
+ placeholder="Leave empty for no cap"
434
+ />
435
+ </Field.Root>
436
+ <ToggleRow
437
+ label="Signed DPA"
438
+ hint="Enterprise data processing agreement on file."
439
+ checked={form.signedDPA}
440
+ onChange={(v) => setField("signedDPA", v)}
441
+ />
442
+
443
+ <SectionHeading>Authentication</SectionHeading>
444
+ <Field.Root>
445
+ <Field.Label>SSO domain</Field.Label>
446
+ <Input
447
+ value={form.ssoDomain}
448
+ onChange={(e) => setField("ssoDomain", e.target.value)}
449
+ placeholder="e.g. acme.com"
450
+ />
451
+ <Field.HelperText>
452
+ Lowercased server-side. Users with this email domain can
453
+ sign in via SSO.
454
+ </Field.HelperText>
455
+ </Field.Root>
456
+ <Field.Root>
457
+ <Field.Label>SSO provider</Field.Label>
458
+ <Input
459
+ value={form.ssoProvider}
460
+ onChange={(e) => setField("ssoProvider", e.target.value)}
461
+ />
462
+ </Field.Root>
463
+
464
+ <SectionHeading>License</SectionHeading>
465
+ <Field.Root>
466
+ <Field.Label>License key</Field.Label>
467
+ <Textarea
468
+ rows={4}
469
+ value={form.license}
470
+ onChange={(e) => setField("license", e.target.value)}
471
+ fontFamily="mono"
472
+ fontSize="xs"
473
+ />
474
+ </Field.Root>
475
+ <Field.Root>
476
+ <Field.Label>License expires at</Field.Label>
477
+ <Input
478
+ type="date"
479
+ value={form.licenseExpiresAt}
480
+ onChange={(e) =>
481
+ setField("licenseExpiresAt", e.target.value)
482
+ }
483
+ />
484
+ </Field.Root>
485
+
486
+ <SectionHeading>Custom Elasticsearch</SectionHeading>
487
+ <Text fontSize="xs" color="fg.muted">
488
+ Credentials below are write-only — the server never reads them
489
+ back. Leave blank to keep the stored value; type to replace.
490
+ </Text>
491
+ <ToggleRow
492
+ label="Use custom Elasticsearch"
493
+ hint="Override the platform-managed ClickHouse/ES cluster for this tenant."
494
+ checked={form.useCustomElasticsearch}
495
+ onChange={(v) => setField("useCustomElasticsearch", v)}
496
+ />
497
+ <Field.Root>
498
+ <Field.Label>Node URL</Field.Label>
499
+ <Input
500
+ type="url"
501
+ value={form.elasticsearchNodeUrl}
502
+ onChange={(e) =>
503
+ setField("elasticsearchNodeUrl", e.target.value)
504
+ }
505
+ placeholder="Leave blank to keep current"
506
+ disabled={!form.useCustomElasticsearch}
507
+ />
508
+ </Field.Root>
509
+ <Field.Root>
510
+ <Field.Label>API key</Field.Label>
511
+ <Input
512
+ type="password"
513
+ value={form.elasticsearchApiKey}
514
+ onChange={(e) =>
515
+ setField("elasticsearchApiKey", e.target.value)
516
+ }
517
+ placeholder="Leave blank to keep current"
518
+ disabled={!form.useCustomElasticsearch}
519
+ autoComplete="new-password"
520
+ />
521
+ </Field.Root>
522
+
523
+ <SectionHeading>Custom S3</SectionHeading>
524
+ <Text fontSize="xs" color="fg.muted">
525
+ Credentials below are write-only — the server never reads them
526
+ back. Leave blank to keep the stored value; type to replace.
527
+ </Text>
528
+ <ToggleRow
529
+ label="Use custom S3"
530
+ hint="Store large blobs (datasets, uploads) in the tenant's own bucket."
531
+ checked={form.useCustomS3}
532
+ onChange={(v) => setField("useCustomS3", v)}
533
+ />
534
+ <Field.Root>
535
+ <Field.Label>Endpoint</Field.Label>
536
+ <Input
537
+ type="url"
538
+ value={form.s3Endpoint}
539
+ onChange={(e) => setField("s3Endpoint", e.target.value)}
540
+ placeholder="Leave blank to keep current"
541
+ disabled={!form.useCustomS3}
542
+ />
543
+ </Field.Root>
544
+ <Field.Root>
545
+ <Field.Label>Bucket</Field.Label>
546
+ <Input
547
+ value={form.s3Bucket}
548
+ onChange={(e) => setField("s3Bucket", e.target.value)}
549
+ placeholder="Leave blank to keep current"
550
+ disabled={!form.useCustomS3}
551
+ />
552
+ </Field.Root>
553
+ <Field.Root>
554
+ <Field.Label>Access key ID</Field.Label>
555
+ <Input
556
+ type="password"
557
+ value={form.s3AccessKeyId}
558
+ onChange={(e) => setField("s3AccessKeyId", e.target.value)}
559
+ placeholder="Leave blank to keep current"
560
+ disabled={!form.useCustomS3}
561
+ autoComplete="new-password"
562
+ />
563
+ </Field.Root>
564
+ <Field.Root>
565
+ <Field.Label>Secret access key</Field.Label>
566
+ <Input
567
+ type="password"
568
+ value={form.s3SecretAccessKey}
569
+ onChange={(e) =>
570
+ setField("s3SecretAccessKey", e.target.value)
571
+ }
572
+ placeholder="Leave blank to keep current"
573
+ autoComplete="new-password"
574
+ disabled={!form.useCustomS3}
575
+ />
576
+ </Field.Root>
577
+
578
+ <Separator my={2} />
579
+ <VStack align="start" gap={0}>
580
+ <Text fontSize="xs" color="fg.muted">
581
+ Organization ID: {organization.id}
582
+ </Text>
583
+ <Text fontSize="xs" color="fg.muted">
584
+ Created: {formatDate(organization.createdAt)}
585
+ </Text>
586
+ </VStack>
587
+ </VStack>
588
+ )}
589
+ </Drawer.Body>
590
+ <Drawer.Footer>
591
+ <HStack width="full">
592
+ <Spacer />
593
+ <Button variant="ghost" onClick={onClose}>
594
+ Cancel
595
+ </Button>
596
+ <Button loading={update.isPending} onClick={handleSave}>
597
+ Save
598
+ </Button>
599
+ </HStack>
600
+ </Drawer.Footer>
601
+ </Drawer.Content>
602
+ </Drawer.Root>
603
+ );
604
+ }
605
+
606
+ function SectionHeading({ children }: { children: React.ReactNode }) {
607
+ return (
608
+ <Heading
609
+ as="h3"
610
+ size="xs"
611
+ color="fg.muted"
612
+ textTransform="uppercase"
613
+ letterSpacing="wider"
614
+ pt={2}
615
+ >
616
+ {children}
617
+ </Heading>
618
+ );
619
+ }
620
+
621
+ function EnumSelect({
622
+ value,
623
+ options,
624
+ onChange,
625
+ }: {
626
+ value: string;
627
+ options: readonly string[];
628
+ onChange: (value: string) => void;
629
+ }) {
630
+ return (
631
+ <NativeSelect.Root size="sm" width="full">
632
+ <NativeSelect.Field
633
+ value={value}
634
+ onChange={(e) => onChange(e.target.value)}
635
+ >
636
+ {options.map((opt) => (
637
+ <option key={opt} value={opt}>
638
+ {opt}
639
+ </option>
640
+ ))}
641
+ </NativeSelect.Field>
642
+ <NativeSelect.Indicator />
643
+ </NativeSelect.Root>
644
+ );
645
+ }
646
+
647
+ function ToggleRow({
648
+ label,
649
+ hint,
650
+ checked,
651
+ onChange,
652
+ }: {
653
+ label: string;
654
+ hint?: string;
655
+ checked: boolean;
656
+ onChange: (v: boolean) => void;
657
+ }) {
658
+ return (
659
+ <Field.Root>
660
+ <HStack width="full">
661
+ <VStack align="start" gap={0}>
662
+ <Field.Label>{label}</Field.Label>
663
+ {hint && (
664
+ <Text fontSize="xs" color="fg.muted">
665
+ {hint}
666
+ </Text>
667
+ )}
668
+ </VStack>
669
+ <Spacer />
670
+ <Switch
671
+ checked={checked}
672
+ onCheckedChange={(e) => onChange(e.checked)}
673
+ />
674
+ </HStack>
675
+ </Field.Root>
676
+ );
677
+ }