|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useCallback, useState } from 'react'; |
| 4 | +import { useWallet } from '@/hooks/use-wallet'; |
| 5 | +import { useLatestLedger } from '@/hooks/use-latest-ledger'; |
| 6 | +import { getConfig } from '@/config/env'; |
| 7 | +import { usePolicies } from '../hooks/usePolicies'; |
| 8 | +import { PolicyCard, PolicyRow } from './PolicyItem'; |
| 9 | +import { PolicyListSkeleton, PolicyEmptyState, PolicyErrorState } from './PolicyStates'; |
| 10 | +import { RenewModal } from './RenewModal'; |
| 11 | +import { TerminateModal } from './TerminateModal'; |
| 12 | +import type { PolicyDto, PolicyStatusFilter, PolicySortField } from '../api'; |
| 13 | + |
| 14 | +const SORT_OPTIONS: { value: PolicySortField; label: string }[] = [ |
| 15 | + { value: 'expiry', label: 'Expiry (soonest)' }, |
| 16 | + { value: 'coverage', label: 'Coverage (highest)' }, |
| 17 | + { value: 'premium', label: 'Premium (highest)' }, |
| 18 | +]; |
| 19 | + |
| 20 | +export function PolicyDashboard() { |
| 21 | + const { address } = useWallet(); |
| 22 | + const { network } = getConfig(); |
| 23 | + const currentLedger = useLatestLedger(); |
| 24 | + |
| 25 | + const [status, setStatus] = useState<PolicyStatusFilter>('all'); |
| 26 | + const [sort, setSort] = useState<PolicySortField>('expiry'); |
| 27 | + const [layout, setLayout] = useState<'row' | 'card'>('row'); |
| 28 | + |
| 29 | + const [renewTarget, setRenewTarget] = useState<PolicyDto | null>(null); |
| 30 | + const [terminateTarget, setTerminateTarget] = useState<PolicyDto | null>(null); |
| 31 | + |
| 32 | + const { policies, total, pageIndex, hasNextPage, hasPrevPage, loading, error, goToPage, retry } = |
| 33 | + usePolicies(address, network, status, sort); |
| 34 | + |
| 35 | + const handleRenew = useCallback((policy: PolicyDto) => setRenewTarget(policy), []); |
| 36 | + const handleTerminate = useCallback((policy: PolicyDto) => setTerminateTarget(policy), []); |
| 37 | + |
| 38 | + const totalPages = Math.max(1, Math.ceil(total / 20)); |
| 39 | + |
| 40 | + return ( |
| 41 | + <section aria-label="My policies" className="space-y-4"> |
| 42 | + {/* ── Toolbar ─────────────────────────────────────────────────── */} |
| 43 | + <div className="flex flex-wrap items-end gap-3"> |
| 44 | + {/* Status filter */} |
| 45 | + <label className="flex flex-col gap-1 text-xs font-medium text-gray-600"> |
| 46 | + Status |
| 47 | + <select |
| 48 | + value={status} |
| 49 | + onChange={(e) => { setStatus(e.target.value as PolicyStatusFilter); goToPage(0); }} |
| 50 | + className="rounded border border-gray-300 px-2 py-1.5 text-sm min-h-[44px] focus:outline-none focus:ring-2 focus:ring-blue-500" |
| 51 | + aria-label="Filter by policy status" |
| 52 | + > |
| 53 | + <option value="all">All</option> |
| 54 | + <option value="active">Active</option> |
| 55 | + <option value="expired">Expired</option> |
| 56 | + </select> |
| 57 | + </label> |
| 58 | + |
| 59 | + {/* Sort */} |
| 60 | + <label className="flex flex-col gap-1 text-xs font-medium text-gray-600"> |
| 61 | + Sort by |
| 62 | + <select |
| 63 | + value={sort} |
| 64 | + onChange={(e) => setSort(e.target.value as PolicySortField)} |
| 65 | + className="rounded border border-gray-300 px-2 py-1.5 text-sm min-h-[44px] focus:outline-none focus:ring-2 focus:ring-blue-500" |
| 66 | + aria-label="Sort policies" |
| 67 | + > |
| 68 | + {SORT_OPTIONS.map((o) => ( |
| 69 | + <option key={o.value} value={o.value}>{o.label}</option> |
| 70 | + ))} |
| 71 | + </select> |
| 72 | + </label> |
| 73 | + |
| 74 | + {/* Layout toggle */} |
| 75 | + <div |
| 76 | + role="group" |
| 77 | + aria-label="Layout" |
| 78 | + className="ml-auto flex rounded border border-gray-300 overflow-hidden" |
| 79 | + > |
| 80 | + <LayoutButton active={layout === 'row'} onClick={() => setLayout('row')} label="Table view" icon="☰" /> |
| 81 | + <LayoutButton active={layout === 'card'} onClick={() => setLayout('card')} label="Card view" icon="⊞" /> |
| 82 | + </div> |
| 83 | + </div> |
| 84 | + |
| 85 | + {/* ── Count ───────────────────────────────────────────────────── */} |
| 86 | + {!loading && !error && ( |
| 87 | + <p className="text-xs text-gray-500" aria-live="polite"> |
| 88 | + {total} {total === 1 ? 'policy' : 'policies'} |
| 89 | + {status !== 'all' ? ` · ${status}` : ''} |
| 90 | + </p> |
| 91 | + )} |
| 92 | + |
| 93 | + {/* ── Content ─────────────────────────────────────────────────── */} |
| 94 | + {loading ? ( |
| 95 | + <PolicyListSkeleton layout={layout} /> |
| 96 | + ) : error ? ( |
| 97 | + <PolicyErrorState message={error} onRetry={retry} /> |
| 98 | + ) : policies.length === 0 ? ( |
| 99 | + <PolicyEmptyState filter={status === 'all' ? 'all' : status} /> |
| 100 | + ) : layout === 'card' ? ( |
| 101 | + <div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3"> |
| 102 | + {policies.map((p) => ( |
| 103 | + <PolicyCard |
| 104 | + key={`${p.holder}:${p.policy_id}`} |
| 105 | + policy={p} |
| 106 | + onRenew={handleRenew} |
| 107 | + onTerminate={handleTerminate} |
| 108 | + currentLedger={currentLedger} |
| 109 | + /> |
| 110 | + ))} |
| 111 | + </div> |
| 112 | + ) : ( |
| 113 | + <div className="overflow-x-auto rounded-lg border border-gray-200"> |
| 114 | + <table className="min-w-full text-sm"> |
| 115 | + <thead className="bg-gray-50 text-xs text-gray-500 uppercase tracking-wide"> |
| 116 | + <tr> |
| 117 | + <th className="px-4 py-3 text-left">Policy</th> |
| 118 | + <th className="px-4 py-3 text-left">Status</th> |
| 119 | + <th className="px-4 py-3 text-right">Coverage</th> |
| 120 | + <th className="px-4 py-3 text-right">Premium / yr</th> |
| 121 | + <th className="px-4 py-3 text-left">Expiry</th> |
| 122 | + <th className="px-4 py-3 text-left">Actions</th> |
| 123 | + </tr> |
| 124 | + </thead> |
| 125 | + <tbody> |
| 126 | + {policies.map((p) => ( |
| 127 | + <PolicyRow |
| 128 | + key={`${p.holder}:${p.policy_id}`} |
| 129 | + policy={p} |
| 130 | + onRenew={handleRenew} |
| 131 | + onTerminate={handleTerminate} |
| 132 | + currentLedger={currentLedger} |
| 133 | + /> |
| 134 | + ))} |
| 135 | + </tbody> |
| 136 | + </table> |
| 137 | + </div> |
| 138 | + )} |
| 139 | + |
| 140 | + {/* ── Pagination ──────────────────────────────────────────────── */} |
| 141 | + {!loading && !error && totalPages > 1 && ( |
| 142 | + <nav aria-label="Policy pages" className="flex items-center justify-center gap-4"> |
| 143 | + <button |
| 144 | + type="button" |
| 145 | + onClick={() => goToPage(pageIndex - 1)} |
| 146 | + disabled={!hasPrevPage} |
| 147 | + aria-label="Previous page" |
| 148 | + className="min-h-[44px] min-w-[44px] rounded border border-gray-300 px-4 py-2 text-sm font-medium text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-blue-500 disabled:opacity-40 disabled:cursor-not-allowed" |
| 149 | + > |
| 150 | + Previous |
| 151 | + </button> |
| 152 | + <span aria-live="polite" className="text-sm text-gray-700"> |
| 153 | + Page {pageIndex + 1} of {totalPages} |
| 154 | + </span> |
| 155 | + <button |
| 156 | + type="button" |
| 157 | + onClick={() => goToPage(pageIndex + 1)} |
| 158 | + disabled={!hasNextPage} |
| 159 | + aria-label="Next page" |
| 160 | + className="min-h-[44px] min-w-[44px] rounded border border-gray-300 px-4 py-2 text-sm font-medium text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-blue-500 disabled:opacity-40 disabled:cursor-not-allowed" |
| 161 | + > |
| 162 | + Next |
| 163 | + </button> |
| 164 | + </nav> |
| 165 | + )} |
| 166 | + |
| 167 | + {/* ── Action modals ───────────────────────────────────────────── */} |
| 168 | + {renewTarget && ( |
| 169 | + <RenewModal policy={renewTarget} onClose={() => setRenewTarget(null)} /> |
| 170 | + )} |
| 171 | + {terminateTarget && ( |
| 172 | + <TerminateModal policy={terminateTarget} onClose={() => setTerminateTarget(null)} /> |
| 173 | + )} |
| 174 | + </section> |
| 175 | + ); |
| 176 | +} |
| 177 | + |
| 178 | +function LayoutButton({ |
| 179 | + active, |
| 180 | + onClick, |
| 181 | + label, |
| 182 | + icon, |
| 183 | +}: { |
| 184 | + active: boolean; |
| 185 | + onClick: () => void; |
| 186 | + label: string; |
| 187 | + icon: string; |
| 188 | +}) { |
| 189 | + return ( |
| 190 | + <button |
| 191 | + type="button" |
| 192 | + onClick={onClick} |
| 193 | + aria-label={label} |
| 194 | + aria-pressed={active} |
| 195 | + className={[ |
| 196 | + 'px-3 py-2 text-sm min-h-[44px] focus:outline-none focus:ring-2 focus:ring-inset focus:ring-blue-500', |
| 197 | + active ? 'bg-blue-600 text-white' : 'bg-white text-gray-600 hover:bg-gray-50', |
| 198 | + ].join(' ')} |
| 199 | + > |
| 200 | + <span aria-hidden="true">{icon}</span> |
| 201 | + </button> |
| 202 | + ); |
| 203 | +} |
0 commit comments