Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | 69x 69x 44x 44x 44x 44x 18x 69x 44x 44x 44x | import { useState, type ReactNode } from 'react'
import { PageHeader } from './Page'
import type { BreadcrumbItem } from './Page'
import { MetadataCard, type MetadataField } from './MetadataCard'
import { ConfirmDialog } from './ConfirmDialog'
import { Skeleton } from '@/components/ui/skeleton'
import { Button } from '@/components/ui/button'
import { Link } from 'react-router-dom'
export interface CrudReadCardProps {
title: ReactNode
I breadcrumbs: BreadcrumbItem[]
fields: MetadataField[]
actions: {
edit?: { onClick: () => void }
delete?: {
onClick: () => void
entityName: string
disabled?: boolean
disabledReason?: string
}
extra?: Array<{
label: string
onClick: () => void
variant?: 'default' | 'outline'
}>
}
isLoading?: boolean
error?: Error | null
is404?: boolean
onRetry?: () => void
backPath?: string
}
export function CrudReadCard({
title,
breadcrumbs,
I fields,
actions,
isLoading,
error,
is404,
onRetry,
backPath,
}: CrudReadCardProps) {
if (error) {
return (
<div className="flex flex-col items-center gap-4 py-12 text-center">
<p className="text-destructive">Failed to load</p>
{onRetry && (
<Button variant="outline" onClick={onRetry} type="button">
Retry
</Button>
)}
</div>
)
}
if (is404) {
return (
<div className="flex flex-col items-center gap-4 py-12 text-center">
<p className="text-muted-foreground">Entity not found</p>
{backPath && (
<Link
to={backPath}
className="text-sm text-primary underline-offset-4 hover:underline"
>
Back to list
</Link>
)}
</div>
)
}
const allActionsDisabled = isLoading ?? false
return (
<div className="space-y-6">
<div className="flex items-start justify-between gap-4">
<div>
{isLoading ? (
<div data-testid="crud-read-card-loading" className="space-y-2">
<Skeleton className="h-4 w-32" />
<Skeleton className="h-8 w-48" />
</div>
) : (
<PageHeader title={title} breadcrumbs={breadcrumbs} />
)}
</div>
<div className="flex shrink-0 items-center gap-2">
{actions.extra?.map((action, i) => (
<Button
key={i}
variant={action.variant ?? 'outline'}
size="sm"
onClick={action.onClick}
disabled={allActionsDisabled}
type="button"
>
{action.label}
</Button>
))}
{actions.edit && (
<Button
variant="default"
size="sm"
onClick={actions.edit.onClick}
disabled={allActionsDisabled}
type="button"
>
Edit
</Button>
)}
{actions.delete && (
<DeleteButton
entityName={actions.delete.entityName}
disabled={actions.delete.disabled || allActionsDisabled}
disabledReason={actions.delete.disabledReason}
onDelete={actions.delete.onClick}
/>
)}
</div>
</div>
{isLoading ? (
<MetadataCard fields={fields} isLoading={true} />
) : (
<MetadataCard fields={fields} />
)}
</div>
)
}
function DeleteButton({
entityName,
disabled,
disabledReason,
onDelete,
}: {
entityName: string
disabled?: boolean
disabledReason?: string
onDelete: () => void
}) {
const [open, setOpen] = useState(false)
return (
<>
<Button
variant="destructive"
size="sm"
disabled={disabled}
title={disabled ? disabledReason : undefined}
onClick={() => setOpen(true)}
type="button"
>
Delete
</Button>
<ConfirmDialog
open={open}
title="Delete entity"
message={`Are you sure you want to delete '${entityName}'?`}
confirmLabel="Delete"
onConfirm={() => {
onDelete()
setOpen(false)
}}
onCancel={() => setOpen(false)}
/>
</>
)
}
|