UIPackage React Native
Search

Support Request

Contextual support intake with topic selection, a message, and draft-preserving retries.

Install

First install? Set up the registry in your app.

npx --yes --package=https://native.uipkge.dev/cli/uipkge-native-0.1.1.tgz uipkge-native add support-request

Source files

Expand a file to read the source the installer copies into your app.

components/blocks/support-request/SupportRequest.tsx
import * as React from 'react'
import { TextInput, View, type ViewProps } from 'react-native'
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'
import { Button } from '@/components/ui/button'
import { FormField } from '@/components/ui/form-field'
import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'
import { Text } from '@/components/ui/text'
import { Textarea } from '@/components/ui/textarea'
import { renderTextChildren } from '@/lib/label-children'
import { cn } from '@/lib/utils'

export interface SupportTopic {
  id: string
  label: string
  description?: string
  disabled?: boolean
}

export interface SupportRequestValue {
  topicId: string
  details: string
}

export interface SupportRequestProps extends Omit<ViewProps, 'children'> {
  className?: string
  title?: string
  context?: React.ReactNode
  topics?: readonly SupportTopic[]
  detailsLabel?: string
  detailsRequired?: boolean
  detailsRequiredMessage?: string
  maxLength?: number
  submitLabel?: string
  emptyMessage?: string
  errorTitle?: string
  errorMessage?: string
  onSubmit?: (request: SupportRequestValue) => void | boolean | Promise<void | boolean>
}

const sampleTopics: SupportTopic[] = [
  { id: 'order', label: 'Order issue' },
  { id: 'account', label: 'Account access' },
]

export function SupportRequest({
  className,
  title = 'Get support',
  context,
  topics = sampleTopics,
  detailsLabel = 'Details',
  detailsRequired = false,
  detailsRequiredMessage = 'Describe what happened.',
  maxLength,
  submitLabel = 'Send request',
  emptyMessage = 'No support topics are available yet.',
  errorTitle = 'Request wasn’t sent',
  errorMessage = 'Your information is still here. Try again when you’re ready.',
  onSubmit,
  ...props
}: SupportRequestProps) {
  const [topicId, setTopicId] = React.useState('')
  const [details, setDetails] = React.useState('')
  const [busy, setBusy] = React.useState(false)
  const [failed, setFailed] = React.useState(false)
  const [detailsError, setDetailsError] = React.useState(false)
  const detailsField = React.useRef<React.ComponentRef<typeof TextInput>>(null)
  const request = React.useRef(false)
  const selected = topics.find((topic) => topic.id === topicId && !topic.disabled)

  async function submit() {
    if (request.current || !selected || !onSubmit) return
    const message = details.trim()
    if (detailsRequired && !message) {
      setDetailsError(true)
      detailsField.current?.focus()
      return
    }
    request.current = true
    setBusy(true)
    setFailed(false)
    try {
      if ((await onSubmit({ topicId: selected.id, details: message })) === false) setFailed(true)
    } catch {
      setFailed(true)
    } finally {
      request.current = false
      setBusy(false)
    }
  }

  return (
    <View {...props} className={cn('gap-4', className)}>
      <Text size="title" accessibilityRole="header">
        {title}
      </Text>
      {renderTextChildren(context)}
      {topics.length ? (
        <RadioGroup
          accessibilityLabel="Topic"
          value={selected?.id ?? ''}
          disabled={busy}
          onValueChange={(id) => {
            if (request.current || !topics.some((topic) => topic.id === id && !topic.disabled)) return
            setTopicId(id)
            setFailed(false)
          }}
        >
          {topics.map((topic) => (
            <RadioGroupItem
              key={topic.id}
              value={topic.id}
              label={topic.label}
              accessibilityLabel={topic.label}
              accessibilityHint={topic.description}
              disabled={topic.disabled}
              className="items-start rounded-lg border border-border bg-card p-3"
            >
              {topic.description ? (
                <Text size="sm" variant="muted">
                  {topic.description}
                </Text>
              ) : null}
            </RadioGroupItem>
          ))}
        </RadioGroup>
      ) : (
        <Text variant="muted">{emptyMessage}</Text>
      )}
      {topics.length ? (
        <FormField
          label={detailsLabel}
          required={detailsRequired}
          error={detailsError ? detailsRequiredMessage : undefined}
        >
          {(field) => (
            <Textarea
              {...field}
              ref={detailsField}
              value={details}
              maxLength={maxLength}
              editable={!busy}
              onChangeText={(message) => {
                if (request.current) return
                setDetails(message)
                setDetailsError(false)
                setFailed(false)
              }}
            />
          )}
        </FormField>
      ) : null}
      {failed ? (
        <Alert variant="destructive">
          <AlertTitle>{errorTitle}</AlertTitle>
          <AlertDescription>{errorMessage}</AlertDescription>
        </Alert>
      ) : null}
      {onSubmit ? (
        <Button
          accessibilityLabel={submitLabel}
          disabled={!selected}
          loading={busy}
          onPress={() => {
            void submit()
          }}
        >
          {submitLabel}
        </Button>
      ) : null}
    </View>
  )
}
components/blocks/support-request/index.ts
export { SupportRequest, type SupportRequestProps, type SupportRequestValue, type SupportTopic } from './SupportRequest'

Dependencies

Browse work and productivity