|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useParams } from 'next/navigation'; |
| 4 | +import { useEffect, useState } from 'react'; |
| 5 | + |
| 6 | +import { CircuitSchemaProps } from '@/components/explore-section/Circuit/type'; |
| 7 | +import { buildCircuitMap } from '@/components/explore-section/Circuit/utils/circuits-map'; |
| 8 | + |
| 9 | +export default function CircuitDetailPage() { |
| 10 | + const [circuitData, setCircuitData] = useState<CircuitSchemaProps | null>(null); |
| 11 | + const [loading, setLoading] = useState<boolean>(true); |
| 12 | + const [error, setError] = useState<string | null>(null); |
| 13 | + |
| 14 | + const params = useParams(); |
| 15 | + const circuitKey = params?.key as string | undefined; |
| 16 | + |
| 17 | + useEffect(() => { |
| 18 | + const fetchCircuit = async () => { |
| 19 | + if (!circuitKey) { |
| 20 | + setError('No existing circuit'); |
| 21 | + setLoading(false); |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + try { |
| 26 | + setLoading(true); |
| 27 | + const response = await fetch('/api/circuit/data', { |
| 28 | + cache: 'no-store', |
| 29 | + }); |
| 30 | + |
| 31 | + if (!response.ok) { |
| 32 | + throw new Error(`Error: ${response.statusText}`); |
| 33 | + } |
| 34 | + |
| 35 | + const data = await response.json(); |
| 36 | + |
| 37 | + if (data.error || !Array.isArray(data.circuits)) { |
| 38 | + throw new Error(data.error || 'Invalid response from API'); |
| 39 | + } |
| 40 | + |
| 41 | + const { circuits } = data; |
| 42 | + const circuitMap = buildCircuitMap(circuits); |
| 43 | + const matchingCircuit = circuitMap.get(circuitKey); |
| 44 | + |
| 45 | + if (!matchingCircuit) { |
| 46 | + throw new Error(`Circuit with key ${circuitKey} not found!`); |
| 47 | + } |
| 48 | + |
| 49 | + setCircuitData(matchingCircuit); |
| 50 | + } catch (er) { |
| 51 | + setError( |
| 52 | + er instanceof Error ? er.message : 'An error occurred while fetching the circuit data' |
| 53 | + ); |
| 54 | + } finally { |
| 55 | + setLoading(false); |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + fetchCircuit(); |
| 60 | + }, [circuitKey]); |
| 61 | + |
| 62 | + if (loading) { |
| 63 | + return ( |
| 64 | + <div className="relative flex h-[50vh] w-full items-center justify-center text-lg font-normal text-primary-9"> |
| 65 | + Loading... |
| 66 | + </div> |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + if (error) { |
| 71 | + return ( |
| 72 | + <div className="relative flex h-[50vh] w-full items-center justify-center text-lg font-normal text-red-600"> |
| 73 | + {error} |
| 74 | + </div> |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + return ( |
| 79 | + <div className="relative flex w-full flex-col"> |
| 80 | + {circuitData && <div>Future detail view private name</div>} |
| 81 | + </div> |
| 82 | + ); |
| 83 | +} |
0 commit comments