-
Notifications
You must be signed in to change notification settings - Fork 0
/
SaveIndicator.tsx
62 lines (56 loc) · 1.69 KB
/
SaveIndicator.tsx
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
52
53
54
55
56
57
58
59
60
61
62
import clsx from 'clsx/lite';
import {useContext, useEffect} from 'react';
import {UpdateContext} from './UpdateProvider';
export default function SaveIndicator({
saveState,
}: {
readonly saveState: 'saved' | 'saving' | 'dirty';
}) {
const {
needRefresh,
setIsHandlerOverridden: setOverrideHandler,
update,
} = useContext(UpdateContext);
useEffect(() => {
// Inform the UpdateProvider that we will handle the update ourselves
setOverrideHandler(true);
return () => {
// Clear our update
setOverrideHandler(false);
};
}, [setOverrideHandler]);
useEffect(() => {
if (!needRefresh) {
return;
}
if (saveState === 'saved') {
update();
}
// Don't do anything if we are dirty or still saving
// It will get updated once we switch to saved
}, [needRefresh, saveState, update]);
return (
<div
className={clsx(
'w-full flex flex-row justify-center pointer-events-none fixed top-2 left-0 transition-opacity ease-in-out text-2xl',
saveState === 'saved' && 'opacity-0 duration-5000',
saveState === 'dirty' && 'opacity-100 duration-1000',
saveState === 'saving' && 'opacity-100 duration-1000',
)}
>
<div className="btn py-1 px-6 shadow-md flex flex-col items-center bg-gray-900 bg-opacity-85">
<div
className={clsx(
'text-teal',
saveState === 'saved' && 'i-tabler-check',
['saving', 'dirty'].includes(saveState) &&
'i-tabler-loader-3 animate-spin',
)}
/>
<div className="leading-none text-sm">
{saveState === 'saved' ? 'saved' : 'saving'}
</div>
</div>
</div>
);
}