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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | 1x 38x 38x 38x 38x 38x 38x 38x 38x 38x 12x 12x 12x 12x 12x 12x 12x 38x 12x 38x 38x 24x 38x 12x 26x 26x 104x 104x | /**
* Collection detail shell: header summary, tab nav, and outlet.
*/
import { useCallback, useEffect, useMemo, useState } from 'react';
import { NavLink, Outlet, useNavigate, useParams } from 'react-router';
import {
BarChart2,
Database,
Pencil,
RefreshCw,
Shield,
Table2,
Trash2,
} from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import DeleteCollectionDialog from '@/components/collections/DeleteCollectionDialog';
import {
deleteCollection,
getCollectionByName,
type Collection,
} from '@/services/collections.service';
import { handleApiError } from '@/lib/api';
import { cn } from '@/lib/utils';
import { useCollectionsWorkspace } from './CollectionsWorkspaceContext';
import {
CollectionDetailProvider,
type CollectionDetailContextValue,
} from './CollectionDetailContext';
const TABS = [
{ segment: 'schema', label: 'Schema', icon: Table2 },
{ segment: 'data', label: 'Data', icon: Database },
{ segment: 'rules', label: 'Rules', icon: Shield },
{ segment: 'analytics', label: 'Analytics', icon: BarChart2 },
] as const;
export default function CollectionDetailLayout() {
const { collectionName = '' } = useParams<{ collectionName: string }>();
const navigate = useNavigate();
const {
getListItem,
refreshCollections,
isSuperadmin,
} = useCollectionsWorkspace();
const listItem = getListItem(collectionName);
const [collection, setCollection] = useState<Collection | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [deleteOpen, setDeleteOpen] = useState(false);
const refreshDetail = useCallback(async () => {
Iif (!collectionName) return;
setLoading(true);
setError(null);
try {
const data = await getCollectionByName(collectionName);
setCollection(data);
} catch (err) {
setError(handleApiError(err));
setCollection(null);
} finally {
setLoading(false);
}
}, [collectionName]);
useEffect(() => {
void refreshDetail();
}, [refreshDetail]);
const handleDelete = async (collectionId: string) => {
await deleteCollection(collectionId);
await refreshCollections();
navigate('/admin/collections', { replace: true });
};
const detailValue = useMemo<CollectionDetailContextValue>(
() => ({
collection,
listItem,
collectionName,
loading,
error,
refreshDetail,
}),
[collection, listItem, collectionName, loading, error, refreshDetail],
);
if (loading && !collection) {
return (
<div className="flex items-center justify-center py-20">
<RefreshCw className="h-6 w-6 animate-spin text-muted-foreground" />
</div>
);
}
Iif (error && !collection) {
return (
<div className="space-y-3 p-8 text-center" data-testid="collection-detail-error">
<p className="font-medium text-destructive">Failed to load collection</p>
<p className="text-sm text-muted-foreground">{error}</p>
<Button size="sm" variant="outline" onClick={() => void refreshDetail()}>
Retry
</Button>
</div>
);
}
return (
<CollectionDetailProvider value={detailValue}>
<div className="flex h-full min-h-0 flex-col" data-testid="collection-detail-layout">
<header className="shrink-0 border-b bg-background px-6 py-4">
<div className="flex flex-wrap items-start justify-between gap-3">
<div className="min-w-0 space-y-1">
<div className="flex flex-wrap items-center gap-2">
<h1 className="truncate text-2xl font-bold">{collectionName}</h1>
{(listItem?.has_public_access || false) && (
<Badge
variant="outline"
className="border-green-500 text-green-600"
>
Public
</Badge>
)}
</div>
<p className="text-sm text-muted-foreground">
{listItem?.fields_count ?? collection?.schema.length ?? 0} fields
{' ยท '}
{listItem?.records_count ?? 0} records
</p>
</div>
{isSuperadmin && (
<div className="flex flex-wrap items-center gap-2">
<Button
variant="outline"
size="sm"
className="gap-1.5"
onClick={() => {
navigate(`/admin/collections/${collectionName}/schema`);
}}
>
<Pencil className="h-3.5 w-3.5" />
Edit schema
</Button>
<Button
variant="outline"
size="sm"
className="gap-1.5 text-destructive hover:text-destructive"
onClick={() => setDeleteOpen(true)}
>
<Trash2 className="h-3.5 w-3.5" />
Delete
</Button>
</div>
)}
</div>
<nav
className="-mb-px mt-4 flex gap-1 border-b"
aria-label="Collection tabs"
>
{TABS.map(({ segment, label, icon: Icon }) => (
<NavLink
key={segment}
to={`/admin/collections/${collectionName}/${segment}`}
className={({ isActive }) =>
cn(
'inline-flex items-center gap-1.5 border-b-2 px-3 py-2 text-sm font-medium -mb-px transition-colors',
isActive
? 'border-primary text-foreground'
: 'border-transparent text-muted-foreground hover:border-muted-foreground/40 hover:text-foreground',
)
}
>
<Icon className="h-3.5 w-3.5" />
{label}
</NavLink>
))}
</nav>
</header>
<div className="min-h-0 flex-1 overflow-y-auto p-6">
<Outlet />
</div>
</div>
<DeleteCollectionDialog
open={deleteOpen}
onOpenChange={setDeleteOpen}
collection={
listItem ??
(collection
? {
id: collection.id,
name: collection.name,
table_name: collection.table_name,
fields_count: collection.schema.length,
records_count: 0,
has_public_access: false,
created_at: collection.created_at,
}
: null)
}
onConfirm={handleDelete}
/>
</CollectionDetailProvider>
);
}
|