bbgo_origin/apps/frontend/components/SyncButton.tsx

40 lines
861 B
TypeScript
Raw Permalink Normal View History

2022-06-15 11:02:24 +00:00
import { styled } from '@mui/styles';
import React, { useEffect, useState } from 'react';
import { querySyncStatus, SyncStatus, triggerSync } from '../api/bbgo';
import useInterval from '../hooks/useInterval';
2022-06-15 03:37:05 +00:00
const ToolbarButton = styled('button')(({ theme }) => ({
padding: theme.spacing(1),
}));
export default function SyncButton() {
const [syncing, setSyncing] = useState(false);
2022-06-15 11:02:24 +00:00
2022-06-15 03:37:05 +00:00
const sync = async () => {
try {
setSyncing(true);
await triggerSync();
} catch {
setSyncing(false);
}
};
2022-06-15 11:02:24 +00:00
2022-06-15 03:37:05 +00:00
useEffect(() => {
sync();
2022-06-15 11:02:24 +00:00
}, []);
2022-06-15 03:37:05 +00:00
useInterval(() => {
2022-06-15 11:02:24 +00:00
querySyncStatus().then((s) => {
2022-06-15 03:37:05 +00:00
if (s !== SyncStatus.Syncing) {
setSyncing(false);
}
2022-06-15 11:02:24 +00:00
});
}, 2000);
2022-06-15 03:37:05 +00:00
return (
2022-06-15 11:02:24 +00:00
<ToolbarButton disabled={syncing} onClick={sync}>
2022-06-15 03:37:05 +00:00
{syncing ? 'Syncing...' : 'Sync'}
</ToolbarButton>
);
}