Added sync status

This commit is contained in:
Andreas Dahm committed 2026-05-04 11:51:11 +02:00
1 parent 8b290d5a13
commit 459b5b6f2d
3 files changed
+50 -21

No files matched your search

+40 -19
View File
@@ -1,7 +1,9 @@
import React, {useEffect, useState} from 'react';
import React, {useCallback, useEffect, useState} from 'react';
import {Link, useNavigate} from 'react-router-dom';
import {useTranslation} from 'react-i18next';
import SteamGameCount from '../components/SteamGameCount';
import SyncStatusBadge from '../components/SyncStatusBadge.jsx';
import useLibrarySync from '../hooks/useLibrarySync.js';
const Dashboard = () => {
const {t} = useTranslation();
@@ -11,6 +13,24 @@ const Dashboard = () => {
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState(null);
const fetchSteamCount = useCallback(async () => {
const token = localStorage.getItem('tbp_jwt');
if (!token) return;
try {
const r = await fetch(`${import.meta.env.VITE_CORE_API_URL}/api/library/games-count`, {
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
},
});
if (r.ok) {
setSteamData(await r.json());
}
} catch (err) {
console.error('Error fetching steam data:', err);
}
}, []);
useEffect(() => {
const fetchUserData = async () => {
const token = localStorage.getItem('tbp_jwt');
@@ -33,23 +53,8 @@ const Dashboard = () => {
const data = await profileResponse.json();
setUserData(data);
// If user has a steamId, fetch game count from Core Service
if (data.steamId) {
try {
const steamResponse = await fetch(`${import.meta.env.VITE_CORE_API_URL}/api/library/games-count`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
}
});
if (steamResponse.ok) {
const steamJson = await steamResponse.json();
setSteamData(steamJson);
}
} catch (steamErr) {
console.error('Error fetching steam data:', steamErr);
}
await fetchSteamCount();
}
} else if (profileResponse.status === 401) {
localStorage.removeItem('tbp_jwt');
@@ -66,7 +71,14 @@ const Dashboard = () => {
};
fetchUserData();
}, [navigate, t]);
}, [navigate, t, fetchSteamCount]);
const expectInitialSync = !!userData?.steamId && steamData.count === 0;
const sync = useLibrarySync({
enabled: !!userData?.steamId,
expectInitialSync,
onComplete: fetchSteamCount,
});
if (isLoading) {
return (
@@ -95,6 +107,15 @@ const Dashboard = () => {
<p className="text-lg text-slate-600 dark:text-slate-300">
{userData?.dashboardMessage}
</p>
{userData?.steamId && (
<div className="mt-4">
<SyncStatusBadge
status={sync.status}
lastSyncedAt={sync.lastSyncedAt}
isRunning={sync.isRunning}
/>
</div>
)}
</div>
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
@@ -125,4 +146,4 @@ const Dashboard = () => {
);
};
export default Dashboard;
export default Dashboard;