Files
clash-proxy/src/components/layout/update-button.tsx

48 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-01-16 03:11:07 +08:00
import useSWR from "swr";
import { useRef } from "react";
2022-01-16 03:11:07 +08:00
import { Button } from "@mui/material";
2024-09-02 19:33:17 +08:00
import { check } from "@tauri-apps/plugin-updater";
import { UpdateViewer } from "../setting/mods/update-viewer";
import { DialogRef } from "../base";
import { useVerge } from "@/hooks/use-verge";
2022-01-16 03:11:07 +08:00
interface Props {
className?: string;
}
export const UpdateButton = (props: Props) => {
2022-01-16 03:11:07 +08:00
const { className } = props;
const { verge } = useVerge();
const { auto_check_update } = verge || {};
2022-01-16 03:11:07 +08:00
const viewerRef = useRef<DialogRef>(null);
const { data: updateInfo } = useSWR(
auto_check_update || auto_check_update === null ? "checkUpdate" : null,
2024-09-02 19:33:17 +08:00
check,
{
errorRetryCount: 2,
revalidateIfStale: false,
focusThrottleInterval: 36e5, // 1 hour
}
);
2022-01-16 03:11:07 +08:00
2024-09-02 19:33:17 +08:00
if (!updateInfo?.available) return null;
2022-01-16 03:11:07 +08:00
return (
<>
<UpdateViewer ref={viewerRef} />
2022-01-16 03:11:07 +08:00
<Button
color="error"
variant="contained"
size="small"
className={className}
onClick={() => viewerRef.current?.open()}
2022-01-16 03:11:07 +08:00
>
New
</Button>
</>
);
};