All files / pages/collections CollectionDefaultTabRedirect.tsx

62.5% Statements 10/16
50% Branches 9/18
50% Functions 1/2
62.5% Lines 10/16

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                        2x 2x     2x   2x               2x                               2x         2x               2x                     2x 2x        
/**
 * Redirect /admin/collections/:name to default tab:
 * Data if records_count > 0, else Schema.
 */
 
import { Navigate, useParams } from 'react-router';
import { RefreshCw } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { useCollectionsWorkspace } from './CollectionsWorkspaceContext';
import { resolveDefaultTab } from './defaultTab';
 
export default function CollectionDefaultTabRedirect() {
  const { collectionName } = useParams<{ collectionName: string }>();
  const { getListItem, loading, collections, error, refreshCollections } =
    useCollectionsWorkspace();
 
  const listItem = collectionName ? getListItem(collectionName) : undefined;
 
  Iif (loading && collections.length === 0) {
    return (
      <div className="flex items-center justify-center py-20">
        <RefreshCw className="h-6 w-6 animate-spin text-muted-foreground" />
      </div>
    );
  }
 
  Iif (error && collections.length === 0) {
    return (
      <div className="p-8 text-center space-y-2">
        <p className="text-destructive">{error}</p>
        <Button
          type="button"
          size="sm"
          variant="outline"
          onClick={() => void refreshCollections()}
        >
          Retry
        </Button>
      </div>
    );
  }
 
  Iif (!collectionName) {
    return <Navigate to="/admin/collections" replace />;
  }
 
  // Wait for list to resolve default tab when item not yet present
  Iif (loading && !listItem) {
    return (
      <div className="flex items-center justify-center py-20">
        <RefreshCw className="h-6 w-6 animate-spin text-muted-foreground" />
      </div>
    );
  }
 
  Iif (!listItem) {
    return (
      <div className="p-8 text-center space-y-2" data-testid="collection-not-found">
        <p className="font-medium">Collection not found</p>
        <p className="text-sm text-muted-foreground">
          “{collectionName}” does not exist or you do not have access.
        </p>
      </div>
    );
  }
 
  const tab = resolveDefaultTab(listItem.records_count);
  return (
    <Navigate to={`/admin/collections/${collectionName}/${tab}`} replace />
  );
}