lbry-desktop/ui/component/settingDataHosting/view.jsx

117 lines
3.5 KiB
React
Raw Normal View History

2022-02-08 23:16:22 +01:00
// @flow
import React from 'react';
import { FormField } from 'component/common/form';
import Button from 'component/button';
import * as DAEMON_SETTINGS from 'constants/daemon_settings';
2022-02-25 21:01:14 +01:00
import { isTrulyANumber } from 'util/number';
import * as ICONS from 'constants/icons';
import * as KEYCODES from 'constants/keycodes';
2022-02-08 23:16:22 +01:00
import { convertGbToMbStr, isValidHostingAmount } from 'util/hosting';
2022-02-08 23:16:22 +01:00
type SetDaemonSettingArg = boolean | string | number;
2022-02-08 23:16:22 +01:00
type DaemonSettings = {
save_blobs: boolean,
};
type Props = {
// --- select ---
daemonSettings: DaemonSettings,
// --- perform ---
setDaemonSetting: (string, ?SetDaemonSettingArg) => void,
2022-02-25 21:01:14 +01:00
cleanBlobs: () => string,
getDaemonStatus: () => void,
isSetting: boolean,
2022-02-08 23:16:22 +01:00
};
2022-02-25 21:01:14 +01:00
function SettingDataHosting(props: Props) {
const { daemonSettings, setDaemonSetting, cleanBlobs, getDaemonStatus, isSetting } = props;
2022-02-08 23:16:22 +01:00
const networkLimitSetting = daemonSettings[DAEMON_SETTINGS.NETWORK_STORAGE_LIMIT_MB] || 0;
2022-02-08 23:16:22 +01:00
const [networkBlobSpaceLimitGB, setNetworkBlobSpaceLimit] = React.useState(
networkLimitSetting ? String(networkLimitSetting / 1024) : 0
2022-02-08 23:16:22 +01:00
);
2022-02-25 21:01:14 +01:00
function handleNetworkLimitChange(gb) {
if (gb === '') {
setNetworkBlobSpaceLimit('');
2022-02-08 23:16:22 +01:00
} else {
2022-02-25 21:01:14 +01:00
const numberGb = Number(gb);
if (isTrulyANumber(numberGb)) {
setNetworkBlobSpaceLimit(gb);
}
2022-02-08 23:16:22 +01:00
}
}
function handleKeyDown(e) {
if (e.keyCode === KEYCODES.ESCAPE) {
e.preventDefault();
setNetworkBlobSpaceLimit(String(networkLimitSetting / 1024));
} else if (e.keyCode === KEYCODES.ENTER) {
e.preventDefault();
handleApply();
2022-02-08 23:16:22 +01:00
}
}
async function handleApply() {
2022-02-25 21:01:14 +01:00
await setDaemonSetting(
DAEMON_SETTINGS.NETWORK_STORAGE_LIMIT_MB,
String(convertGbToMbStr(Number(networkBlobSpaceLimitGB)))
2022-02-25 21:01:14 +01:00
);
await cleanBlobs();
getDaemonStatus();
2022-02-08 23:16:22 +01:00
}
return (
<>
2022-02-25 21:01:14 +01:00
<div className={'fieldset-section'}>
2022-02-08 23:16:22 +01:00
<FormField
name="network_blob_limit_gb"
type="number"
label={__(`Automatic Hosting (GB)`)}
disabled={!daemonSettings.save_blobs || isSetting}
onKeyDown={handleKeyDown}
inputButton={
<>
<Button
disabled={
// disabled if settings are equal or not valid amounts
String(networkLimitSetting) === convertGbToMbStr(networkBlobSpaceLimitGB) ||
!isValidHostingAmount(String(networkBlobSpaceLimitGB)) ||
isSetting ||
!daemonSettings.save_blobs
}
type="button"
button="alt"
onClick={handleApply}
aria-label={__('Apply')}
icon={ICONS.COMPLETE}
/>
<Button
disabled={
// disabled if settings are equal or not valid amounts
String(networkLimitSetting) === convertGbToMbStr(networkBlobSpaceLimitGB) ||
!isValidHostingAmount(String(networkBlobSpaceLimitGB)) ||
isSetting ||
!daemonSettings.save_blobs
}
type="button"
button="alt"
onClick={() => setNetworkBlobSpaceLimit(String(networkLimitSetting / 1024))}
aria-label={__('Reset')}
icon={ICONS.REMOVE}
/>
</>
2022-02-25 21:01:14 +01:00
}
onChange={(e) => handleNetworkLimitChange(e.target.value)}
value={networkBlobSpaceLimitGB}
2022-02-25 21:01:14 +01:00
/>
</div>
2022-02-08 23:16:22 +01:00
</>
);
}
2022-02-25 21:01:14 +01:00
export default SettingDataHosting;