| 1 | 'use client' |
| 2 | |
| 3 | import { useRouter } from '@bprogress/next/app' |
| 4 | import { zodResolver } |
| 5 | import { Save } |
| 6 | import { useAction } |
| 7 | import { useState } |
| 8 | import { Controller, useForm |
| 9 | import { toast } |
| 10 | import type { z |
| 11 | import { Button } |
| 12 | import { |
| 13 | Card, |
| 14 | CardContent, |
| 15 | CardDescription, |
| 16 | CardFooter, |
| 17 | CardHeader, |
| 18 | CardTitle, |
| 19 | } from '@/components/ui/card' |
| 20 | import { Checkbox } |
| 21 | import { |
| 22 | Field, |
| 23 | FieldDescription, |
| 24 | FieldError, |
| 25 | FieldGroup, |
| 26 | FieldLabel, |
| 27 | } from '@/components/ui/field' |
| 28 | import { Input } |
| 29 | import { renameOrganizationAction } |
| 30 | import { renameOrganizationSchema } |
| 31 | |
| 32 | type FormValues |
| 33 | |
| 34 | export function GeneralCard({ |
| 35 | organization, |
| 36 | orgSlug, |
| 37 | }: { |
| 38 | organization: { |
| 39 | orgSlug: string |
| 40 | }) { |
| 41 | const router = |
| 42 | |
| 43 | const form = |
| 44 | resolver: zodResolver(renameOrganizationSchema |
| 45 | defaultValues: { |
| 46 | organizationId: organization.id |
| 47 | name: organization.name |
| 48 | slug: organization.slug |
| 49 | }, |
| 50 | }) |
| 51 | |
| 52 | const [slugAcknowledged, |
| 53 | const watchedSlug = |
| 54 | const slugChanged = |
| 55 | |
| 56 | const { execute, |
| 57 | onSuccess({ data |
| 58 | toast.success('Workspace updated |
| 59 | if (data?.slug |
| 60 | router.push(`/ |
| 61 | } else { |
| 62 | router.refresh() |
| 63 | } |
| 64 | }, |
| 65 | onError({ error |
| 66 | toast.error(error |
| 67 | }, |
| 68 | }) |
| 69 | |
| 70 | function onSubmit(data |
| 71 | execute({ |
| 72 | organizationId: data.organizationId |
| 73 | name: data.name |
| 74 | slug: data.slug |
| 75 | }) |
| 76 | } |
| 77 | |
| 78 | return ( |
| 79 | <Card> |
| 80 | <CardHeader> |
| 81 | <CardTitle>General</CardTitle |
| 82 | <CardDescription> |
| 83 | Manage your workspace name and URL slug. |
| 84 | </CardDescription> |
| 85 | </CardHeader> |
| 86 | <form onSubmit={form |
| 87 | <CardContent> |
| 88 | <FieldGroup> |
| 89 | <Controller |
| 90 | control={form.control |
| 91 | name='name' |
| 92 | render={({ field |
| 93 | <Field className=' |
| 94 | <FieldLabel>Workspace Name</FieldLabel |
| 95 | <Input |
| 96 | {...field} |
| 97 | aria-invalid={fieldState.invalid |
| 98 | placeholder='My Workspace' |
| 99 | /> |
| 100 | {fieldState.invalid && |
| 101 | <FieldError errors={ |
| 102 | )} |
| 103 | </Field> |
| 104 | )} |
| 105 | /> |
| 106 | <Controller |
| 107 | control={form.control |
| 108 | name='slug' |
| 109 | render={({ field |
| 110 | <Field className=' |
| 111 | <FieldLabel>URL Slug</FieldLabel |
| 112 | <Input |
| 113 | {...field} |
| 114 | aria-invalid={fieldState.invalid |
| 115 | onChange={(e) |
| 116 | const newSlug = |
| 117 | .toLowerCase() |
| 118 | .replace(/[^a-z0-9 |
| 119 | field.onChange(newSlug |
| 120 | if (newSlug === organization |
| 121 | setSlugAcknowledged(false) |
| 122 | } |
| 123 | }} |
| 124 | placeholder='my-workspace' |
| 125 | /> |
| 126 | <FieldDescription> |
| 127 | Your workspace will be accessible at /{field.value} |
| 128 | </FieldDescription> |
| 129 | {fieldState.invalid && |
| 130 | <FieldError errors={ |
| 131 | )} |
| 132 | </Field> |
| 133 | )} |
| 134 | /> |
| 135 | {slugChanged && ( |
| 136 | <div className='rounded-lg border border-amber-500/50 bg-amber-500/5 p-3 |
| 137 | <div className='flex items-start gap-3 |
| 138 | <Checkbox |
| 139 | checked={slugAcknowledged} |
| 140 | id='org-slug-ack' |
| 141 | onCheckedChange={(checked) |
| 142 | setSlugAcknowledged(checked === |
| 143 | } |
| 144 | /> |
| 145 | <label |
| 146 | className='text-sm leading-snug' |
| 147 | htmlFor='org-slug-ack' |
| 148 | > |
| 149 | I acknowledge that by changing the slug of workspace, links |
| 150 | in previously sent emails and notifications will stop |
| 151 | working, as they still point to{' '} |
| 152 | <span className='font-medium |
| 153 | </label> |
| 154 | </div> |
| 155 | </div> |
| 156 | )} |
| 157 | </FieldGroup> |
| 158 | </CardContent> |
| 159 | <CardFooter> |
| 160 | <Button |
| 161 | className='mt-4' |
| 162 | disabled={ |
| 163 | !form.formState.isDirty |
| 164 | } |
| 165 | loading={isPending} |
| 166 | type='submit' |
| 167 | > |
| 168 | <Save className=' |
| 169 | Save Changes |
| 170 | </Button> |
| 171 | </CardFooter> |
| 172 | </form> |
| 173 | </Card> |
| 174 | ) |
| 175 | } |