Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

Use Shadcn Admin Kit commponents in Settings Page

François Zaninottoe6403c9202eb
diff --git a/registry.json b/registry.json
index 1de27635..f9ee6d6a 100644
--- a/registry.json
+++ b/registry.json
@@ -123,6 +123,10 @@
"path": "src/components/atomic-crm/settings/SettingsPage.tsx",
"type": "registry:component"
},
+ {
+ "path": "src/components/atomic-crm/settings/AppConfigPage.tsx",
+ "type": "registry:component"
+ },
{
"path": "src/components/atomic-crm/sales/index.ts",
"type": "registry:component"
@@ -147,6 +151,18 @@
"path": "src/components/atomic-crm/sales/SaleName.tsx",
"type": "registry:component"
},
+ {
+ "path": "src/components/atomic-crm/root/useConfigurationLoader.ts",
+ "type": "registry:component"
+ },
+ {
+ "path": "src/components/atomic-crm/root/storedConfiguration.ts",
+ "type": "registry:component"
+ },
+ {
+ "path": "src/components/atomic-crm/root/iconRegistry.ts",
+ "type": "registry:component"
+ },
{
"path": "src/components/atomic-crm/root/i18nProvider.tsx",
"type": "registry:component"
diff --git a/src/components/atomic-crm/settings/AppConfigPage.tsx b/src/components/atomic-crm/settings/AppConfigPage.tsx
index a959c695..bed4589a 100644
--- a/src/components/atomic-crm/settings/AppConfigPage.tsx
+++ b/src/components/atomic-crm/settings/AppConfigPage.tsx
@@ -1,21 +1,15 @@
-import { Plus, Save, Trash2 } from "lucide-react";
-import { useDataProvider, useNotify } from "ra-core";
-import { useCallback, useEffect, useState } from "react";
+import { Save } from "lucide-react";
+import { EditBase, Form, useNotify } from "ra-core";
+import { useMemo } from "react";
+import { useFormContext } from "react-hook-form";
import { Button } from "@/components/ui/button";
import { Card, CardContent } from "@/components/ui/card";
-import { Input } from "@/components/ui/input";
-import { Label } from "@/components/ui/label";
import { Separator } from "@/components/ui/separator";
-import {
- Select,
- SelectContent,
- SelectItem,
- SelectTrigger,
- SelectValue,
-} from "@/components/ui/select";
-import { Textarea } from "@/components/ui/textarea";
+import { ArrayInput } from "@/components/admin/array-input";
+import { SelectInput } from "@/components/admin/select-input";
+import { SimpleFormIterator } from "@/components/admin/simple-form-iterator";
+import { TextInput } from "@/components/admin/text-input";
-import type { CrmDataProvider } from "../providers/types";
import {
useConfigurationContext,
useConfigurationUpdater,
@@ -36,94 +30,95 @@ const SECTIONS = [
{ id: "contact-gender", label: "Contact Gender" },
];
+const iconChoices = Object.keys(genderIconRegistry).map((name) => ({
+ id: name,
+ name,
+}));
+
+const splitLines = (text: string): string[] =>
+ text
+ .split("\n")
+ .map((s) => s.trim())
+ .filter(Boolean);
+
+const transformFormValues = (data: Record<string, any>) => ({
+ config: {
+ title: data.title,
+ lightModeLogo: data.lightModeLogo,
+ darkModeLogo: data.darkModeLogo,
+ companySectors: splitLines(data.companySectors),
+ dealCategories: splitLines(data.dealCategories),
+ taskTypes: splitLines(data.taskTypes),
+ dealStages: data.dealStages,
+ dealPipelineStatuses: data.dealPipelineStatuses,
+ noteStatuses: data.noteStatuses,
+ contactGender: data.contactGender,
+ } satisfies StoredConfiguration,
+});
+
export const AppConfigPage = () => {
- const config = useConfigurationContext();
- const dataProvider = useDataProvider<CrmDataProvider>();
const updateConfiguration = useConfigurationUpdater();
const notify = useNotify();
- // Local form state
- const [title, setTitle] = useState(config.title);
- const [lightModeLogo, setLightModeLogo] = useState(config.lightModeLogo);
- const [darkModeLogo, setDarkModeLogo] = useState(config.darkModeLogo);
- const [companySectors, setCompanySectors] = useState(
- config.companySectors.join("\n"),
- );
- const [dealCategories, setDealCategories] = useState(
- config.dealCategories.join("\n"),
+ return (
+ <EditBase
+ resource="configuration"
+ id={1}
+ mutationMode="pessimistic"
+ redirect={false}
+ transform={transformFormValues}
+ mutationOptions={{
+ onSuccess: (data: any) => {
+ updateConfiguration(data.config);
+ notify("Configuration saved successfully");
+ },
+ onError: () => {
+ notify("Failed to save configuration", { type: "error" });
+ },
+ }}
+ >
+ <AppConfigForm />
+ </EditBase>
);
- const [taskTypes, setTaskTypes] = useState(config.taskTypes.join("\n"));
- const [dealStages, setDealStages] = useState(config.dealStages);
- const [dealPipelineStatuses, setDealPipelineStatuses] = useState(
- config.dealPipelineStatuses,
+};
+
+AppConfigPage.path = "/app-settings";
+
+const AppConfigForm = () => {
+ const config = useConfigurationContext();
+
+ const defaultValues = useMemo(
+ () => ({
+ title: config.title,
+ lightModeLogo: config.lightModeLogo,
+ darkModeLogo: config.darkModeLogo,
+ companySectors: config.companySectors.join("\n"),
+ dealCategories: config.dealCategories.join("\n"),
+ taskTypes: config.taskTypes.join("\n"),
+ dealStages: config.dealStages,
+ dealPipelineStatuses: config.dealPipelineStatuses,
+ noteStatuses: config.noteStatuses,
+ contactGender: dehydrateContactGender(config.contactGender),
+ }),
+ [config],
);
- const [noteStatuses, setNoteStatuses] = useState(config.noteStatuses);
- const [contactGender, setContactGender] = useState(
- dehydrateContactGender(config.contactGender),
+
+ return (
+ <Form defaultValues={defaultValues}>
+ <AppConfigFormFields />
+ </Form>
);
- const [saving, setSaving] = useState(false);
+};
- // Sync form state when config changes (e.g. after DB load)
- useEffect(() => {
- setTitle(config.title);
- setLightModeLogo(config.lightModeLogo);
- setDarkModeLogo(config.darkModeLogo);
- setCompanySectors(config.companySectors.join("\n"));
- setDealCategories(config.dealCategories.join("\n"));
- setTaskTypes(config.taskTypes.join("\n"));
- setDealStages(config.dealStages);
- setDealPipelineStatuses(config.dealPipelineStatuses);
- setNoteStatuses(config.noteStatuses);
- setContactGender(dehydrateContactGender(config.contactGender));
- }, [config]);
+const AppConfigFormFields = () => {
+ const {
+ watch,
+ setValue,
+ formState: { isSubmitting },
+ } = useFormContext();
- const handleSave = useCallback(async () => {
- setSaving(true);
- try {
- const storedConfig: StoredConfiguration = {
- title,
- lightModeLogo,
- darkModeLogo,
- companySectors: companySectors
- .split("\n")
- .map((s) => s.trim())
- .filter(Boolean),
- dealCategories: dealCategories
- .split("\n")
- .map((s) => s.trim())
- .filter(Boolean),
- taskTypes: taskTypes
- .split("\n")
- .map((s) => s.trim())
- .filter(Boolean),
- dealStages,
- dealPipelineStatuses,
- noteStatuses,
- contactGender,
- };
- const saved = await dataProvider.updateConfiguration(storedConfig);
- updateConfiguration(saved);
- notify("Configuration saved successfully");
- } catch {
- notify("Failed to save configuration", { type: "error" });
- } finally {
- setSaving(false);
- }
- }, [
- title,
- lightModeLogo,
- darkModeLogo,
- companySectors,
- dealCategories,
- taskTypes,
- dealStages,
- dealPipelineStatuses,
- noteStatuses,
- contactGender,
- dataProvider,
- updateConfiguration,
- notify,
- ]);
+ const dealStages = watch("dealStages");
+ const dealPipelineStatuses: string[] = watch("dealPipelineStatuses") ?? [];
return (
<div className="flex gap-8 mt-8 pb-20">
@@ -157,30 +152,9 @@ export const AppConfigPage = () => {
<h2 className="text-xl font-semibold text-muted-foreground">
Branding
</h2>
- <div className="space-y-2">
- <Label htmlFor="title">App Title</Label>
- <Input
- id="title"
- value={title}
- onChange={(e) => setTitle(e.target.value)}
- />
- </div>
- <div className="space-y-2">
- <Label htmlFor="lightModeLogo">Light Mode Logo URL</Label>
- <Input
- id="lightModeLogo"
- value={lightModeLogo}
- onChange={(e) => setLightModeLogo(e.target.value)}
- />
- </div>
- <div className="space-y-2">
- <Label htmlFor="darkModeLogo">Dark Mode Logo URL</Label>
- <Input
- id="darkModeLogo"
- value={darkModeLogo}
- onChange={(e) => setDarkModeLogo(e.target.value)}
- />
- </div>
+ <TextInput source="title" label="App Title" />
+ <TextInput source="lightModeLogo" label="Light Mode Logo URL" />
+ <TextInput source="darkModeLogo" label="Dark Mode Logo URL" />
</CardContent>
</Card>
@@ -190,11 +164,12 @@ export const AppConfigPage = () => {
<h2 className="text-xl font-semibold text-muted-foreground">
Company Sectors
</h2>
- <p className="text-sm text-muted-foreground">One sector per line</p>
- <Textarea
- value={companySectors}
- onChange={(e) => setCompanySectors(e.target.value)}
+ <TextInput
+ source="companySectors"
+ label={false}
+ multiline
rows={8}
+ helperText="One sector per line"
/>
</CardContent>
</Card>
@@ -205,50 +180,12 @@ export const AppConfigPage = () => {
<h2 className="text-xl font-semibold text-muted-foreground">
Deal Stages
</h2>
- {dealStages.map((stage, index) => (
- <div key={index} className="flex items-center gap-2">
- <Input
- placeholder="Value"
- value={stage.value}
- onChange={(e) => {
- const updated = [...dealStages];
- updated[index] = { ...stage, value: e.target.value };
- setDealStages(updated);
- }}
- className="flex-1"
- />
- <Input
- placeholder="Label"
- value={stage.label}
- onChange={(e) => {
- const updated = [...dealStages];
- updated[index] = { ...stage, label: e.target.value };
- setDealStages(updated);
- }}
- className="flex-1"
- />
- <Button
- variant="ghost"
- size="icon"
- type="button"
- onClick={() =>
- setDealStages(dealStages.filter((_, i) => i !== index))
- }
- >
- <Trash2 className="h-4 w-4" />
- </Button>
- </div>
- ))}
- <Button
- variant="outline"
- size="sm"
- type="button"
- onClick={() =>
- setDealStages([...dealStages, { value: "", label: "" }])
- }
- >
- <Plus className="h-4 w-4 mr-1" /> Add Stage
- </Button>
+ <ArrayInput source="dealStages" label={false}>
+ <SimpleFormIterator inline disableReordering>
+ <TextInput source="value" label={false} placeholder="Value" />
+ <TextInput source="label" label={false} placeholder="Label" />
+ </SimpleFormIterator>
+ </ArrayInput>
<Separator />
@@ -256,34 +193,40 @@ export const AppConfigPage = () => {
Pipeline Statuses
</h3>
<p className="text-sm text-muted-foreground">
- Select which deal stages count as "pipeline" (completed) deals.
+ Select which deal stages count as &quot;pipeline&quot; (completed)
+ deals.
</p>
<div className="flex flex-wrap gap-2">
- {dealStages.map((stage) => {
- const isSelected = dealPipelineStatuses.includes(stage.value);
- return (
- <Button
- key={stage.value}
- type="button"
- variant={isSelected ? "default" : "outline"}
- size="sm"
- onClick={() => {
- if (isSelected) {
- setDealPipelineStatuses(
- dealPipelineStatuses.filter((s) => s !== stage.value),
- );
- } else {
- setDealPipelineStatuses([
- ...dealPipelineStatuses,
- stage.value,
- ]);
- }
- }}
- >
- {stage.label || stage.value}
- </Button>
- );
- })}
+ {dealStages?.map(
+ (stage: { value: string; label: string }, idx: number) => {
+ const isSelected = dealPipelineStatuses.includes(stage.value);
+ return (
+ <Button
+ key={idx}
+ type="button"
+ variant={isSelected ? "default" : "outline"}
+ size="sm"
+ onClick={() => {
+ if (isSelected) {
+ setValue(
+ "dealPipelineStatuses",
+ dealPipelineStatuses.filter(
+ (s) => s !== stage.value,
+ ),
+ );
+ } else {
+ setValue("dealPipelineStatuses", [
+ ...dealPipelineStatuses,
+ stage.value,
+ ]);
+ }
+ }}
+ >
+ {stage.label || stage.value}
+ </Button>
+ );
+ },
+ )}
</div>
</CardContent>
</Card>
@@ -294,13 +237,12 @@ export const AppConfigPage = () => {
<h2 className="text-xl font-semibold text-muted-foreground">
Deal Categories
</h2>
- <p className="text-sm text-muted-foreground">
- One category per line
- </p>
- <Textarea
- value={dealCategories}
- onChange={(e) => setDealCategories(e.target.value)}
+ <TextInput
+ source="dealCategories"
+ label={false}
+ multiline
rows={6}
+ helperText="One category per line"
/>
</CardContent>
</Card>
@@ -311,63 +253,18 @@ export const AppConfigPage = () => {
<h2 className="text-xl font-semibold text-muted-foreground">
Note Statuses
</h2>
- {noteStatuses.map((status, index) => (
- <div key={index} className="flex items-center gap-2">
- <Input
- placeholder="Value"
- value={status.value}
- onChange={(e) => {
- const updated = [...noteStatuses];
- updated[index] = { ...status, value: e.target.value };
- setNoteStatuses(updated);
- }}
- className="flex-1"
- />
- <Input
- placeholder="Label"
- value={status.label}
- onChange={(e) => {
- const updated = [...noteStatuses];
- updated[index] = { ...status, label: e.target.value };
- setNoteStatuses(updated);
- }}
- className="flex-1"
- />
- <Input
+ <ArrayInput source="noteStatuses" label={false}>
+ <SimpleFormIterator inline disableReordering>
+ <TextInput source="value" label={false} placeholder="Value" />
+ <TextInput source="label" label={false} placeholder="Label" />
+ <TextInput
+ source="color"
type="color"
- value={status.color}
- onChange={(e) => {
- const updated = [...noteStatuses];
- updated[index] = { ...status, color: e.target.value };
- setNoteStatuses(updated);
- }}
- className="w-16 p-1 h-9"
+ label={false}
+ inputClassName="w-16 p-1 h-9"
/>
- <Button
- variant="ghost"
- size="icon"
- type="button"
- onClick={() =>
- setNoteStatuses(noteStatuses.filter((_, i) => i !== index))
- }
- >
- <Trash2 className="h-4 w-4" />
- </Button>
- </div>
- ))}
- <Button
- variant="outline"
- size="sm"
- type="button"
- onClick={() =>
- setNoteStatuses([
- ...noteStatuses,
- { value: "", label: "", color: "#808080" },
- ])
- }
- >
- <Plus className="h-4 w-4 mr-1" /> Add Status
- </Button>
+ </SimpleFormIterator>
+ </ArrayInput>
</CardContent>
</Card>
@@ -377,11 +274,12 @@ export const AppConfigPage = () => {
<h2 className="text-xl font-semibold text-muted-foreground">
Task Types
</h2>
- <p className="text-sm text-muted-foreground">One type per line</p>
- <Textarea
- value={taskTypes}
- onChange={(e) => setTaskTypes(e.target.value)}
+ <TextInput
+ source="taskTypes"
+ label={false}
+ multiline
rows={6}
+ helperText="One type per line"
/>
</CardContent>
</Card>
@@ -392,74 +290,18 @@ export const AppConfigPage = () => {
<h2 className="text-xl font-semibold text-muted-foreground">
Contact Gender Options
</h2>
- {contactGender.map((gender, index) => (
- <div key={index} className="flex items-center gap-2">
- <Input
- placeholder="Value"
- value={gender.value}
- onChange={(e) => {
- const updated = [...contactGender];
- updated[index] = { ...gender, value: e.target.value };
- setContactGender(updated);
- }}
- className="flex-1"
+ <ArrayInput source="contactGender" label={false}>
+ <SimpleFormIterator inline disableReordering>
+ <TextInput source="value" label={false} placeholder="Value" />
+ <TextInput source="label" label={false} placeholder="Label" />
+ <SelectInput
+ source="icon"
+ label={false}
+ choices={iconChoices}
+ className="w-36"
/>
- <Input
- placeholder="Label"
- value={gender.label}
- onChange={(e) => {
- const updated = [...contactGender];
- updated[index] = { ...gender, label: e.target.value };
- setContactGender(updated);
- }}
- className="flex-1"
- />
- <Select
- value={gender.icon}
- onValueChange={(val) => {
- const updated = [...contactGender];
- updated[index] = { ...gender, icon: val };
- setContactGender(updated);
- }}
- >
- <SelectTrigger className="w-36">
- <SelectValue />
- </SelectTrigger>
- <SelectContent>
- {Object.keys(genderIconRegistry).map((name) => (
- <SelectItem key={name} value={name}>
- {name}
- </SelectItem>
- ))}
- </SelectContent>
- </Select>
- <Button
- variant="ghost"
- size="icon"
- type="button"
- onClick={() =>
- setContactGender(
- contactGender.filter((_, i) => i !== index),
- )
- }
- >
- <Trash2 className="h-4 w-4" />
- </Button>
- </div>
- ))}
- <Button
- variant="outline"
- size="sm"
- type="button"
- onClick={() =>
- setContactGender([
- ...contactGender,
- { value: "", label: "", icon: "User" },
- ])
- }
- >
- <Plus className="h-4 w-4 mr-1" /> Add Gender Option
- </Button>
+ </SimpleFormIterator>
+ </ArrayInput>
</CardContent>
</Card>
</div>
@@ -469,9 +311,9 @@ export const AppConfigPage = () => {
<div className="max-w-screen-xl mx-auto flex gap-8 px-4">
<div className="hidden md:block w-48 shrink-0" />
<div className="flex-1 min-w-0 max-w-2xl flex justify-end">
- <Button onClick={handleSave} disabled={saving}>
+ <Button type="submit" disabled={isSubmitting}>
<Save className="h-4 w-4 mr-1" />
- {saving ? "Saving..." : "Save Configuration"}
+ {isSubmitting ? "Saving..." : "Save Configuration"}
</Button>
</div>
</div>
@@ -479,5 +321,3 @@ export const AppConfigPage = () => {
</div>
);
};
-
-AppConfigPage.path = "/app-settings";