aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/client/components/SyncButton.tsx
blob: 805cb45d07ba29cc77c407d27609d3ca6cd9432c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { faArrowsRotate, faSpinner } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { useAtomValue, useSetAtom } from "jotai";
import { isOnlineAtom, isSyncingAtom, syncActionAtom } from "../atoms";

export function SyncButton() {
	const isOnline = useAtomValue(isOnlineAtom);
	const isSyncing = useAtomValue(isSyncingAtom);
	const sync = useSetAtom(syncActionAtom);

	const handleSync = async () => {
		await sync();
	};

	const isDisabled = !isOnline || isSyncing;

	return (
		<button
			type="button"
			data-testid="sync-button"
			onClick={handleSync}
			disabled={isDisabled}
			title={!isOnline ? "Cannot sync while offline" : undefined}
			className={`inline-flex items-center gap-1.5 px-3 py-1.5 rounded-lg text-sm font-medium transition-all duration-200 ${
				isDisabled
					? "bg-ivory text-muted cursor-not-allowed"
					: "bg-primary text-white hover:bg-primary-dark active:scale-[0.98]"
			}`}
		>
			{isSyncing ? (
				<>
					<FontAwesomeIcon
						icon={faSpinner}
						className="w-4 h-4 animate-spin"
						aria-hidden="true"
					/>
					<span>Syncing...</span>
				</>
			) : (
				<>
					<FontAwesomeIcon
						icon={faArrowsRotate}
						className="w-4 h-4"
						aria-hidden="true"
					/>
					<span>Sync</span>
				</>
			)}
		</button>
	);
}