SlotsPage.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. 'use client';
  2. import { useState, useEffect, useMemo } from 'react';
  3. import { Radio } from 'lucide-react';
  4. import { useLanguage } from '@/lib/i18n/LanguageContext';
  5. import SlotFilterBar, { LOCATIONS } from '@/components/slots/SlotFilterBar';
  6. import SlotGrid from '@/components/slots/SlotGrid';
  7. import { SlotSnapshot } from '@/types';
  8. import { statsService } from '@/lib/services';
  9. export default function SlotDashboardPage() {
  10. const { t } = useLanguage();
  11. const [loading, setLoading] = useState(true);
  12. const [snapshots, setSnapshots] = useState<SlotSnapshot[]>([]);
  13. const [selectedCountryCode, setSelectedCountryCode] = useState('IE');
  14. const [selectedCity, setSelectedCity] = useState('Dublin');
  15. const currentCities = useMemo(() => {
  16. return LOCATIONS.find((l) => l.code === selectedCountryCode)?.cities || [];
  17. }, [selectedCountryCode]);
  18. const handleCountryChange = (code: string) => {
  19. setSelectedCountryCode(code);
  20. const countryData = LOCATIONS.find((l) => l.code === code);
  21. if (countryData && countryData.cities.length > 0) {
  22. setSelectedCity(countryData.cities[0]);
  23. } else {
  24. setSelectedCity('');
  25. }
  26. };
  27. const fetchDashboard = async () => {
  28. setLoading(true);
  29. try {
  30. const res = await statsService.getSlotsOverview(selectedCity);
  31. const data = res?.data || [];
  32. // 过滤当前城市
  33. const filtered = data.filter((i: SlotSnapshot) => i.city === selectedCity);
  34. // 排序: Available > Waitlist > None
  35. filtered.sort((a: SlotSnapshot, b: SlotSnapshot) => {
  36. const score = (s: string) => (s === 'Available' ? 3 : s === 'Waitlist' ? 2 : 1);
  37. return score(b.availability_status) - score(a.availability_status);
  38. });
  39. setSnapshots(filtered);
  40. } catch (e) {
  41. console.warn('Fetch slots failed', e);
  42. setSnapshots([]);
  43. } finally {
  44. setLoading(false);
  45. }
  46. };
  47. useEffect(() => {
  48. if (selectedCity) {
  49. fetchDashboard();
  50. }
  51. }, [selectedCity]);
  52. return (
  53. <div className="min-h-screen bg-neutral-100 py-6 px-4 sm:px-6 md:py-10 md:px-8 space-y-6 max-w-7xl mx-auto">
  54. {/* 顶部雷达标头卡片 */}
  55. <div className="bg-neutral-100 border-2 border-black p-6 sm:p-8 shadow-[6px_6px_0px_#000] text-black">
  56. <div className="flex flex-wrap items-center justify-between gap-2 border-b-2 border-black pb-3 mb-4 font-mono text-xs font-bold select-none">
  57. <div className="flex items-center gap-2">
  58. <span className="w-3 h-3 bg-amber-300 border border-black inline-block animate-pulse" />
  59. <span className="tracking-widest uppercase">[ {t('slots.title')} ]</span>
  60. </div>
  61. <div className="text-neutral-500 font-mono flex items-center gap-1.5">
  62. <Radio size={14} className="text-black animate-spin" />
  63. <span>{t('slots.updated_at')}</span>
  64. </div>
  65. </div>
  66. <div className="max-w-3xl space-y-2">
  67. <h1 className="text-2xl sm:text-3xl font-black tracking-tight text-black uppercase">
  68. {t('slots.title') || '实时使馆名额雷达监控'}
  69. </h1>
  70. <p className="text-xs sm:text-sm text-neutral-600 font-mono leading-relaxed">
  71. {t('slots.subtitle') || '查看预约名额记录,并留意数据更新时间。'}
  72. </p>
  73. </div>
  74. </div>
  75. {/* Filter Bar 组件 */}
  76. <SlotFilterBar
  77. selectedCountryCode={selectedCountryCode}
  78. selectedCity={selectedCity}
  79. currentCities={currentCities}
  80. onCountryChange={handleCountryChange}
  81. onCityChange={setSelectedCity}
  82. onRefresh={fetchDashboard}
  83. loading={loading}
  84. />
  85. {/* Grid List 组件 */}
  86. <SlotGrid
  87. loading={loading}
  88. snapshots={snapshots}
  89. selectedCity={selectedCity}
  90. />
  91. </div>
  92. );
  93. }