import { ChevronLeft, ChevronRight, RefreshCw, Terminal } from 'lucide-react' import Link from 'next/link' import { useRouter } from 'next/router' import { useState } from 'react' import { Badge, Button, Card, CardFooter, Input, Select, SelectContent, SelectItem, SelectTrigger, SelectValue, Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from 'ui' import { COMPUTE_REGION_SHORT } from './Compute.constants' import type { ComputeInstance, ComputeInstanceAccess, ComputeInstanceBuildState, } from './Compute.types' import { filterComputeInstances, formatResources, getPage } from './Compute.utils' import { ComputeInstanceStatePill } from './ComputeInstanceStatePill' import { RuntimeBadge } from './RuntimeBadge' import { ButtonTooltip } from '@/components/ui/ButtonTooltip' interface ComputeListProps { projectRef: string instances: ComputeInstance[] onDeploy: () => void onRefresh: () => void isRefreshing: boolean } const STATE_FILTERS: { value: ComputeInstanceBuildState | 'all'; label: string }[] = [ { value: 'all', label: 'All states' }, { value: 'active', label: 'Active' }, { value: 'building', label: 'Building' }, { value: 'failed', label: 'Failed' }, ] const ACCESS_FILTERS: { value: ComputeInstanceAccess | 'all'; label: string }[] = [ { value: 'all', label: 'All access' }, { value: 'public', label: 'Public' }, { value: 'private', label: 'Private' }, ] const PAGE_SIZE = 10 const parseStateFilter = (value: string): ComputeInstanceBuildState | 'all' => STATE_FILTERS.find((option) => option.value === value)?.value ?? 'all' const parseAccessFilter = (value: string): ComputeInstanceAccess | 'all' => ACCESS_FILTERS.find((option) => option.value === value)?.value ?? 'all' export const ComputeList = ({ projectRef, instances, onDeploy, onRefresh, isRefreshing, }: ComputeListProps) => { const router = useRouter() const [search, setSearch] = useState('') const [stateFilter, setStateFilter] = useState('all') const [accessFilter, setAccessFilter] = useState('all') const [page, setPage] = useState(1) const filtered = filterComputeInstances(instances, { search, state: stateFilter, access: accessFilter, }) const { items: paged, currentPage, totalPages, startIndex } = getPage(filtered, page, PAGE_SIZE) const resetToFirstPage = () => setPage(1) const computeInstancePagePath = (name: string) => `/project/${projectRef}/compute/${name}` return (
{ setSearch(e.target.value) resetToFirstPage() }} />
Name State Runtime Access Region Resources {filtered.length === 0 && ( No instances match your filters )} {paged.map((instance) => ( router.push(computeInstancePagePath(instance.name))} > e.stopPropagation()} > {instance.name} {instance.access === 'public' ? ( Public ) : ( Private )} {COMPUTE_REGION_SHORT} {formatResources(instance)} ))}
{filtered.length > 0 && (

Showing {startIndex + 1} to {startIndex + paged.length} of {filtered.length} instance {filtered.length === 1 ? '' : 's'}

} aria-label="Previous page" size="tiny" disabled={currentPage === 1} onClick={() => setPage(currentPage - 1)} tooltip={{ content: { side: 'top', text: currentPage === 1 ? 'Already on the first page' : undefined, }, }} /> Page {currentPage} of {totalPages} } aria-label="Next page" size="tiny" disabled={currentPage >= totalPages} onClick={() => setPage(currentPage + 1)} tooltip={{ content: { side: 'top', text: currentPage >= totalPages ? 'Already on the last page' : undefined, }, }} />
)}
) }