2022-06-01 21:27:47 +02: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';
|
|
|
|
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 ---
|
2022-06-02 17:54:50 +02:00
|
|
|
viewBlobSpace: number,
|
2022-06-01 21:27:47 +02:00
|
|
|
viewHostingLimit: number,
|
|
|
|
disabled?: boolean,
|
|
|
|
isSetting: boolean,
|
|
|
|
// --- perform ---
|
|
|
|
setDaemonSetting: (string, ?SetDaemonSettingArg) => void,
|
|
|
|
cleanBlobs: () => string,
|
|
|
|
getDaemonStatus: () => void,
|
2022-06-02 17:54:50 +02:00
|
|
|
diskSpace: DiskSpace,
|
2022-06-01 21:27:47 +02:00
|
|
|
};
|
|
|
|
|
2022-06-02 19:58:15 +02:00
|
|
|
const TWENTY_PERCENT = 0.2;
|
|
|
|
const TEN_PERCENT = 0.1;
|
|
|
|
const MINIMUM_VIEW_SETTING = '0.01';
|
|
|
|
|
2022-06-01 21:27:47 +02:00
|
|
|
function SettingViewHosting(props: Props) {
|
2022-06-02 17:54:50 +02:00
|
|
|
const {
|
|
|
|
diskSpace,
|
|
|
|
viewHostingLimit,
|
|
|
|
viewBlobSpace,
|
|
|
|
setDaemonSetting,
|
|
|
|
cleanBlobs,
|
|
|
|
getDaemonStatus,
|
|
|
|
disabled,
|
|
|
|
isSetting,
|
|
|
|
} = props;
|
2022-06-01 21:27:47 +02:00
|
|
|
|
2022-06-02 17:54:50 +02:00
|
|
|
// best effort to recommend a hosting amount default for the user
|
|
|
|
const totalMB = diskSpace && Math.floor(Number(diskSpace.total) / 1024);
|
|
|
|
const freeMB = diskSpace && Math.floor(Number(diskSpace.free) / 1024);
|
|
|
|
const getGB = (val) => (Number(val) / 1024).toFixed(2);
|
|
|
|
const recommendedSpace =
|
2022-06-02 19:58:15 +02:00
|
|
|
freeMB > totalMB * TWENTY_PERCENT // plenty of space?
|
|
|
|
? Math.ceil(Number(getGB(totalMB * TEN_PERCENT))) // 10% of total
|
2022-06-02 17:54:50 +02:00
|
|
|
: Math.ceil(Number(getGB(viewBlobSpace))); // current amount to avoid deleting
|
2022-06-01 21:27:47 +02:00
|
|
|
// daemon settings come in as 'number', but we manage them as 'String'.
|
|
|
|
const [contentBlobSpaceLimitGB, setContentBlobSpaceLimit] = React.useState(
|
2022-06-02 17:54:50 +02:00
|
|
|
viewHostingLimit === 0 ? String(recommendedSpace) : String(viewHostingLimit / 1024)
|
2022-06-01 21:27:47 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
const [unlimited, setUnlimited] = React.useState(viewHostingLimit === 0);
|
|
|
|
|
|
|
|
function handleContentLimitChange(gb) {
|
|
|
|
if (gb === '') {
|
|
|
|
setContentBlobSpaceLimit('');
|
|
|
|
} else if (gb === '0') {
|
2022-06-02 19:58:15 +02:00
|
|
|
setContentBlobSpaceLimit(MINIMUM_VIEW_SETTING); // setting 0 means unlimited.
|
2022-06-01 21:27:47 +02:00
|
|
|
} 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,
|
2022-06-02 19:58:15 +02:00
|
|
|
contentBlobSpaceLimitGB === MINIMUM_VIEW_SETTING ? '1' : convertGbToMbStr(contentBlobSpaceLimitGB)
|
2022-06-01 21:27:47 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
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 (
|
|
|
|
<>
|
|
|
|
<div className={'fieldset-section'}>
|
|
|
|
<FormField
|
|
|
|
type="checkbox"
|
|
|
|
name="hosting_limit"
|
|
|
|
checked={unlimited}
|
|
|
|
disabled={disabled || isSetting}
|
|
|
|
label={__('Unlimited View Hosting')}
|
|
|
|
onChange={() => setUnlimited(!unlimited)}
|
|
|
|
/>
|
|
|
|
<FormField
|
|
|
|
name="content_blob_limit_gb"
|
|
|
|
type="number"
|
|
|
|
min={0}
|
|
|
|
onKeyDown={handleKeyDown}
|
|
|
|
inputButton={
|
|
|
|
<>
|
|
|
|
<Button
|
|
|
|
disabled={
|
|
|
|
// disabled if settings are equal or not valid amounts
|
|
|
|
(viewHostingLimit === 1 && contentBlobSpaceLimitGB === '0') ||
|
|
|
|
(unlimited && viewHostingLimit === 0) ||
|
|
|
|
(!unlimited &&
|
|
|
|
String(viewHostingLimit) ===
|
|
|
|
convertGbToMbStr(
|
|
|
|
contentBlobSpaceLimitGB || !isValidHostingAmount(String(contentBlobSpaceLimitGB))
|
|
|
|
)) ||
|
|
|
|
isSetting ||
|
|
|
|
disabled
|
|
|
|
}
|
|
|
|
type="button"
|
|
|
|
button="alt"
|
|
|
|
onClick={handleApply}
|
|
|
|
aria-label={__('Apply')}
|
|
|
|
icon={ICONS.COMPLETE}
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
disabled={
|
|
|
|
// disabled if settings are equal or not valid amounts
|
|
|
|
(viewHostingLimit === 1 && contentBlobSpaceLimitGB === '0') ||
|
|
|
|
(unlimited && viewHostingLimit === 0) ||
|
|
|
|
(!unlimited &&
|
|
|
|
(String(viewHostingLimit) === convertGbToMbStr(contentBlobSpaceLimitGB) ||
|
|
|
|
!isValidHostingAmount(String(contentBlobSpaceLimitGB)))) ||
|
|
|
|
isSetting ||
|
|
|
|
disabled
|
|
|
|
}
|
|
|
|
type="button"
|
|
|
|
button="alt"
|
|
|
|
onClick={() => setContentBlobSpaceLimit(String(viewHostingLimit / 1024))}
|
|
|
|
aria-label={__('Reset')}
|
|
|
|
icon={ICONS.REMOVE}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
disabled={isSetting || disabled || unlimited}
|
|
|
|
onWheel={(e) => e.preventDefault()}
|
|
|
|
label={__(`View Hosting Limit (GB)`)}
|
|
|
|
onChange={(e) => handleContentLimitChange(e.target.value)}
|
2022-06-02 19:58:15 +02:00
|
|
|
value={Number(contentBlobSpaceLimitGB) <= Number(MINIMUM_VIEW_SETTING) ? '0' : contentBlobSpaceLimitGB}
|
2022-06-01 21:27:47 +02:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default SettingViewHosting;
|