summaryrefslogtreecommitdiff
path: root/frontend/src/components/settings/pages/general/StoreSelect.tsx
blob: 80bc4930c899508b2b77ace3f5794611b9b5b6e3 (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
import { Dropdown, Field, TextField, showModal } from '@decky/ui';
import { FunctionComponent } from 'react';
import { useTranslation } from 'react-i18next';
import { FaShapes } from 'react-icons/fa';

import Logger from '../../../../logger';
import { Store } from '../../../../store';
import { WarnThirdPartyType } from '../../../../utils/globalTypes';
import { useSetting } from '../../../../utils/hooks/useSetting';
import WarnThirdParty from '../../../modals/WarnThirdParty';

const logger = new Logger('StoreSelect');

const StoreSelect: FunctionComponent<{}> = () => {
  const [selectedStore, setSelectedStore] = useSetting<Store>('store', Store.Default);
  const [selectedStoreURL, setSelectedStoreURL] = useSetting<string | null>('store-url', null);
  const [acceptedWarning, setAcceptedWarning] = useSetting<boolean>('store_select.warn.third_party', false);
  const waitTime = acceptedWarning ? 0 : 5;
  const { t } = useTranslation();
  const tStores = [
    t('StoreSelect.store_channel.default'),
    t('StoreSelect.store_channel.testing'),
    t('StoreSelect.store_channel.custom'),
  ];

  // Returns numerical values from 0 to 2 (with current branch setup as of 8/28/22)
  // 0 being Default, 1 being Testing and 2 being Custom
  return (
    <>
      <Field label={t('StoreSelect.store_channel.label')} childrenContainerWidth={'fixed'}>
        <Dropdown
          rgOptions={Object.values(Store)
            .filter((store) => typeof store == 'number')
            .map((store) => ({
              label: tStores[store as number],
              data: store,
            }))}
          selectedOption={selectedStore}
          onChange={async (newVal) => {
            await setSelectedStore(newVal.data);
            logger.log('switching stores!');
          }}
        />
      </Field>
      {selectedStore == Store.Custom &&
        showModal(
          <WarnThirdParty
            type={WarnThirdPartyType.REPO}
            seconds={waitTime}
            onOK={() => {
              setAcceptedWarning(true);
            }}
            onCancel={() => setSelectedStore(Store.Default)}
          />,
        ) && (
          <Field
            label={t('StoreSelect.custom_store.label')}
            indentLevel={1}
            description={
              <TextField
                label={t('StoreSelect.custom_store.url_label')}
                value={selectedStoreURL || undefined}
                onChange={(e) => setSelectedStoreURL(e?.target.value || null)}
              />
            }
            icon={<FaShapes style={{ display: 'block' }} />}
          ></Field>
        )}
    </>
  );
};

export default StoreSelect;