@nexys/user-management-web 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +61 -0
- package/components.json +21 -0
- package/package.json +66 -0
- package/src/components/audit/audit-log-table.test.tsx +98 -0
- package/src/components/audit/audit-log-table.tsx +227 -0
- package/src/components/auth/accept-invitation-card.tsx +104 -0
- package/src/components/auth/forgot-password-form.tsx +93 -0
- package/src/components/auth/reset-password-form.tsx +144 -0
- package/src/components/auth/sign-in-form.test.tsx +164 -0
- package/src/components/auth/sign-in-form.tsx +504 -0
- package/src/components/auth/sign-up-form.tsx +124 -0
- package/src/components/layout/app-shell.tsx +189 -0
- package/src/components/layout/auth-shell.tsx +28 -0
- package/src/components/layout/theme-toggle.tsx +55 -0
- package/src/components/layout/user-menu.tsx +84 -0
- package/src/components/primitives/empty-state.tsx +40 -0
- package/src/components/primitives/page-header.tsx +27 -0
- package/src/components/primitives/spinner.tsx +28 -0
- package/src/components/profile/api-tokens-card.tsx +238 -0
- package/src/components/profile/change-password-form.tsx +159 -0
- package/src/components/profile/passkeys-card.tsx +186 -0
- package/src/components/profile/profile-card.tsx +107 -0
- package/src/components/profile/refresh-tokens-list.tsx +176 -0
- package/src/components/profile/two-factor-card.tsx +230 -0
- package/src/components/projects/project-consents.tsx +577 -0
- package/src/components/projects/project-create-dialog.tsx +193 -0
- package/src/components/projects/project-detail-tabs.tsx +64 -0
- package/src/components/projects/project-email-templates.tsx +285 -0
- package/src/components/projects/project-notices.tsx +461 -0
- package/src/components/projects/project-settings.tsx +1059 -0
- package/src/components/projects/project-tenants.tsx +108 -0
- package/src/components/projects/projects-table.test.tsx +109 -0
- package/src/components/projects/projects-table.tsx +240 -0
- package/src/components/roles/role-badge.tsx +91 -0
- package/src/components/roles/role-select.tsx +76 -0
- package/src/components/tenants/tenant-create-dialog.tsx +164 -0
- package/src/components/tenants/tenant-detail-tabs.tsx +51 -0
- package/src/components/tenants/tenant-members.tsx +587 -0
- package/src/components/tenants/tenant-roles.test.tsx +107 -0
- package/src/components/tenants/tenant-roles.tsx +418 -0
- package/src/components/tenants/tenant-settings.tsx +392 -0
- package/src/components/tenants/tenants-table.test.tsx +81 -0
- package/src/components/tenants/tenants-table.tsx +247 -0
- package/src/components/ui/alert.tsx +52 -0
- package/src/components/ui/avatar.tsx +46 -0
- package/src/components/ui/badge.tsx +32 -0
- package/src/components/ui/button.tsx +64 -0
- package/src/components/ui/card.tsx +65 -0
- package/src/components/ui/dialog.tsx +99 -0
- package/src/components/ui/dropdown-menu.tsx +97 -0
- package/src/components/ui/field.tsx +44 -0
- package/src/components/ui/input.tsx +23 -0
- package/src/components/ui/label.tsx +19 -0
- package/src/components/ui/select.tsx +87 -0
- package/src/components/ui/separator.tsx +22 -0
- package/src/components/ui/skeleton.tsx +13 -0
- package/src/components/ui/switch.tsx +29 -0
- package/src/components/ui/table.tsx +87 -0
- package/src/components/ui/tabs.tsx +51 -0
- package/src/components/ui/textarea.tsx +22 -0
- package/src/components/ui/tooltip.tsx +27 -0
- package/src/components/users/user-create-dialog.tsx +229 -0
- package/src/components/users/user-delete-dialog.tsx +88 -0
- package/src/components/users/user-detail.tsx +818 -0
- package/src/components/users/users-table.test.tsx +107 -0
- package/src/components/users/users-table.tsx +386 -0
- package/src/context/auth-context.tsx +127 -0
- package/src/context/theme-context.test.tsx +78 -0
- package/src/context/theme-context.tsx +110 -0
- package/src/hooks/use-call.ts +60 -0
- package/src/index.ts +160 -0
- package/src/lib/error-message.ts +16 -0
- package/src/lib/utils.test.ts +61 -0
- package/src/lib/utils.ts +79 -0
- package/src/lib/webauthn.ts +36 -0
- package/src/styles.css +107 -0
- package/src/test-setup.ts +5 -0
- package/tailwind.preset.cjs +15 -0
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { createProject, type TenancyMode } from "@nexys/user-management-client";
|
|
2
|
+
import { AlertCircle, Plus } from "lucide-react";
|
|
3
|
+
import { useState, type FormEvent, type ReactNode } from "react";
|
|
4
|
+
|
|
5
|
+
import { useUmClient } from "../../context/auth-context.js";
|
|
6
|
+
import { errorMessage } from "../../lib/error-message.js";
|
|
7
|
+
import { Alert, AlertDescription } from "../ui/alert.js";
|
|
8
|
+
import { Button } from "../ui/button.js";
|
|
9
|
+
import {
|
|
10
|
+
Dialog,
|
|
11
|
+
DialogContent,
|
|
12
|
+
DialogDescription,
|
|
13
|
+
DialogFooter,
|
|
14
|
+
DialogHeader,
|
|
15
|
+
DialogTitle,
|
|
16
|
+
DialogTrigger,
|
|
17
|
+
} from "../ui/dialog.js";
|
|
18
|
+
import { Field } from "../ui/field.js";
|
|
19
|
+
import { Input } from "../ui/input.js";
|
|
20
|
+
import { Label } from "../ui/label.js";
|
|
21
|
+
import {
|
|
22
|
+
Select,
|
|
23
|
+
SelectContent,
|
|
24
|
+
SelectItem,
|
|
25
|
+
SelectTrigger,
|
|
26
|
+
SelectValue,
|
|
27
|
+
} from "../ui/select.js";
|
|
28
|
+
import { Switch } from "../ui/switch.js";
|
|
29
|
+
|
|
30
|
+
export interface ProjectCreateDialogProps {
|
|
31
|
+
/** Override the trigger button. */
|
|
32
|
+
trigger?: ReactNode;
|
|
33
|
+
onCreated?: (project: { id: string; name: string; slug: string }) => void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const ProjectCreateDialog = ({ trigger, onCreated }: ProjectCreateDialogProps) => {
|
|
37
|
+
const client = useUmClient();
|
|
38
|
+
const [open, setOpen] = useState(false);
|
|
39
|
+
const [name, setName] = useState("");
|
|
40
|
+
const [slug, setSlug] = useState("");
|
|
41
|
+
const [tenancyMode, setTenancyMode] = useState<TenancyMode>("multi");
|
|
42
|
+
const [allowSignup, setAllowSignup] = useState(false);
|
|
43
|
+
const [allowTenantCreation, setAllowTenantCreation] = useState(false);
|
|
44
|
+
const [loading, setLoading] = useState(false);
|
|
45
|
+
const [error, setError] = useState<string | null>(null);
|
|
46
|
+
|
|
47
|
+
const reset = () => {
|
|
48
|
+
setName("");
|
|
49
|
+
setSlug("");
|
|
50
|
+
setTenancyMode("multi");
|
|
51
|
+
setAllowSignup(false);
|
|
52
|
+
setAllowTenantCreation(false);
|
|
53
|
+
setError(null);
|
|
54
|
+
setLoading(false);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const submit = async (e: FormEvent) => {
|
|
58
|
+
e.preventDefault();
|
|
59
|
+
setLoading(true);
|
|
60
|
+
setError(null);
|
|
61
|
+
const res = await client.call(createProject, {
|
|
62
|
+
name: name.trim(),
|
|
63
|
+
slug: slug.trim() || undefined,
|
|
64
|
+
tenancyMode,
|
|
65
|
+
allowSignup,
|
|
66
|
+
allowTenantCreation,
|
|
67
|
+
});
|
|
68
|
+
setLoading(false);
|
|
69
|
+
if (res.ok) {
|
|
70
|
+
onCreated?.({ id: res.data.id, name: res.data.name, slug: res.data.slug });
|
|
71
|
+
reset();
|
|
72
|
+
setOpen(false);
|
|
73
|
+
} else {
|
|
74
|
+
setError(errorMessage(res.error, "Couldn't create product."));
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
return (
|
|
79
|
+
<Dialog
|
|
80
|
+
open={open}
|
|
81
|
+
onOpenChange={(o) => {
|
|
82
|
+
setOpen(o);
|
|
83
|
+
if (!o) reset();
|
|
84
|
+
}}
|
|
85
|
+
>
|
|
86
|
+
<DialogTrigger asChild>
|
|
87
|
+
{trigger ?? (
|
|
88
|
+
<Button>
|
|
89
|
+
<Plus />
|
|
90
|
+
New product
|
|
91
|
+
</Button>
|
|
92
|
+
)}
|
|
93
|
+
</DialogTrigger>
|
|
94
|
+
<DialogContent>
|
|
95
|
+
<DialogHeader>
|
|
96
|
+
<DialogTitle>Create a new product</DialogTitle>
|
|
97
|
+
<DialogDescription>
|
|
98
|
+
Each product is an isolated user-management space (project) with
|
|
99
|
+
its own tenancy configuration.
|
|
100
|
+
</DialogDescription>
|
|
101
|
+
</DialogHeader>
|
|
102
|
+
<form onSubmit={submit} className="grid gap-4" noValidate>
|
|
103
|
+
<Field label="Name" htmlFor="project-name" required>
|
|
104
|
+
<Input
|
|
105
|
+
id="project-name"
|
|
106
|
+
autoFocus
|
|
107
|
+
required
|
|
108
|
+
value={name}
|
|
109
|
+
onChange={(e) => setName(e.target.value)}
|
|
110
|
+
placeholder="Digis"
|
|
111
|
+
/>
|
|
112
|
+
</Field>
|
|
113
|
+
<Field
|
|
114
|
+
label="Slug"
|
|
115
|
+
htmlFor="project-slug"
|
|
116
|
+
description="URL-safe identifier. Leave blank to derive from the name."
|
|
117
|
+
>
|
|
118
|
+
<Input
|
|
119
|
+
id="project-slug"
|
|
120
|
+
value={slug}
|
|
121
|
+
onChange={(e) => setSlug(e.target.value.toLowerCase().replace(/\s+/g, "-"))}
|
|
122
|
+
placeholder="digis"
|
|
123
|
+
/>
|
|
124
|
+
</Field>
|
|
125
|
+
<Field
|
|
126
|
+
label="Tenancy"
|
|
127
|
+
htmlFor="project-tenancy"
|
|
128
|
+
description="Single keeps the product to exactly one tenant; multi allows any number."
|
|
129
|
+
>
|
|
130
|
+
<Select
|
|
131
|
+
value={tenancyMode}
|
|
132
|
+
onValueChange={(v) => setTenancyMode(v as TenancyMode)}
|
|
133
|
+
>
|
|
134
|
+
<SelectTrigger id="project-tenancy">
|
|
135
|
+
<SelectValue />
|
|
136
|
+
</SelectTrigger>
|
|
137
|
+
<SelectContent>
|
|
138
|
+
<SelectItem value="multi">Multi-tenant</SelectItem>
|
|
139
|
+
<SelectItem value="single">Single tenant</SelectItem>
|
|
140
|
+
</SelectContent>
|
|
141
|
+
</Select>
|
|
142
|
+
</Field>
|
|
143
|
+
<div className="flex items-center justify-between gap-4">
|
|
144
|
+
<div className="grid gap-0.5">
|
|
145
|
+
<Label htmlFor="project-allow-signup">Open sign-up</Label>
|
|
146
|
+
<p className="text-muted-foreground text-xs">
|
|
147
|
+
Allow self-service sign-up scoped to this product.
|
|
148
|
+
</p>
|
|
149
|
+
</div>
|
|
150
|
+
<Switch
|
|
151
|
+
id="project-allow-signup"
|
|
152
|
+
checked={allowSignup}
|
|
153
|
+
onCheckedChange={setAllowSignup}
|
|
154
|
+
/>
|
|
155
|
+
</div>
|
|
156
|
+
<div className="flex items-center justify-between gap-4">
|
|
157
|
+
<div className="grid gap-0.5">
|
|
158
|
+
<Label htmlFor="project-allow-tenants">Self-serve tenants</Label>
|
|
159
|
+
<p className="text-muted-foreground text-xs">
|
|
160
|
+
Allow any authenticated user to create a tenant inside this
|
|
161
|
+
product.
|
|
162
|
+
</p>
|
|
163
|
+
</div>
|
|
164
|
+
<Switch
|
|
165
|
+
id="project-allow-tenants"
|
|
166
|
+
checked={allowTenantCreation}
|
|
167
|
+
onCheckedChange={setAllowTenantCreation}
|
|
168
|
+
/>
|
|
169
|
+
</div>
|
|
170
|
+
{error ? (
|
|
171
|
+
<Alert variant="destructive">
|
|
172
|
+
<AlertCircle />
|
|
173
|
+
<AlertDescription>{error}</AlertDescription>
|
|
174
|
+
</Alert>
|
|
175
|
+
) : null}
|
|
176
|
+
<DialogFooter>
|
|
177
|
+
<Button
|
|
178
|
+
type="button"
|
|
179
|
+
variant="ghost"
|
|
180
|
+
onClick={() => setOpen(false)}
|
|
181
|
+
disabled={loading}
|
|
182
|
+
>
|
|
183
|
+
Cancel
|
|
184
|
+
</Button>
|
|
185
|
+
<Button type="submit" loading={loading} disabled={!name.trim()}>
|
|
186
|
+
Create product
|
|
187
|
+
</Button>
|
|
188
|
+
</DialogFooter>
|
|
189
|
+
</form>
|
|
190
|
+
</DialogContent>
|
|
191
|
+
</Dialog>
|
|
192
|
+
);
|
|
193
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { getProject } from "@nexys/user-management-client";
|
|
2
|
+
|
|
3
|
+
import { useCall } from "../../hooks/use-call.js";
|
|
4
|
+
import { Tabs, TabsContent, TabsList, TabsTrigger } from "../ui/tabs.js";
|
|
5
|
+
import { ProjectConsents } from "./project-consents.js";
|
|
6
|
+
import { ProjectEmailTemplates } from "./project-email-templates.js";
|
|
7
|
+
import { ProjectNotices } from "./project-notices.js";
|
|
8
|
+
import { ProjectSettings } from "./project-settings.js";
|
|
9
|
+
import { ProjectTenants } from "./project-tenants.js";
|
|
10
|
+
|
|
11
|
+
export interface ProjectDetailTabsProps {
|
|
12
|
+
projectId: string;
|
|
13
|
+
canManage?: boolean;
|
|
14
|
+
canDelete?: boolean;
|
|
15
|
+
onDeleted?: () => void;
|
|
16
|
+
onSelectTenant?: (id: string) => void;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const ProjectDetailTabs = ({
|
|
20
|
+
projectId,
|
|
21
|
+
canManage = false,
|
|
22
|
+
canDelete = false,
|
|
23
|
+
onDeleted,
|
|
24
|
+
onSelectTenant,
|
|
25
|
+
}: ProjectDetailTabsProps) => {
|
|
26
|
+
// The email tab opens on the product's default locale.
|
|
27
|
+
const { data } = useCall(getProject, { projectId });
|
|
28
|
+
|
|
29
|
+
return (
|
|
30
|
+
<Tabs defaultValue="tenants">
|
|
31
|
+
<TabsList>
|
|
32
|
+
<TabsTrigger value="tenants">Tenants</TabsTrigger>
|
|
33
|
+
<TabsTrigger value="emails">Emails</TabsTrigger>
|
|
34
|
+
<TabsTrigger value="consents">Consents</TabsTrigger>
|
|
35
|
+
<TabsTrigger value="notices">Notices</TabsTrigger>
|
|
36
|
+
<TabsTrigger value="settings">Settings</TabsTrigger>
|
|
37
|
+
</TabsList>
|
|
38
|
+
<TabsContent value="tenants" className="mt-4">
|
|
39
|
+
<ProjectTenants projectId={projectId} onSelectTenant={onSelectTenant} />
|
|
40
|
+
</TabsContent>
|
|
41
|
+
<TabsContent value="emails" className="mt-4">
|
|
42
|
+
<ProjectEmailTemplates
|
|
43
|
+
projectId={projectId}
|
|
44
|
+
defaultLocale={data?.defaultLocale ?? "en"}
|
|
45
|
+
canManage={canManage}
|
|
46
|
+
/>
|
|
47
|
+
</TabsContent>
|
|
48
|
+
<TabsContent value="consents" className="mt-4">
|
|
49
|
+
<ProjectConsents projectId={projectId} canManage={canManage} />
|
|
50
|
+
</TabsContent>
|
|
51
|
+
<TabsContent value="notices" className="mt-4">
|
|
52
|
+
<ProjectNotices projectId={projectId} canManage={canManage} />
|
|
53
|
+
</TabsContent>
|
|
54
|
+
<TabsContent value="settings" className="mt-4">
|
|
55
|
+
<ProjectSettings
|
|
56
|
+
projectId={projectId}
|
|
57
|
+
canManage={canManage}
|
|
58
|
+
canDelete={canDelete}
|
|
59
|
+
onDeleted={onDeleted}
|
|
60
|
+
/>
|
|
61
|
+
</TabsContent>
|
|
62
|
+
</Tabs>
|
|
63
|
+
);
|
|
64
|
+
};
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import {
|
|
2
|
+
deleteProjectEmailTemplate,
|
|
3
|
+
listProjectEmailTemplates,
|
|
4
|
+
saveProjectEmailTemplate,
|
|
5
|
+
type EmailTemplateRow,
|
|
6
|
+
} from "@nexys/user-management-client";
|
|
7
|
+
import { AlertCircle, Check, RotateCcw, Save } from "lucide-react";
|
|
8
|
+
import { useEffect, useState, type FormEvent } from "react";
|
|
9
|
+
|
|
10
|
+
import { useUmClient } from "../../context/auth-context.js";
|
|
11
|
+
import { useCall } from "../../hooks/use-call.js";
|
|
12
|
+
import { errorMessage } from "../../lib/error-message.js";
|
|
13
|
+
import { Alert, AlertDescription } from "../ui/alert.js";
|
|
14
|
+
import { Badge } from "../ui/badge.js";
|
|
15
|
+
import { Button } from "../ui/button.js";
|
|
16
|
+
import { Field } from "../ui/field.js";
|
|
17
|
+
import { Input } from "../ui/input.js";
|
|
18
|
+
import { Label } from "../ui/label.js";
|
|
19
|
+
import { Skeleton } from "../ui/skeleton.js";
|
|
20
|
+
import { Textarea } from "../ui/textarea.js";
|
|
21
|
+
|
|
22
|
+
const TEMPLATE_LABELS: Record<string, string> = {
|
|
23
|
+
verification: "Verify email address",
|
|
24
|
+
magic_link: "Magic sign-in link",
|
|
25
|
+
password_reset: "Password reset",
|
|
26
|
+
invitation: "Tenant invitation",
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const SOURCE_LABELS: Record<EmailTemplateRow["source"], string> = {
|
|
30
|
+
project: "customised",
|
|
31
|
+
global: "inherited (global default)",
|
|
32
|
+
builtin: "inherited (built-in)",
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export interface ProjectEmailTemplatesProps {
|
|
36
|
+
projectId: string;
|
|
37
|
+
/** The product's default locale — the variant shown first. */
|
|
38
|
+
defaultLocale: string;
|
|
39
|
+
canManage?: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Per-product editor for the transactional emails. Each template shows
|
|
44
|
+
* the copy that will actually be sent plus where it comes from, so an
|
|
45
|
+
* owner can override it or reset back to what it inherits.
|
|
46
|
+
*/
|
|
47
|
+
export const ProjectEmailTemplates = ({
|
|
48
|
+
projectId,
|
|
49
|
+
defaultLocale,
|
|
50
|
+
canManage = false,
|
|
51
|
+
}: ProjectEmailTemplatesProps) => {
|
|
52
|
+
const [locale, setLocale] = useState(defaultLocale);
|
|
53
|
+
const { data, loading, error, refresh } = useCall(listProjectEmailTemplates, {
|
|
54
|
+
projectId,
|
|
55
|
+
locale,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// The product's default locale changed underneath us (settings saved).
|
|
59
|
+
useEffect(() => setLocale(defaultLocale), [defaultLocale]);
|
|
60
|
+
|
|
61
|
+
if (error) {
|
|
62
|
+
return (
|
|
63
|
+
<Alert variant="destructive">
|
|
64
|
+
<AlertCircle />
|
|
65
|
+
<AlertDescription>{errorMessage(error)}</AlertDescription>
|
|
66
|
+
</Alert>
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return (
|
|
71
|
+
<div className="grid gap-4">
|
|
72
|
+
<LocalePicker
|
|
73
|
+
value={locale}
|
|
74
|
+
defaultLocale={defaultLocale}
|
|
75
|
+
onChange={setLocale}
|
|
76
|
+
/>
|
|
77
|
+
{loading ? (
|
|
78
|
+
<>
|
|
79
|
+
<Skeleton className="h-28 w-full" />
|
|
80
|
+
<Skeleton className="h-28 w-full" />
|
|
81
|
+
</>
|
|
82
|
+
) : (
|
|
83
|
+
(data?.templates ?? []).map((t) => (
|
|
84
|
+
<TemplateEditor
|
|
85
|
+
// Remount when the stored copy changes underneath the form.
|
|
86
|
+
key={`${t.templateKey}:${t.locale}:${t.updatedAt ?? "builtin"}`}
|
|
87
|
+
projectId={projectId}
|
|
88
|
+
locale={locale}
|
|
89
|
+
template={t}
|
|
90
|
+
canManage={canManage}
|
|
91
|
+
onChanged={refresh}
|
|
92
|
+
/>
|
|
93
|
+
))
|
|
94
|
+
)}
|
|
95
|
+
</div>
|
|
96
|
+
);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Locale of the variant being edited. Free text rather than a fixed
|
|
101
|
+
* list — the service accepts any BCP-47 tag, and a product may ship
|
|
102
|
+
* copy in languages this console doesn't know about.
|
|
103
|
+
*/
|
|
104
|
+
const LocalePicker = ({
|
|
105
|
+
value,
|
|
106
|
+
defaultLocale,
|
|
107
|
+
onChange,
|
|
108
|
+
}: {
|
|
109
|
+
value: string;
|
|
110
|
+
defaultLocale: string;
|
|
111
|
+
onChange: (v: string) => void;
|
|
112
|
+
}) => {
|
|
113
|
+
const [draft, setDraft] = useState(value);
|
|
114
|
+
useEffect(() => setDraft(value), [value]);
|
|
115
|
+
|
|
116
|
+
return (
|
|
117
|
+
<form
|
|
118
|
+
className="flex items-end gap-2"
|
|
119
|
+
onSubmit={(e) => {
|
|
120
|
+
e.preventDefault();
|
|
121
|
+
const next = draft.trim().toLowerCase();
|
|
122
|
+
if (next) onChange(next);
|
|
123
|
+
}}
|
|
124
|
+
>
|
|
125
|
+
<Field
|
|
126
|
+
label="Locale"
|
|
127
|
+
htmlFor="email-template-locale"
|
|
128
|
+
description={`Which language variant to edit. The product default is "${defaultLocale}".`}
|
|
129
|
+
>
|
|
130
|
+
<Input
|
|
131
|
+
id="email-template-locale"
|
|
132
|
+
value={draft}
|
|
133
|
+
onChange={(e) => setDraft(e.target.value)}
|
|
134
|
+
placeholder={defaultLocale}
|
|
135
|
+
className="w-[8rem] font-mono"
|
|
136
|
+
/>
|
|
137
|
+
</Field>
|
|
138
|
+
<Button
|
|
139
|
+
type="submit"
|
|
140
|
+
variant="outline"
|
|
141
|
+
disabled={!draft.trim() || draft.trim().toLowerCase() === value}
|
|
142
|
+
>
|
|
143
|
+
Load
|
|
144
|
+
</Button>
|
|
145
|
+
</form>
|
|
146
|
+
);
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const TemplateEditor = ({
|
|
150
|
+
projectId,
|
|
151
|
+
locale,
|
|
152
|
+
template,
|
|
153
|
+
canManage,
|
|
154
|
+
onChanged,
|
|
155
|
+
}: {
|
|
156
|
+
projectId: string;
|
|
157
|
+
locale: string;
|
|
158
|
+
template: EmailTemplateRow;
|
|
159
|
+
canManage: boolean;
|
|
160
|
+
onChanged: () => void;
|
|
161
|
+
}) => {
|
|
162
|
+
const client = useUmClient();
|
|
163
|
+
const [subject, setSubject] = useState(template.subject);
|
|
164
|
+
const [body, setBody] = useState(template.body);
|
|
165
|
+
const [saving, setSaving] = useState(false);
|
|
166
|
+
const [saved, setSaved] = useState(false);
|
|
167
|
+
const [resetting, setResetting] = useState(false);
|
|
168
|
+
const [error, setError] = useState<string | null>(null);
|
|
169
|
+
|
|
170
|
+
const dirty =
|
|
171
|
+
subject.trim() !== template.subject.trim() || body !== template.body;
|
|
172
|
+
const overridden = template.source === "project";
|
|
173
|
+
|
|
174
|
+
const submit = async (e: FormEvent) => {
|
|
175
|
+
e.preventDefault();
|
|
176
|
+
setSaving(true);
|
|
177
|
+
setError(null);
|
|
178
|
+
setSaved(false);
|
|
179
|
+
const res = await client.call(saveProjectEmailTemplate, {
|
|
180
|
+
projectId,
|
|
181
|
+
templateKey: template.templateKey,
|
|
182
|
+
subject: subject.trim(),
|
|
183
|
+
body,
|
|
184
|
+
locale,
|
|
185
|
+
});
|
|
186
|
+
setSaving(false);
|
|
187
|
+
if (res.ok) {
|
|
188
|
+
setSaved(true);
|
|
189
|
+
setTimeout(() => setSaved(false), 2000);
|
|
190
|
+
onChanged();
|
|
191
|
+
} else {
|
|
192
|
+
setError(errorMessage(res.error, "Couldn't save this template."));
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
/** Drop the override so the template falls back to what it inherits. */
|
|
197
|
+
const resetToInherited = async () => {
|
|
198
|
+
setResetting(true);
|
|
199
|
+
setError(null);
|
|
200
|
+
const res = await client.call(deleteProjectEmailTemplate, {
|
|
201
|
+
projectId,
|
|
202
|
+
templateKey: template.templateKey,
|
|
203
|
+
locale,
|
|
204
|
+
});
|
|
205
|
+
setResetting(false);
|
|
206
|
+
if (res.ok) {
|
|
207
|
+
onChanged();
|
|
208
|
+
} else {
|
|
209
|
+
setError(errorMessage(res.error, "Couldn't reset this template."));
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
return (
|
|
214
|
+
<form
|
|
215
|
+
onSubmit={submit}
|
|
216
|
+
className="grid gap-3 rounded-lg border bg-card p-4"
|
|
217
|
+
noValidate
|
|
218
|
+
>
|
|
219
|
+
<div className="flex flex-wrap items-center justify-between gap-2">
|
|
220
|
+
<div className="grid gap-0.5">
|
|
221
|
+
<Label className="text-sm font-medium">
|
|
222
|
+
{TEMPLATE_LABELS[template.templateKey] ?? template.templateKey}
|
|
223
|
+
</Label>
|
|
224
|
+
<code className="text-muted-foreground font-mono text-xs">
|
|
225
|
+
{template.templateKey}
|
|
226
|
+
</code>
|
|
227
|
+
</div>
|
|
228
|
+
<Badge variant={overridden ? "default" : "muted"}>
|
|
229
|
+
{SOURCE_LABELS[template.source]}
|
|
230
|
+
</Badge>
|
|
231
|
+
</div>
|
|
232
|
+
|
|
233
|
+
{error ? (
|
|
234
|
+
<Alert variant="destructive">
|
|
235
|
+
<AlertCircle />
|
|
236
|
+
<AlertDescription>{error}</AlertDescription>
|
|
237
|
+
</Alert>
|
|
238
|
+
) : null}
|
|
239
|
+
|
|
240
|
+
<Field label="Subject" htmlFor={`subject-${template.templateKey}`}>
|
|
241
|
+
<Input
|
|
242
|
+
id={`subject-${template.templateKey}`}
|
|
243
|
+
value={subject}
|
|
244
|
+
onChange={(e) => setSubject(e.target.value)}
|
|
245
|
+
disabled={!canManage}
|
|
246
|
+
/>
|
|
247
|
+
</Field>
|
|
248
|
+
<Field
|
|
249
|
+
label="Body"
|
|
250
|
+
htmlFor={`body-${template.templateKey}`}
|
|
251
|
+
description={`Plain text. Available placeholders: ${template.variables
|
|
252
|
+
.map((v) => `{{${v}}}`)
|
|
253
|
+
.join(", ")}`}
|
|
254
|
+
>
|
|
255
|
+
<Textarea
|
|
256
|
+
id={`body-${template.templateKey}`}
|
|
257
|
+
value={body}
|
|
258
|
+
onChange={(e) => setBody(e.target.value)}
|
|
259
|
+
rows={8}
|
|
260
|
+
disabled={!canManage}
|
|
261
|
+
className="font-mono text-xs"
|
|
262
|
+
/>
|
|
263
|
+
</Field>
|
|
264
|
+
|
|
265
|
+
{canManage ? (
|
|
266
|
+
<div className="flex flex-wrap items-center gap-2">
|
|
267
|
+
<Button type="submit" loading={saving} disabled={!dirty}>
|
|
268
|
+
{saved ? <Check /> : <Save />}
|
|
269
|
+
{saved ? "Saved" : "Save"}
|
|
270
|
+
</Button>
|
|
271
|
+
{overridden ? (
|
|
272
|
+
<Button
|
|
273
|
+
type="button"
|
|
274
|
+
variant="ghost"
|
|
275
|
+
loading={resetting}
|
|
276
|
+
onClick={() => void resetToInherited()}
|
|
277
|
+
>
|
|
278
|
+
<RotateCcw /> Reset to inherited
|
|
279
|
+
</Button>
|
|
280
|
+
) : null}
|
|
281
|
+
</div>
|
|
282
|
+
) : null}
|
|
283
|
+
</form>
|
|
284
|
+
);
|
|
285
|
+
};
|