import { useContext, useEffect, useState } from "react"; import { ErrorDetails as ApiErrorDetails } from "./api-types"; import { APIContext } from "./context"; import { RootContent } from "./components/RootContent"; import { customSetInterval } from "./helper/customSetInterval"; import { IconButton } from "./components/buttons/IconButton"; import { BsBodyText, BsMoon } from "react-icons/bs"; import { LogStreamModal } from "./components/modal/LogStreamModal"; import { Header } from "./components/Header"; import { DarkMode } from "./helper/darkMode"; import { useTorrentStore } from "./stores/torrentStore"; import { useErrorStore } from "./stores/errorStore"; export interface ErrorWithLabel { text: string; details?: ApiErrorDetails; } export interface ContextType { setCloseableError: (error: ErrorWithLabel | null) => void; refreshTorrents: () => void; } export const RqbitWebUI = (props: { title: string; version: string; menuButtons?: JSX.Element[]; }) => { let [logsOpened, setLogsOpened] = useState(false); const setOtherError = useErrorStore((state) => state.setOtherError); const API = useContext(APIContext); const setTorrents = useTorrentStore((state) => state.setTorrents); const setTorrentsLoading = useTorrentStore( (state) => state.setTorrentsLoading, ); const setRefreshTorrents = useTorrentStore( (state) => state.setRefreshTorrents, ); const refreshTorrents = async () => { setTorrentsLoading(true); let torrents = await API.listTorrents().finally(() => setTorrentsLoading(false), ); setTorrents(torrents.torrents); }; setRefreshTorrents(refreshTorrents); useEffect(() => { return customSetInterval( async () => refreshTorrents().then( () => { setOtherError(null); return 5000; }, (e) => { setOtherError({ text: "Error refreshing torrents", details: e }); console.error(e); return 5000; }, ), 0, ); }, []); return (
{/* Menu buttons */}
{props.menuButtons && props.menuButtons.map((b, i) => {b})} setLogsOpened(true)}>
setLogsOpened(false)} />
); };