Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

18.4 KB
Raw Blame
import { zodResolver } from '@hookform/resolvers/zod'
import type { Customer } from '@spree/admin-sdk'
import type { BulkAction, BulkActionFormProps } from '@spree/dashboard-core'
import {
  adminClient,
  ExportButton,
  ImportButton,
  mapSpreeErrorsToForm,
  ResourceMultiAutocomplete,
  ResourceTable,
  resourceSearchSchema,
  Subject,
  TagCombobox,
  useStore,
} from '@spree/dashboard-core'
import {
  BulkDialog,
  Button,
  Checkbox,
  Field,
  FieldError,
  FieldGroup,
  FieldLabel,
  Input,
  RichTextEditor,
  RowActions,
  Sheet,
  SheetContent,
  SheetDescription,
  SheetFooter,
  SheetHeader,
  SheetTitle,
} from '@spree/dashboard-ui'
import { PlusIcon, TagsIcon, UserMinusIcon, UserPlusIcon } from '@spree/dashboard-ui/icons'
import { useMutation } from '@tanstack/react-query'
import { createFileRoute, useNavigate } from '@tanstack/react-router'
import { useState } from 'react'
import { Controller, useForm } from 'react-hook-form'
import { useTranslation } from 'react-i18next'
import { z } from 'zod/v4'
import { ImportWizardDialog } from '../../../../components/spree/imports/import-wizard-dialog'
import {
  importWizardSearchSchema,
  useImportWizardSearch,
} from '../../../../components/spree/imports/import-wizard-search'
import {
  customerGroupAutocompleteProps,
  useCustomerGroups,
} from '../../../../hooks/use-customer-groups'
import {
  useBulkAddCustomersToGroups,
  useBulkAddCustomerTags,
  useBulkRemoveCustomersFromGroups,
  useBulkRemoveCustomerTags,
} from '../../../../hooks/use-customers'
import {
  NEW_CUSTOMER_DEFAULTS,
  type NewCustomerFormValues,
  newCustomerFormSchema,
} from '../../../../schemas/customer'
import '../../../../tables/customers'

// Adds `?new=1` on top of the standard table search schema so the create sheet
// is deep-linkable (`/customers?new=1`) and back-button friendly. `import`
// carries the prefixed id of the import wizard dialog open over the table.
const customersSearchSchema = resourceSearchSchema.extend({
  new: z.coerce.boolean().optional(),
  ...importWizardSearchSchema.shape,
})

export const Route = createFileRoute('/_authenticated/$storeId/customers/')({
  validateSearch: customersSearchSchema,
  component: CustomersPage,
})

type GroupFormValues = { customer_group_ids: string[] }
type TagsFormValues = { tags: string[] }

// Bulk-mutating customers shifts `customers_count` on every affected group, so
// drop the groups cache to force a refetch when the user navigates back.
const GROUP_INVALIDATIONS = [['customer-groups']]

function CustomersPage() {
  const { t } = useTranslation()
  const { storeId } = Route.useParams()
  const search = Route.useSearch() as z.infer<typeof customersSearchSchema>
  const navigate = useNavigate()
  const bulkAddToGroups = useBulkAddCustomersToGroups()
  const bulkRemoveFromGroups = useBulkRemoveCustomersFromGroups()
  const bulkAddTags = useBulkAddCustomerTags()
  const bulkRemoveTags = useBulkRemoveCustomerTags()

  // BulkActionBar's `successMessage` runs through a `{n}` interpolation in the
  // bar itself; we pass i18next `{{count}}` as the literal `{n}` token so the
  // bar can substitute the real count at runtime.
  const bulkActions: BulkAction<GroupFormValues | TagsFormValues>[] = [
    {
      key: 'add-to-groups',
      label: t('admin.customers.groups.bulk_add_action'),
      icon: <UserPlusIcon className="size-4" />,
      subject: Subject.Customer,
      form: (props) => (
        <GroupPickerDialog
          {...(props as BulkActionFormProps<GroupFormValues>)}
          title={t('admin.customers.groups.picker.add_title')}
          description={t('admin.customers.groups.picker.add_description')}
          submitLabel={t('admin.actions.add')}
        />
      ),
      run: ({ ids, formValues }) =>
        bulkAddToGroups.mutateAsync({
          ids,
          customer_group_ids: (formValues as GroupFormValues).customer_group_ids ?? [],
        }),
      invalidate: GROUP_INVALIDATIONS,
      successMessage: t('admin.customers.groups.bulk_added', { count: '{n}' as unknown as number }),
      errorMessage: t('admin.customers.groups.bulk_add_failed'),
    },
    {
      key: 'remove-from-groups',
      label: t('admin.customers.groups.bulk_remove_action'),
      icon: <UserMinusIcon className="size-4" />,
      subject: Subject.Customer,
      form: (props) => (
        <GroupPickerDialog
          {...(props as BulkActionFormProps<GroupFormValues>)}
          title={t('admin.customers.groups.picker.remove_title')}
          description={t('admin.customers.groups.picker.remove_description')}
          submitLabel={t('admin.actions.remove')}
        />
      ),
      run: ({ ids, formValues }) =>
        bulkRemoveFromGroups.mutateAsync({
          ids,
          customer_group_ids: (formValues as GroupFormValues).customer_group_ids ?? [],
        }),
      invalidate: GROUP_INVALIDATIONS,
      successMessage: t('admin.customers.groups.bulk_removed', {
        count: '{n}' as unknown as number,
      }),
      errorMessage: t('admin.customers.groups.bulk_remove_failed'),
    },
    {
      key: 'add-tags',
      label: t('admin.customers.tags.bulk_add_action'),
      icon: <TagsIcon className="size-4" />,
      subject: Subject.Customer,
      form: (props) => (
        <TagPickerDialog
          {...(props as BulkActionFormProps<TagsFormValues>)}
          title={t('admin.customers.tags.picker.add_title')}
          description={t('admin.customers.tags.picker.add_description')}
          submitLabel={t('admin.actions.add')}
        />
      ),
      run: ({ ids, formValues }) =>
        bulkAddTags.mutateAsync({ ids, tags: (formValues as TagsFormValues).tags ?? [] }),
      successMessage: t('admin.customers.tags.bulk_added'),
      errorMessage: t('admin.customers.tags.bulk_add_failed'),
    },
    {
      key: 'remove-tags',
      label: t('admin.customers.tags.bulk_remove_action'),
      icon: <TagsIcon className="size-4" />,
      subject: Subject.Customer,
      form: (props) => (
        <TagPickerDialog
          {...(props as BulkActionFormProps<TagsFormValues>)}
          title={t('admin.customers.tags.picker.remove_title')}
          description={t('admin.customers.tags.picker.remove_description')}
          submitLabel={t('admin.actions.remove')}
        />
      ),
      run: ({ ids, formValues }) =>
        bulkRemoveTags.mutateAsync({ ids, tags: (formValues as TagsFormValues).tags ?? [] }),
      successMessage: t('admin.customers.tags.bulk_removed'),
      errorMessage: t('admin.customers.tags.bulk_remove_failed'),
    },
  ]

  const isCreating = !!search.new

  function openCreate() {
    navigate({ search: (prev: Record<string, unknown>) => ({ ...prev, new: true }) as never })
  }

  function closeSheet() {
    navigate({
      search: (prev: Record<string, unknown>) => {
        const { new: _n, ...rest } = prev
        return rest as never
      },
    })
  }

  const wizard = useImportWizardSearch(search)

  return (
    <>
      <ResourceTable<Customer>
        tableKey="customers"
        queryKey="customers"
        queryFn={(params) => adminClient.customers.list(params)}
        searchParams={search}
        // Erased accounts stay out of the list unless asked for. They keep
        // their orders, so the row still means something, but nobody can be
        // reached through it and leaving them in makes every search noisier.
        // The Erased filter overrides this: filters are applied over
        // the defaults.
        defaultParams={{ expand: ['customer_groups'], anonymized: 'false' }}
        bulkActions={bulkActions}
        rowActions={(customer) => <CustomerRowActions customer={customer} storeId={storeId} />}
        actions={(ctx) => (
          <>
            <ImportButton
              type="customers"
              subject={Subject.Customer}
              onCreated={(imp) => wizard.open(imp.id)}
            />
            <ExportButton type="customers" {...ctx} />
            <Button size="sm" className="h-[2.125rem]" onClick={openCreate}>
              <PlusIcon className="size-4" />
              {t('admin.pages.customers.new_cta')}
            </Button>
          </>
        )}
      />
      {isCreating && <NewCustomerSheet open onOpenChange={(o) => !o && closeSheet()} />}
      <ImportWizardDialog importId={wizard.importId} onClose={wizard.close} />
    </>
  )
}

/**
 * Per-row Edit kebab.
 *
 * No delete. Destroying the row and erasing the person were two destructive
 * actions that never both applied — deletion is refused once someone has
 * bought anything, which is exactly when a real erasure request arrives — and
 * the milder-sounding one was the destructive one, taking store credit and
 * gift cards with it. Erasing is on the customer's own page, where the record
 * being erased is in front of you.
 */
function CustomerRowActions({ customer, storeId }: { customer: Customer; storeId: string }) {
  const navigate = useNavigate()

  return (
    <RowActions
      actions={[
        {
          key: 'edit',
          onSelect: () =>
            navigate({
              to: '/$storeId/customers/$customerId',
              params: { storeId, customerId: customer.id },
            }),
        },
      ]}
    />
  )
}

/**
 * Multi-select group picker used by the bulk add/remove actions. Resolves
 * with `{ customer_group_ids }` so the same component drives both verbs;
 * the caller supplies the title/description/submit-label copy.
 */
function GroupPickerDialog({
  onSubmit,
  onCancel,
  title,
  description,
  submitLabel,
}: BulkActionFormProps<GroupFormValues> & {
  title: string
  description: string
  submitLabel: string
}) {
  const { t } = useTranslation()
  const { storeId } = useStore()
  const [groupIds, setGroupIds] = useState<string[]>([])
  // Surface the store's customer groups on focus so the merchant doesn't have
  // to type to discover them. The list is small and already cached by
  // +useCustomerGroups+ (5-min stale time).
  const { data: groupsData } = useCustomerGroups()

  return (
    <BulkDialog
      title={title}
      description={description}
      submitLabel={submitLabel}
      submitDisabled={groupIds.length === 0}
      onCancel={onCancel}
      onSubmit={() => onSubmit({ customer_group_ids: groupIds })}
    >
      <Field>
        <FieldLabel>{t('admin.fields.customer.customer_groups.label')}</FieldLabel>
        <ResourceMultiAutocomplete
          {...customerGroupAutocompleteProps(`customer-groups-picker-${storeId}`)}
          initialItems={groupsData?.data}
          value={groupIds}
          onChange={setGroupIds}
        />
      </Field>
    </BulkDialog>
  )
}

/**
 * Multi-select tag picker used by the bulk add/remove tag actions. Resolves
 * with `{ tags }` (raw tag names) so the same component drives both verbs.
 */
function TagPickerDialog({
  onSubmit,
  onCancel,
  title,
  description,
  submitLabel,
}: BulkActionFormProps<TagsFormValues> & {
  title: string
  description: string
  submitLabel: string
}) {
  const { t } = useTranslation()
  const [tags, setTags] = useState<string[]>([])

  return (
    <BulkDialog
      title={title}
      description={description}
      submitLabel={submitLabel}
      submitDisabled={tags.length === 0}
      onCancel={onCancel}
      onSubmit={() => onSubmit({ tags })}
    >
      <Field>
        <FieldLabel>{t('admin.fields.customer.tags.label')}</FieldLabel>
        <TagCombobox taggableType="customer" value={tags} onChange={setTags} />
      </Field>
    </BulkDialog>
  )
}

// ============================================================================
// Create Sheet
// ============================================================================

function NewCustomerSheet({
  open,
  onOpenChange,
}: {
  open: boolean
  onOpenChange: (open: boolean) => void
}) {
  const { t } = useTranslation()
  const { storeId } = Route.useParams()
  const navigate = useNavigate()

  const form = useForm<NewCustomerFormValues>({
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    resolver: zodResolver(newCustomerFormSchema) as any,
    defaultValues: NEW_CUSTOMER_DEFAULTS,
  })

  const createMutation = useMutation({
    mutationFn: (params: Parameters<typeof adminClient.customers.create>[0]) =>
      adminClient.customers.create(params),
    onSuccess: (customer) => {
      // Land on the new customer's detail page rather than leaving the user on
      // the index — they almost always want to add an address or note next.
      navigate({
        to: '/$storeId/customers/$customerId',
        params: { storeId, customerId: customer.id },
        replace: true,
      })
    },
  })

  async function onSubmit(values: NewCustomerFormValues) {
    const payload: Parameters<typeof adminClient.customers.create>[0] = {
      email: values.email.trim(),
      accepts_email_marketing: values.accepts_email_marketing,
    }
    if (values.first_name.trim()) payload.first_name = values.first_name.trim()
    if (values.last_name.trim()) payload.last_name = values.last_name.trim()
    if (values.phone.trim()) payload.phone = values.phone.trim()
    // The editor emits an empty paragraph once it has been focused and cleared,
    // which is not a note.
    const internalNote = values.internal_note.trim()
    if (internalNote && internalNote !== '<p></p>') payload.internal_note = internalNote
    if (values.tags.length) payload.tags = values.tags

    try {
      await createMutation.mutateAsync(payload)
    } catch (err) {
      if (!mapSpreeErrorsToForm(err, form.setError)) throw err
    }
  }

  return (
    <Sheet open={open} onOpenChange={onOpenChange}>
      <SheetContent>
        <SheetHeader>
          <SheetTitle>{t('admin.pages.customers.new_cta')}</SheetTitle>
          <SheetDescription>{t('admin.customers.new_sheet_description')}</SheetDescription>
        </SheetHeader>
        <form onSubmit={form.handleSubmit(onSubmit)} className="flex min-h-0 flex-1 flex-col">
          <div className="flex flex-1 flex-col gap-4 overflow-y-auto p-4">
            {form.formState.errors.root?.message && (
              <p className="text-sm text-destructive" role="alert">
                {form.formState.errors.root.message}
              </p>
            )}
            <FieldGroup>
              <Field>
                <FieldLabel htmlFor="email">{t('admin.fields.email.label')}</FieldLabel>
                <Input
                  id="email"
                  type="email"
                  autoFocus
                  aria-invalid={!!form.formState.errors.email || undefined}
                  {...form.register('email')}
                />
                <FieldError errors={[form.formState.errors.email]} />
              </Field>
              <div className="grid grid-cols-2 gap-3">
                <Field>
                  <FieldLabel htmlFor="first_name">{t('admin.fields.first_name.label')}</FieldLabel>
                  <Input
                    id="first_name"
                    aria-invalid={!!form.formState.errors.first_name || undefined}
                    {...form.register('first_name')}
                  />
                  <FieldError errors={[form.formState.errors.first_name]} />
                </Field>
                <Field>
                  <FieldLabel htmlFor="last_name">{t('admin.fields.last_name.label')}</FieldLabel>
                  <Input
                    id="last_name"
                    aria-invalid={!!form.formState.errors.last_name || undefined}
                    {...form.register('last_name')}
                  />
                  <FieldError errors={[form.formState.errors.last_name]} />
                </Field>
              </div>
              <Field>
                <FieldLabel htmlFor="phone">{t('admin.fields.phone.label')}</FieldLabel>
                <Input
                  id="phone"
                  aria-invalid={!!form.formState.errors.phone || undefined}
                  {...form.register('phone')}
                />
                <FieldError errors={[form.formState.errors.phone]} />
              </Field>
              <Field>
                <FieldLabel>{t('admin.fields.customer.tags.label')}</FieldLabel>
                <Controller
                  name="tags"
                  control={form.control}
                  render={({ field }) => (
                    <TagCombobox
                      taggableType="customer"
                      value={field.value}
                      onChange={field.onChange}
                    />
                  )}
                />
              </Field>
              <Field>
                <div className="flex items-start justify-between gap-4">
                  <FieldLabel htmlFor="accepts_email_marketing" className="cursor-pointer">
                    {t('admin.fields.customer.accepts_email_marketing.label')}
                  </FieldLabel>
                  <Controller
                    name="accepts_email_marketing"
                    control={form.control}
                    render={({ field }) => (
                      <Checkbox
                        id="accepts_email_marketing"
                        checked={!!field.value}
                        onCheckedChange={field.onChange}
                      />
                    )}
                  />
                </div>
              </Field>
              <Field>
                <FieldLabel htmlFor="internal_note">
                  {t('admin.fields.customer.internal_note.label')}
                </FieldLabel>
                <Controller
                  control={form.control}
                  name="internal_note"
                  render={({ field }) => (
                    <RichTextEditor
                      id="internal_note"
                      ariaLabel={t('admin.fields.customer.internal_note.label')}
                      placeholder={t('admin.fields.customer.internal_note.placeholder')}
                      value={field.value}
                      onChange={field.onChange}
                    />
                  )}
                />
                <FieldError errors={[form.formState.errors.internal_note]} />
              </Field>
            </FieldGroup>
          </div>
          <SheetFooter>
            <Button
              type="button"
              variant="outline"
              onClick={() => onOpenChange(false)}
              disabled={createMutation.isPending}
            >
              {t('admin.actions.cancel')}
            </Button>
            <Button type="submit" disabled={createMutation.isPending}>
              {createMutation.isPending
                ? t('admin.actions.creating')
                : t('admin.pages.customers.create_label')}
            </Button>
          </SheetFooter>
        </form>
      </SheetContent>
    </Sheet>
  )
}