File Transfer List
Upload and download state, caller-provided progress and context-appropriate retry or cancel actions.
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 file-transfer-listSource files
Expand a file to read the source the installer copies into your app.
components/blocks/file-transfer-list/FileTransferList.tsx
import { Fragment } from 'react'
import { View, type ViewProps } from 'react-native'
import { Button } from '@/components/ui/button'
import { Progress } from '@/components/ui/progress'
import { Separator } from '@/components/ui/separator'
import { Text } from '@/components/ui/text'
import { cn } from '@/lib/utils'
export type FileTransferState =
| { kind: 'active'; progress?: number }
| { kind: 'stopped' }
| { kind: 'failed'; reason?: string }
| { kind: 'completed' }
export interface FileTransfer {
id: string
name: string
direction: 'upload' | 'download'
detail?: string
/** Active progress is a fraction from 0 to 1. Omit it for unknown progress. */
state: FileTransferState
}
const uploadLabels = { active: 'Uploading', stopped: 'Upload stopped', failed: 'Upload failed', completed: 'Uploaded' }
const downloadLabels = {
active: 'Downloading',
stopped: 'Download stopped',
failed: 'Download failed',
completed: 'Downloaded',
}
const percent = new Intl.NumberFormat(undefined, { style: 'percent', maximumFractionDigits: 0 })
export interface FileTransferListProps extends Omit<ViewProps, 'children'> {
className?: string
title?: string
/** Bounded caller-owned transfer rows with unique, nonempty IDs. */
transfers?: readonly FileTransfer[]
onCancel?: (id: string) => void
onRetry?: (id: string) => void
labels?: {
upload?: Partial<typeof uploadLabels>
download?: Partial<typeof downloadLabels>
empty?: string
cancel?: string
retry?: string
}
}
export function FileTransferList({
className,
title = 'File transfers',
transfers = [],
onCancel,
onRetry,
labels,
...props
}: FileTransferListProps) {
const cancel = labels?.cancel ?? 'Cancel'
const retry = labels?.retry ?? 'Retry'
return (
<View {...props} className={cn('gap-[16px]', className)}>
<Text size="title" accessibilityRole="header">
{title}
</Text>
{transfers.length ? (
<View className="overflow-hidden rounded-lg border border-border bg-card">
{transfers.map(({ id, name, direction, detail, state }, index) => {
const fallback = direction === 'upload' ? uploadLabels : downloadLabels
const status = labels?.[direction]?.[state.kind] ?? fallback[state.kind]
const fraction = state.kind === 'active' ? state.progress : undefined
const percentage =
fraction !== undefined && Number.isFinite(fraction) && fraction >= 0 && fraction <= 1
? fraction === 1
? 100
: Math.min(99, Math.round(fraction * 100))
: undefined
const action =
state.kind === 'active' && onCancel
? { label: cancel, invoke: onCancel }
: (state.kind === 'stopped' || state.kind === 'failed') && onRetry
? { label: retry, invoke: onRetry }
: undefined
return (
<Fragment key={id}>
{index > 0 ? <Separator /> : null}
<View className="gap-[8px] px-[16px] py-[12px]">
<View className="gap-[4px]">
<Text className="font-semibold text-card-foreground">{name}</Text>
{detail ? (
<Text size="sm" variant="muted">
{detail}
</Text>
) : null}
<View className="flex-row flex-wrap items-baseline justify-between gap-x-[12px]">
<Text
size="sm"
variant="muted"
className={cn('shrink', state.kind === 'failed' && 'text-destructive')}
>
{status}
</Text>
{percentage !== undefined ? (
<Text size="sm" aria-hidden style={{ fontVariant: ['tabular-nums'] }}>
{percent.format(percentage / 100)}
</Text>
) : null}
</View>
{state.kind === 'failed' && state.reason ? (
<Text size="sm" variant="muted">
{state.reason}
</Text>
) : null}
</View>
{percentage !== undefined ? (
<Progress
accessible
accessibilityState={{ busy: true }}
accessibilityLabel={`${status}, ${name}`}
value={percentage}
/>
) : null}
{action ? (
<View className="items-start">
<Button
size="sm"
variant="outline"
accessibilityLabel={`${action.label}, ${name}`}
onPress={() => action.invoke(id)}
>
{action.label}
</Button>
</View>
) : null}
</View>
</Fragment>
)
})}
</View>
) : (
<Text variant="muted">{labels?.empty ?? 'No file transfers.'}</Text>
)}
</View>
)
}
components/blocks/file-transfer-list/index.ts
export {
FileTransferList,
type FileTransfer,
type FileTransferListProps,
type FileTransferState,
} from './FileTransferList'