summaryrefslogtreecommitdiff
path: root/frontend/src/components/settings/pages/general/Updater.tsx
blob: 2772bb2cabdac5a528cb4032c6c40f5f9b705cdb (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { DialogButton, Field, ProgressBarWithInfo, Spinner } from 'decky-frontend-lib';
import { useEffect, useState } from 'react';
import { FaArrowDown } from 'react-icons/fa';

import { callUpdaterMethod, finishUpdate } from '../../../../updater';

interface VerInfo {
  current: string;
  remote: {
    assets: {
      browser_download_url: string;
      created_at: string;
    }[];
    name: string;
    body: string;
    prerelease: boolean;
    published_at: string;
    tag_name: string;
  } | null;
  updatable: boolean;
}

export default function UpdaterSettings() {
  const [versionInfo, setVersionInfo] = useState<VerInfo | null>(null);
  const [updateProgress, setUpdateProgress] = useState<number>(-1);
  const [reloading, setReloading] = useState<boolean>(false);
  useEffect(() => {
    (async () => {
      const res = (await callUpdaterMethod('get_version')) as { result: VerInfo };
      setVersionInfo(res.result);
    })();
  }, []);

  return (
    <Field
      label="Updates"
      description={
        versionInfo && (
          <span style={{ whiteSpace: 'pre-line' }}>{`Current version: ${versionInfo.current}\n${
            versionInfo.updatable ? `Latest version: ${versionInfo.remote?.tag_name}` : ''
          }`}</span>
        )
      }
      icon={
        !versionInfo ? (
          <Spinner style={{ width: '1em', height: 20, display: 'block' }} />
        ) : (
          <FaArrowDown style={{ display: 'block' }} />
        )
      }
    >
      {updateProgress == -1 ? (
        <DialogButton
          disabled={
            !versionInfo?.updatable || !versionInfo?.remote || versionInfo.remote.tag_name == versionInfo.current
          }
          onClick={async () => {
            window.DeckyUpdater = {
              updateProgress: (i) => {
                setUpdateProgress(i);
              },
              finish: async () => {
                setUpdateProgress(0);
                setReloading(true);
                await finishUpdate();
              },
            };
            setUpdateProgress(0);
            callUpdaterMethod('do_update');
          }}
        >
          Update
        </DialogButton>
      ) : (
        <ProgressBarWithInfo
          layout="inline"
          bottomSeparator={false}
          nProgress={updateProgress}
          nTransitionSec={0.01}
          indeterminate={reloading}
          sOperationText={reloading ? 'Reloading' : 'Updating'}
        />
      )}
    </Field>
  );
}