|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useState } from "react"; |
| 4 | +import { initCache } from "@/storage/cache"; |
| 5 | +import "./Initializer.css"; |
| 6 | +import { usePathname } from "next/navigation"; |
| 7 | +import type { StatusMap } from "@/types/initializer"; |
| 8 | + |
| 9 | +function getInitializerText(status: StatusMap): string { |
| 10 | + if (!status.airports) { |
| 11 | + return "Downloading airport data ..."; |
| 12 | + } else if (!status.firs) { |
| 13 | + return "Downloading VAT-Spy data ..."; |
| 14 | + } else if (!status.tracons) { |
| 15 | + return "Downloading SimAware data ..."; |
| 16 | + } else if (!status.airlines) { |
| 17 | + return "Downloading airline data ..."; |
| 18 | + } else if (!status.cache) { |
| 19 | + return "Initializing local cache ..."; |
| 20 | + } else if (!status.map) { |
| 21 | + return "Setting up map ..."; |
| 22 | + } else { |
| 23 | + return "Initialization complete!"; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +export default function Initializer() { |
| 28 | + const [open, setOpen] = useState(true); |
| 29 | + const [visible, setVisible] = useState(true); |
| 30 | + const [status, setStatus] = useState<StatusMap>({}); |
| 31 | + const pathname = usePathname(); |
| 32 | + |
| 33 | + useEffect(() => { |
| 34 | + initCache(setStatus, pathname); |
| 35 | + return () => {}; |
| 36 | + }, [pathname]); |
| 37 | + |
| 38 | + useEffect(() => { |
| 39 | + if (status.airports && status.firs && status.tracons && status.airlines && status.cache && status.map) { |
| 40 | + setOpen(false); |
| 41 | + setTimeout(() => setVisible(false), 500); |
| 42 | + } |
| 43 | + }, [status]); |
| 44 | + |
| 45 | + if (!visible) { |
| 46 | + return null; |
| 47 | + } |
| 48 | + |
| 49 | + return ( |
| 50 | + <div id="initializer" className={open ? "open" : ""}> |
| 51 | + <span id="initializer-progress" style={{ width: `${(Object.keys(status).length / 6) * 100}%` }}></span> |
| 52 | + <p id="initializer-title">Initializing data ...</p> |
| 53 | + <p id="initializer-disclaimer">This can take up to a minute during the first load.</p> |
| 54 | + <p id="initializer-text">{getInitializerText(status)}</p> |
| 55 | + </div> |
| 56 | + ); |
| 57 | +} |
0 commit comments