import { Button } from "../components/Button.tsx"; import { useEffect } from "preact/hooks"; import { ComponentChildren } from "preact"; import { Signal, useSignal } from "@preact/signals"; interface StockProps { pageName: string; } interface ToggleButtonProps { children?: ComponentChildren; } function ToggleButton(props: ToggleButtonProps) { return ( ); } type QueryStatus = { type: "loading"; } | { type: "complete"; data: T; } | { type: "error"; err: Error; }; const cacheMap = new Map(); function useQuery(url: string): Signal> { const state = useSignal({ type: "loading", } as QueryStatus); useEffect(() => { if (!cacheMap.has(url)) { (async () => { try { const res = await fetch(url); const data = await res.json(); cacheMap.set(url, data); state.value = { type: "complete", data: data, }; } catch (err) { state.value = { type: "error", err: err, }; } })(); } else { state.value = { type: "complete", data: cacheMap.get(url) as T, }; } },[]); return state; } interface Coperation { Name: string; Code: string; Sector: string; Product: string; ListingDay: string; ClosingMonth: string; Representative: string; Homepage: string; AddressArea: string; LastUpdate: string; } interface PageCorpsInfo { name: string; description: string; corpListByDate: Record; } function StockList({data}: {data: PageCorpsInfo}){ console.log("data") const keys = Object.keys(data.corpListByDate).sort().reverse().slice(0,5).reverse(); //const rows = data.corpListbyDate; return {keys.map((x,i)=>{ const rows = data.corpListByDate[x]; return {rows.map(row=>{ return {row.Name} })} })} } export default function StockListUI(props: StockProps) { const sig = useQuery("/api/pages/" + props.pageName); return ( Kospi Kosdaq Otherwise {sig.value.type == "loading" ? (new Array(20).fill(0).map((_) => ( ))) : { sig.value.type == "error" ? ( File Loading Failed ) : } } ); }
File Loading Failed