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';
2022-06-01 21:27:47 +02:00
import * as ICONS from 'constants/icons';
import * as KEYCODES from 'constants/keycodes';
2022-02-08 23:16:22 +01:00
2022-06-01 21:27:47 +02:00
import { convertGbToMbStr, isValidHostingAmount } from 'util/hosting';
2022-02-08 23:16:22 +01:00
2022-06-01 21:27:47 +02: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,
2022-06-01 21:27:47 +02:00
isSetting: boolean,
2022-02-08 23:16:22 +01:00
};
2022-02-25 21:01:14 +01:00
function SettingDataHosting(props: Props) {
2022-06-01 21:27:47 +02:00
const { daemonSettings, setDaemonSetting, cleanBlobs, getDaemonStatus, isSetting } = props;
2022-02-08 23:16:22 +01:00
2022-06-01 21:27:47 +02:00
const networkLimitSetting = daemonSettings[DAEMON_SETTINGS.NETWORK_STORAGE_LIMIT_MB] || 0;
2022-02-08 23:16:22 +01:00
const [networkBlobSpaceLimitGB, setNetworkBlobSpaceLimit] = React.useState(
2022-06-01 21:27:47 +02:00
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
}
}
2022-06-01 21:27:47 +02: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
}
2022-06-01 21:27:47 +02:00
}
async function handleApply() {
2022-02-25 21:01:14 +01:00
await setDaemonSetting(
DAEMON_SETTINGS.NETWORK_STORAGE_LIMIT_MB,
2022-06-01 21:27:47 +02:00
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
2022-06-01 21:27:47 +02:00
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
}
2022-06-01 21:27:47 +02: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;