|
| 1 | +import { useState } from "react"; |
| 2 | +import { toast } from "sonner"; |
| 3 | + |
| 4 | +import { trpc } from "~/api/trpc"; |
| 5 | +import { Button } from "~/components/ui/button"; |
| 6 | +import { |
| 7 | + Card, |
| 8 | + CardContent, |
| 9 | + CardDescription, |
| 10 | + CardHeader, |
| 11 | + CardTitle, |
| 12 | +} from "~/components/ui/card"; |
| 13 | +import { |
| 14 | + Field, |
| 15 | + FieldContent, |
| 16 | + FieldDescription, |
| 17 | + FieldError, |
| 18 | + FieldGroup, |
| 19 | + FieldLabel, |
| 20 | +} from "~/components/ui/field"; |
| 21 | +import { Input } from "~/components/ui/input"; |
| 22 | +import { useWorkspace } from "~/components/WorkspaceProvider"; |
| 23 | + |
| 24 | +export default function GeneralSettingsPage() { |
| 25 | + const { workspace } = useWorkspace(); |
| 26 | + const [name, setName] = useState(workspace.name); |
| 27 | + const [slug, setSlug] = useState(workspace.slug); |
| 28 | + const [errors, setErrors] = useState<{ |
| 29 | + name?: string; |
| 30 | + slug?: string; |
| 31 | + }>({}); |
| 32 | + |
| 33 | + const utils = trpc.useUtils(); |
| 34 | + const updateWorkspace = trpc.workspace.update.useMutation({ |
| 35 | + onSuccess: () => { |
| 36 | + toast.success("Workspace updated successfully"); |
| 37 | + // Invalidate the user session to refetch the updated workspace info |
| 38 | + void utils.user.session.invalidate(); |
| 39 | + }, |
| 40 | + onError: (error: unknown) => { |
| 41 | + const message = |
| 42 | + error instanceof Error ? error.message : "Failed to update workspace"; |
| 43 | + toast.error(message); |
| 44 | + }, |
| 45 | + }); |
| 46 | + |
| 47 | + const validateForm = () => { |
| 48 | + const newErrors: { name?: string; slug?: string } = {}; |
| 49 | + |
| 50 | + if (!name || name.trim().length === 0) { |
| 51 | + newErrors.name = "Workspace name is required"; |
| 52 | + } else if (name.length > 100) { |
| 53 | + newErrors.name = "Workspace name must be less than 100 characters"; |
| 54 | + } |
| 55 | + |
| 56 | + if (!slug || slug.trim().length === 0) { |
| 57 | + newErrors.slug = "Workspace slug is required"; |
| 58 | + } else if (!/^[a-z0-9-]+$/.test(slug)) { |
| 59 | + newErrors.slug = |
| 60 | + "Workspace slug can only contain lowercase letters, numbers, and hyphens"; |
| 61 | + } else if (slug.length < 3) { |
| 62 | + newErrors.slug = "Workspace slug must be at least 3 characters"; |
| 63 | + } else if (slug.length > 50) { |
| 64 | + newErrors.slug = "Workspace slug must be less than 50 characters"; |
| 65 | + } |
| 66 | + |
| 67 | + setErrors(newErrors); |
| 68 | + return Object.keys(newErrors).length === 0; |
| 69 | + }; |
| 70 | + |
| 71 | + const handleSubmit = (e: React.FormEvent) => { |
| 72 | + e.preventDefault(); |
| 73 | + |
| 74 | + if (!validateForm()) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + const data: { name?: string; slug?: string } = {}; |
| 79 | + if (name !== workspace.name) data.name = name; |
| 80 | + if (slug !== workspace.slug) data.slug = slug; |
| 81 | + |
| 82 | + updateWorkspace.mutate({ |
| 83 | + workspaceId: workspace.id, |
| 84 | + data, |
| 85 | + }); |
| 86 | + }; |
| 87 | + |
| 88 | + const hasChanges = name !== workspace.name || slug !== workspace.slug; |
| 89 | + |
| 90 | + return ( |
| 91 | + <div className="space-y-6"> |
| 92 | + <div> |
| 93 | + <h1 className="text-2xl font-bold">General Settings</h1> |
| 94 | + <p className="mt-1 text-sm text-muted-foreground"> |
| 95 | + Manage your workspace settings |
| 96 | + </p> |
| 97 | + </div> |
| 98 | + |
| 99 | + <Card> |
| 100 | + <CardHeader> |
| 101 | + <CardTitle>Workspace Information</CardTitle> |
| 102 | + <CardDescription> |
| 103 | + Update your workspace name and URL slug |
| 104 | + </CardDescription> |
| 105 | + </CardHeader> |
| 106 | + <CardContent> |
| 107 | + <form onSubmit={handleSubmit}> |
| 108 | + <FieldGroup> |
| 109 | + <Field> |
| 110 | + <FieldLabel htmlFor="workspace-name">Workspace Name</FieldLabel> |
| 111 | + <FieldContent> |
| 112 | + <Input |
| 113 | + id="workspace-name" |
| 114 | + type="text" |
| 115 | + value={name} |
| 116 | + onChange={(e) => setName(e.target.value)} |
| 117 | + placeholder="My Workspace" |
| 118 | + aria-invalid={!!errors.name} |
| 119 | + /> |
| 120 | + <FieldDescription> |
| 121 | + The display name for your workspace |
| 122 | + </FieldDescription> |
| 123 | + {errors.name && <FieldError>{errors.name}</FieldError>} |
| 124 | + </FieldContent> |
| 125 | + </Field> |
| 126 | + |
| 127 | + <Field> |
| 128 | + <FieldLabel htmlFor="workspace-slug">Workspace Slug</FieldLabel> |
| 129 | + <FieldContent> |
| 130 | + <Input |
| 131 | + id="workspace-slug" |
| 132 | + type="text" |
| 133 | + value={slug} |
| 134 | + onChange={(e) => setSlug(e.target.value.toLowerCase())} |
| 135 | + placeholder="my-workspace" |
| 136 | + aria-invalid={!!errors.slug} |
| 137 | + /> |
| 138 | + <FieldDescription> |
| 139 | + The URL-friendly identifier for your workspace. Used in URLs |
| 140 | + like /{slug}/... |
| 141 | + </FieldDescription> |
| 142 | + {errors.slug && <FieldError>{errors.slug}</FieldError>} |
| 143 | + </FieldContent> |
| 144 | + </Field> |
| 145 | + |
| 146 | + <div className="flex justify-end gap-3 pt-2"> |
| 147 | + <Button |
| 148 | + type="button" |
| 149 | + variant="outline" |
| 150 | + onClick={() => { |
| 151 | + setName(workspace.name); |
| 152 | + setSlug(workspace.slug); |
| 153 | + setErrors({}); |
| 154 | + }} |
| 155 | + disabled={!hasChanges || updateWorkspace.isPending} |
| 156 | + > |
| 157 | + Reset |
| 158 | + </Button> |
| 159 | + <Button |
| 160 | + type="submit" |
| 161 | + disabled={!hasChanges || updateWorkspace.isPending} |
| 162 | + > |
| 163 | + {updateWorkspace.isPending ? "Saving..." : "Save Changes"} |
| 164 | + </Button> |
| 165 | + </div> |
| 166 | + </FieldGroup> |
| 167 | + </form> |
| 168 | + </CardContent> |
| 169 | + </Card> |
| 170 | + </div> |
| 171 | + ); |
| 172 | +} |
0 commit comments