UIPackage React Native
Search
Menu

Edit Conflict

A native section for comparing saved and draft edits and applying a revision-specific choice.

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 edit-conflict

Source files

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

components/blocks/edit-conflict/EditConflict.tsx
import * as React from 'react'
import { View, type ViewProps } from 'react-native'
import { Alert, AlertDescription } from '@/components/ui/alert'
import { Button } from '@/components/ui/button'
import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'
import { Text } from '@/components/ui/text'
import { useAnnouncement } from '@/lib/accessibility'
import { cn } from '@/lib/utils'

export type EditConflictChoice = 'saved' | 'draft'

export interface EditConflictVersion {
  /** A revision ID. Give a replacement version a different ID. */
  id: string
  summary: string
  /** A caller-localized date or time. */
  updatedAt?: string
}

export interface EditConflictDecision {
  choice: EditConflictChoice
  savedId: string
  draftId: string
}

const defaultLabels = {
  options: 'Choose the version to keep',
  saved: 'Keep saved version',
  draft: 'Keep my draft',
  apply: 'Apply choice',
  applying: 'Applying choice…',
  retry: 'Retry applying choice',
  failed: 'The choice could not be applied. Both versions are still shown.',
}

export interface EditConflictProps extends Omit<ViewProps, 'children'> {
  className?: string
  title: string
  description?: string
  saved: EditConflictVersion
  draft: EditConflictVersion
  onResolve: (decision: EditConflictDecision) => void | boolean | Promise<void | boolean>
  disabled?: boolean
  labels?: Partial<typeof defaultLabels>
}

export function EditConflict(props: EditConflictProps) {
  return <ConflictDecision key={JSON.stringify([props.saved.id, props.draft.id])} {...props} />
}

function ConflictDecision({
  className,
  title,
  description,
  saved,
  draft,
  onResolve,
  disabled = false,
  labels,
  ...props
}: EditConflictProps) {
  const copy = { ...defaultLabels, ...labels }
  const [choice, setChoice] = React.useState<EditConflictChoice>()
  const [pending, setPending] = React.useState(false)
  const [failed, setFailed] = React.useState(false)
  const inFlight = React.useRef(false)
  const actionLabel = pending ? copy.applying : failed ? copy.retry : copy.apply
  useAnnouncement(failed ? copy.failed : undefined)

  async function confirm() {
    if (!choice || disabled || inFlight.current) return
    inFlight.current = true
    setPending(true)
    setFailed(false)
    try {
      if ((await onResolve({ choice, savedId: saved.id, draftId: draft.id })) === false) setFailed(true)
    } catch {
      setFailed(true)
    } finally {
      inFlight.current = false
      setPending(false)
    }
  }

  return (
    <View {...props} className={cn('gap-4', className)}>
      <View className="gap-1">
        <Text size="xl" accessibilityRole="header">
          {title}
        </Text>
        {description ? <Text variant="muted">{description}</Text> : null}
      </View>
      <RadioGroup
        accessibilityLabel={copy.options}
        value={choice ?? ''}
        disabled={pending || disabled}
        onValueChange={(value) => {
          if (inFlight.current || (value !== 'saved' && value !== 'draft')) return
          setChoice(value)
          setFailed(false)
        }}
      >
        <RadioGroupItem
          value="saved"
          label={copy.saved}
          accessibilityLabel={[copy.saved, saved.updatedAt, saved.summary].filter(Boolean).join(', ')}
          className={cn('items-start rounded-xl border border-border bg-card', choice === 'saved' && 'border-primary')}
        >
          {saved.updatedAt ? (
            <Text variant="muted" size="sm">
              {saved.updatedAt}
            </Text>
          ) : null}
          <Text>{saved.summary}</Text>
        </RadioGroupItem>
        <RadioGroupItem
          value="draft"
          label={copy.draft}
          accessibilityLabel={[copy.draft, draft.updatedAt, draft.summary].filter(Boolean).join(', ')}
          className={cn('items-start rounded-xl border border-border bg-card', choice === 'draft' && 'border-primary')}
        >
          {draft.updatedAt ? (
            <Text variant="muted" size="sm">
              {draft.updatedAt}
            </Text>
          ) : null}
          <Text>{draft.summary}</Text>
        </RadioGroupItem>
      </RadioGroup>
      {failed ? (
        <Alert accessibilityRole="none" variant="destructive">
          <AlertDescription>{copy.failed}</AlertDescription>
        </Alert>
      ) : null}
      <Button
        loading={pending}
        disabled={disabled || !choice}
        accessibilityLabel={actionLabel}
        onPress={() => {
          void confirm()
        }}
      >
        {actionLabel}
      </Button>
    </View>
  )
}
components/blocks/edit-conflict/index.ts
export {
  EditConflict,
  type EditConflictChoice,
  type EditConflictDecision,
  type EditConflictProps,
  type EditConflictVersion,
} from './EditConflict'

Dependencies

Browse work and productivity