Draft Recovery
A saved draft summary with inspect, resume, and confirmed discard actions plus recoverable failures.
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 draft-recoverySource files
Expand a file to read the source the installer copies into your app.
components/blocks/draft-recovery/DraftRecovery.tsx
import * as React from 'react'
import { View, type ViewProps } from 'react-native'
import { Alert, AlertDescription } from '@/components/ui/alert'
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogTrigger,
} from '@/components/ui/alert-dialog'
import { Button } from '@/components/ui/button'
import { Text } from '@/components/ui/text'
import { useAnnouncement } from '@/lib/accessibility'
import { cn } from '@/lib/utils'
export interface DraftRecoveryEntry {
id: string
title: string
preview?: string
/** A caller-localized short date or time. */
savedAt?: string
}
type DraftAction = 'inspect' | 'resume' | 'discard'
type DraftActionHandler = (id: string) => void | boolean | Promise<void | boolean>
const defaultLabels = {
savedAt: (time: string) => `Saved ${time}`,
inspect: 'Inspect draft',
inspecting: 'Opening preview…',
retryInspect: 'Retry preview',
resume: 'Resume draft',
resuming: 'Opening draft…',
retryResume: 'Retry opening draft',
discard: 'Discard draft',
discarding: 'Discarding draft…',
retryDiscard: 'Retry discarding',
keep: 'Keep draft',
confirm: 'Discard saved draft',
confirmTitle: 'Discard this draft?',
confirmDescription: (title: string) => `“${title}” will be removed from saved drafts.`,
close: 'Close confirmation',
failed: (action: DraftAction) =>
action === 'discard'
? 'Could not discard this draft. It is still saved.'
: 'Could not open this draft. It is still saved.',
}
export interface DraftRecoveryProps extends Omit<ViewProps, 'children'> {
className?: string
draft?: DraftRecoveryEntry | null
onInspect?: DraftActionHandler
onResume?: DraftActionHandler
onDiscard?: DraftActionHandler
disabled?: boolean
labels?: Partial<typeof defaultLabels>
}
export function DraftRecovery({ draft, ...props }: DraftRecoveryProps) {
return draft ? <DraftCard key={draft.id} draft={draft} {...props} /> : null
}
function DraftCard({
className,
draft,
onInspect,
onResume,
onDiscard,
disabled = false,
labels,
...props
}: Omit<DraftRecoveryProps, 'draft'> & { draft: DraftRecoveryEntry }) {
const copy = { ...defaultLabels, ...labels }
const [pending, setPending] = React.useState<DraftAction>()
const [failed, setFailed] = React.useState<DraftAction>()
const busy = React.useRef(false)
const blocked = disabled || !!pending
const error = failed ? copy.failed(failed) : undefined
useAnnouncement(error)
async function run(action: DraftAction, handler: DraftActionHandler) {
if (disabled || busy.current) return
busy.current = true
setPending(action)
setFailed(undefined)
try {
if ((await handler(draft.id)) === false) setFailed(action)
} catch {
setFailed(action)
} finally {
busy.current = false
setPending(undefined)
}
}
const resumeLabel = pending === 'resume' ? copy.resuming : failed === 'resume' ? copy.retryResume : copy.resume
const inspectLabel = pending === 'inspect' ? copy.inspecting : failed === 'inspect' ? copy.retryInspect : copy.inspect
const discardLabel = pending === 'discard' ? copy.discarding : failed === 'discard' ? copy.retryDiscard : copy.discard
return (
<View {...props} className={cn('gap-4 rounded-xl border border-border bg-card p-4', className)}>
<View className="gap-1">
<Text accessibilityRole="header" size="xl">
{draft.title}
</Text>
{draft.savedAt ? (
<Text size="sm" variant="muted">
{copy.savedAt(draft.savedAt)}
</Text>
) : null}
{draft.preview ? <Text>{draft.preview}</Text> : null}
</View>
{error ? (
<Alert accessibilityRole="none" variant="destructive">
<AlertDescription>{error}</AlertDescription>
</Alert>
) : null}
{onResume ? (
<Button
accessibilityLabel={resumeLabel}
loading={pending === 'resume'}
disabled={blocked && pending !== 'resume'}
onPress={() => {
void run('resume', onResume)
}}
>
{resumeLabel}
</Button>
) : null}
{onInspect || onDiscard ? (
<View className="flex-row flex-wrap gap-2">
{onInspect ? (
<Button
variant="outline"
accessibilityLabel={inspectLabel}
loading={pending === 'inspect'}
disabled={blocked && pending !== 'inspect'}
onPress={() => {
void run('inspect', onInspect)
}}
>
{inspectLabel}
</Button>
) : null}
{onDiscard ? (
<AlertDialog>
<AlertDialogTrigger
variant="ghost"
accessibilityLabel={discardLabel}
loading={pending === 'discard'}
disabled={blocked && pending !== 'discard'}
onPress={(event) => {
if (busy.current) event.preventDefault()
}}
>
{discardLabel}
</AlertDialogTrigger>
<AlertDialogContent
title={copy.confirmTitle}
description={copy.confirmDescription(draft.title)}
closeLabel={copy.close}
>
<View className="flex-row flex-wrap gap-2">
<AlertDialogCancel variant="outline" accessibilityLabel={copy.keep}>
{copy.keep}
</AlertDialogCancel>
<AlertDialogAction
variant="destructive"
accessibilityLabel={copy.confirm}
disabled={blocked}
onPress={() => {
void run('discard', onDiscard)
}}
>
{copy.confirm}
</AlertDialogAction>
</View>
</AlertDialogContent>
</AlertDialog>
) : null}
</View>
) : null}
</View>
)
}
components/blocks/draft-recovery/index.ts
export { DraftRecovery, type DraftRecoveryEntry, type DraftRecoveryProps } from './DraftRecovery'