update-button.tsx 962 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import useSWR from "swr";
  2. import { useState } from "react";
  3. import { Button } from "@mui/material";
  4. import { checkUpdate } from "@tauri-apps/api/updater";
  5. import UpdateDialog from "./update-dialog";
  6. interface Props {
  7. className?: string;
  8. }
  9. const UpdateButton = (props: Props) => {
  10. const { className } = props;
  11. const [dialogOpen, setDialogOpen] = useState(false);
  12. const { data: updateInfo } = useSWR("checkUpdate", checkUpdate, {
  13. errorRetryCount: 2,
  14. revalidateIfStale: false,
  15. focusThrottleInterval: 36e5, // 1 hour
  16. });
  17. if (!updateInfo?.shouldUpdate) return null;
  18. return (
  19. <>
  20. <Button
  21. color="error"
  22. variant="contained"
  23. size="small"
  24. className={className}
  25. onClick={() => setDialogOpen(true)}
  26. >
  27. New
  28. </Button>
  29. {dialogOpen && (
  30. <UpdateDialog open={dialogOpen} onClose={() => setDialogOpen(false)} />
  31. )}
  32. </>
  33. );
  34. };
  35. export default UpdateButton;