mirror of
https://github.com/tiddly-gittly/TidGi-Desktop.git
synced 2026-01-24 05:21:02 -08:00
feat: refresh count down wait for input to finished
This commit is contained in:
parent
25dcb08c31
commit
2ebfd522f7
1 changed files with 23 additions and 6 deletions
|
|
@ -1,31 +1,48 @@
|
|||
import React, { useCallback, useState } from 'react';
|
||||
import React, { useCallback, useState, useRef } from 'react';
|
||||
import { delay } from 'bluebird';
|
||||
import { Snackbar, Button, IconButton, Tooltip } from '@material-ui/core';
|
||||
import { Close as CloseIcon } from '@material-ui/icons';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useDebouncedFn } from 'beautiful-react-hooks';
|
||||
|
||||
export function useRestartSnackbar(waitBeforeCountDown = 500, waitBeforeRestart = 5000): [() => void, JSX.Element] {
|
||||
export function useRestartSnackbar(waitBeforeCountDown = 1000, waitBeforeRestart = 5000): [() => void, JSX.Element] {
|
||||
const { t } = useTranslation();
|
||||
const [opened, openedSetter] = useState(false);
|
||||
const [inCountDown, inCountDownSetter] = useState(false);
|
||||
const [currentWaitBeforeRestart, currentWaitBeforeRestartSetter] = useState(waitBeforeRestart);
|
||||
|
||||
const handleCloseAndRestart = useCallback(() => {
|
||||
openedSetter(false);
|
||||
// void window.service.window.requestRestart();
|
||||
inCountDownSetter(false);
|
||||
void window.service.window.requestRestart();
|
||||
}, [openedSetter]);
|
||||
|
||||
const handleCancelRestart = useCallback(() => {
|
||||
openedSetter(false);
|
||||
inCountDownSetter(false);
|
||||
}, [openedSetter]);
|
||||
|
||||
const requestRestartCountDown = useDebouncedFn(
|
||||
const startRestartCountDown = useDebouncedFn(
|
||||
() => {
|
||||
inCountDownSetter(true);
|
||||
openedSetter(true);
|
||||
},
|
||||
waitBeforeCountDown,
|
||||
{ leading: false },
|
||||
[openedSetter],
|
||||
[openedSetter, inCountDown, inCountDownSetter],
|
||||
);
|
||||
|
||||
const requestRestartCountDown = useCallback(() => {
|
||||
if (inCountDown) {
|
||||
// if already started,refresh count down of autoHideDuration, so the count down will rerun
|
||||
// so if user is editing userName in the config, count down will refresh on each onChange of Input
|
||||
currentWaitBeforeRestartSetter(currentWaitBeforeRestart + 1);
|
||||
} else {
|
||||
// of not started, we try start it
|
||||
startRestartCountDown();
|
||||
}
|
||||
}, [inCountDown, currentWaitBeforeRestart, startRestartCountDown]);
|
||||
|
||||
return [
|
||||
requestRestartCountDown,
|
||||
<div key="RestartSnackbar">
|
||||
|
|
@ -34,7 +51,7 @@ export function useRestartSnackbar(waitBeforeCountDown = 500, waitBeforeRestart
|
|||
open={opened}
|
||||
onClose={handleCloseAndRestart}
|
||||
message={t('Dialog.RestartMessage')}
|
||||
autoHideDuration={waitBeforeRestart}
|
||||
autoHideDuration={currentWaitBeforeRestart}
|
||||
action={
|
||||
<>
|
||||
<Button color="secondary" size="small" onClick={handleCloseAndRestart}>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue