// @flow import React from 'react'; import { FormField } from 'component/common/form'; import Button from 'component/button'; import * as DAEMON_SETTINGS from 'constants/daemon_settings'; import { isTrulyANumber } from 'util/number'; import * as ICONS from 'constants/icons'; import * as KEYCODES from 'constants/keycodes'; import { convertGbToMbStr, isValidHostingAmount } from 'util/hosting'; type SetDaemonSettingArg = boolean | string | number; type Props = { // --- select --- viewHostingLimit: number, disabled?: boolean, isSetting: boolean, // --- perform --- setDaemonSetting: (string, ?SetDaemonSettingArg) => void, cleanBlobs: () => string, getDaemonStatus: () => void, }; function SettingViewHosting(props: Props) { const { viewHostingLimit, setDaemonSetting, cleanBlobs, getDaemonStatus, disabled, isSetting } = props; // daemon settings come in as 'number', but we manage them as 'String'. const [contentBlobSpaceLimitGB, setContentBlobSpaceLimit] = React.useState( viewHostingLimit === 0 ? '0.01' : String(viewHostingLimit / 1024) ); const [unlimited, setUnlimited] = React.useState(viewHostingLimit === 0); function handleContentLimitChange(gb) { if (gb === '') { setContentBlobSpaceLimit(''); } else if (gb === '0') { setContentBlobSpaceLimit('0.01'); // setting 0 means unlimited. } else { if (isTrulyANumber(Number(gb))) { setContentBlobSpaceLimit(gb); } } } async function handleApply() { if (unlimited) { await setDaemonSetting(DAEMON_SETTINGS.BLOB_STORAGE_LIMIT_MB, '0'); } else { await setDaemonSetting( DAEMON_SETTINGS.BLOB_STORAGE_LIMIT_MB, String(contentBlobSpaceLimitGB === '0.01' ? '1' : convertGbToMbStr(contentBlobSpaceLimitGB)) ); } await cleanBlobs(); getDaemonStatus(); } function handleKeyDown(e) { if (e.keyCode === KEYCODES.ESCAPE) { e.preventDefault(); setContentBlobSpaceLimit(String(viewHostingLimit / 1024)); } else if (e.keyCode === KEYCODES.ENTER) { e.preventDefault(); handleApply(); } } React.useEffect(() => { if (unlimited) { handleApply(); } }, [unlimited]); return ( <>
setUnlimited(!unlimited)} />
); } export default SettingViewHosting;