incr/resources/js/pages/dashboard.tsx

124 lines
3.5 KiB
TypeScript
Raw Normal View History

2025-07-12 19:59:22 +02:00
import LedDisplay from '@/components/Display/LedDisplay';
import SetCountForm from '@/components/Counter/SetCountForm';
2025-08-01 00:56:26 +02:00
import OnboardingFlow from '@/components/Onboarding/OnboardingFlow';
2025-08-01 01:12:21 +02:00
import TerminalSpinner from '@/components/ui/TerminalSpinner';
import { csrfToken } from '@/lib/utils';
2025-07-10 15:24:15 +02:00
import { Head } from '@inertiajs/react';
import { useCallback, useEffect, useState } from 'react';
2025-07-10 15:24:15 +02:00
export default function Dashboard() {
const [count, setCount] = useState(0);
const [formOpen, setFormOpen] = useState(false);
2025-07-10 18:04:58 +02:00
const [loading, setLoading] = useState(true);
const [incrementing, setIncrementing] = useState(false);
2025-08-01 00:56:26 +02:00
const [needsOnboarding, setNeedsOnboarding] = useState(false);
2025-07-10 18:04:58 +02:00
const loadData = useCallback(async () => {
const response = await fetch('/tracker');
if (!response.ok) {
setNeedsOnboarding(true);
return;
}
const { exists, tracker } = await response.json();
setCount(tracker?.count ?? 0);
setNeedsOnboarding(!exists);
}, []);
2025-07-10 18:04:58 +02:00
useEffect(() => {
const fetchData = async () => {
try {
await loadData();
2025-07-10 18:04:58 +02:00
} catch (error) {
console.error('Failed to fetch data:', error);
} finally {
setLoading(false);
}
};
fetchData();
}, [loadData]);
2025-07-10 18:04:58 +02:00
const increment = async () => {
if (incrementing) return;
const previous = count;
setIncrementing(true);
setCount(previous + 1);
2025-07-10 18:04:58 +02:00
try {
const response = await fetch('/increment', {
method: 'POST',
headers: {
'X-CSRF-TOKEN': csrfToken(),
Accept: 'application/json',
},
});
if (!response.ok) {
setCount(previous);
return;
2025-07-10 18:04:58 +02:00
}
const { count: updated } = await response.json();
setCount(updated);
} catch {
setCount(previous);
} finally {
setIncrementing(false);
2025-07-10 18:04:58 +02:00
}
};
if (loading) {
return (
<>
<Head title="Dashboard" />
<TerminalSpinner fullScreen />
</>
);
}
2025-08-01 00:56:26 +02:00
if (needsOnboarding) {
return (
<>
<Head title="incr - Setup" />
<OnboardingFlow onComplete={loadData} />
2025-08-01 00:56:26 +02:00
</>
);
}
2025-07-10 18:04:58 +02:00
return (
<>
<Head title="incr" />
2025-07-13 02:10:52 +02:00
2025-07-13 00:03:34 +02:00
<div className="min-h-screen bg-black">
<div className="w-full max-w-4xl mx-auto px-4">
<div className="pt-32">
<LedDisplay value={count} onClick={increment} />
2025-07-12 19:59:22 +02:00
</div>
2025-07-13 02:10:52 +02:00
<div className="text-center">
<button
type="button"
onClick={() => setFormOpen(true)}
className="text-red-400/60 hover:text-red-400 font-mono text-xs uppercase tracking-widest transition-colors"
>
[SET VALUE]
</button>
</div>
{formOpen && (
<SetCountForm
currentCount={count}
onClose={() => setFormOpen(false)}
onSuccess={setCount}
/>
)}
2025-07-12 19:59:22 +02:00
</div>
2025-07-10 15:24:15 +02:00
</div>
2025-07-10 18:04:58 +02:00
</>
2025-07-10 15:24:15 +02:00
);
}