import { useState, useEffect } from 'react'; import { Link, useNavigate } from 'react-router'; import { useAuth } from '../contexts/AuthContext'; import JsonUploadModal from '../components/modals/JsonUploadModal'; import EamusementModal from '../components/modals/EamusementModal'; import type { SupportedGame } from '../types/game'; import { uploadScore } from '../utils/scoreUpload'; const Import = () => { const { user, isLoading, logout } = useAuth(); const navigate = useNavigate(); const [selectedGame, setSelectedGame] = useState(''); const [isJsonModalOpen, setIsJsonModalOpen] = useState(false); const [isEamusementModalOpen, setIsEamusementModalOpen] = useState(false); const [supportedGames, setSupportedGames] = useState([]); const [gamesLoading, setGamesLoading] = useState(true); const [uploadStatus, setUploadStatus] = useState<{ type: 'success' | 'error' | null; message: string; }>({ type: null, message: '' }); useEffect(() => { const fetchSupportedGames = async () => { try { const response = await fetch(import.meta.env.VITE_API_URL+'/supportedGames'); if (!response.ok) { throw new Error('Failed to fetch supported games'); } const data = await response.json(); setSupportedGames(data); } catch (error) { console.error('Failed to fetch supported games:', error); setUploadStatus({ type: 'error', message: 'Failed to load supported games. Please refresh the page.' }); } finally { setGamesLoading(false); } }; fetchSupportedGames(); }, []); const handleLogout = async () => { try { await logout(); navigate('/'); } catch (error) { console.error('Logout failed:', error); alert('Network error during logout. Please try again.'); } }; // has to be any as this is a dynamic trackerm with dynamic score formats // eslint-disable-next-line @typescript-eslint/no-explicit-any const handleJsonUpload = async (data: any) => { try { console.log('Uploading data for game:', selectedGame, data); const result = await uploadScore({ meta: { game: data.meta.game, service: data.meta.service, playtype: data.meta.playtype }, scores: data.scores }); setUploadStatus({ type: 'success', message: `Successfully imported ${result.scoreCount} score(s) for ${supportedGames.find(g => g.internalName === data.meta.game)?.formattedName || data.meta.game}` }); setTimeout(() => { setUploadStatus({ type: null, message: '' }); }, 5000); } catch (error) { console.error('Upload failed:', error); setUploadStatus({ type: 'error', message: error instanceof Error ? error.message : 'Failed to import data. Please try again.' }); } }; const JsonUploadCard = () => (

Batch-Manual Upload

Upload your game data from a Mirage compatible JSON file

); const EamusementScrapeUploadCard = () => ( <> {/* e-amusement Card */}

e-amusement Play History

Import via scraping your playdata from KONAMI e-amusement

); const renderImportOptions = () => { switch (selectedGame) { case 'dancerush': return ( <> {/* JSON Upload Card */} ); default: return ; } }; if (isLoading) { return (

Loading import page...

); } if (!user) { return (

Session Expired

Please sign in to import your data.

Sign In Back to Home
); } return (
{/* Navigation */} {/* Main Content */}
{/* Header */}

Import Data

Import your game scores and progress from various sources

{/* Status Message */} {uploadStatus.type && (

{uploadStatus.message}

)} {/* Game Selection Card */}

Select Game

Choose the game you want to import data for

{gamesLoading ? (
Loading games...
) : ( )}
{/* Import Options */} {selectedGame && (

Import Options

{renderImportOptions()}
)}
{/* JSON Upload Modal */} setIsJsonModalOpen(false)} onUpload={handleJsonUpload} game={supportedGames.find(g => g.internalName === selectedGame)?.formattedName || ''} /> {/* Eamusement Modal */} setIsEamusementModalOpen(false)} game={supportedGames.find(g => g.internalName === selectedGame) || undefined} />
); }; export default Import;