import {FontAwesomeIcon} from "@fortawesome/react-fontawesome"; import {faCloud, faCloudArrowDown, faCloudArrowUp, faSpinner} from "@fortawesome/free-solid-svg-icons"; import {useTranslations} from "next-intl"; import {useState, useContext} from "react"; import OfflineContext, {OfflineContextType} from "@/context/OfflineContext"; import System from "@/lib/models/System"; import {SessionContext, SessionContextProps} from "@/context/SessionContext"; import {LangContext} from "@/context/LangContext"; import {CompleteBook} from "@/lib/models/Book"; import {SyncType} from "@/context/BooksSyncContext"; import {AlertContext, AlertContextProps} from "@/context/AlertContext"; interface SyncBookProps { bookId: string; status: SyncType; } export default function SyncBook({bookId, status}: SyncBookProps) { const t = useTranslations(); const {session} = useContext(SessionContext); const {lang} = useContext(LangContext); const {errorMessage} = useContext(AlertContext); const {isCurrentlyOffline} = useContext(OfflineContext); const [isLoading, setIsLoading] = useState(false); const [currentStatus, setCurrentStatus] = useState(status); const isOffline: boolean = isCurrentlyOffline(); async function upload(): Promise { // TODO: Implement upload local-only book to server } async function download(): Promise { try { const response: CompleteBook = await System.authGetQueryToServer('book/sync/download', session.accessToken, lang, {bookId}); if (!response) { errorMessage(t("bookCard.downloadError")); return; } const syncStatus:boolean = await window.electron.invoke('db:book:syncSave', response); if (!syncStatus) { errorMessage(t("bookCard.downloadError")); return; } setCurrentStatus('synced'); } catch (e:unknown) { if (e instanceof Error) { errorMessage(e.message); } else { errorMessage(t("bookCard.downloadError")); } } } async function syncFromServer(): Promise { // TODO: Implement sync from server (server has newer version) } async function syncToServer(): Promise { // TODO: Implement sync to server (local has newer version) } if (isLoading) { return (
); } return (
{/* Fully synced - no action needed */} {currentStatus === 'synced' && ( )} {/* Local only - can upload to server */} {currentStatus === 'local-only' && ( )} {/* Server only - can download to local */} {currentStatus === 'server-only' && ( )} {/* Needs to sync from server (server has newer version) */} {currentStatus === 'to-sync-from-server' && ( )} {/* Needs to sync to server (local has newer version) */} {currentStatus === 'to-sync-to-server' && ( )}
); }