Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 2s
196 lines
5.9 KiB
TypeScript
196 lines
5.9 KiB
TypeScript
import type { ISettingColor, SettingEditor, SettingValue } from '@rocket.chat/core-typings';
|
|
import { isSettingColor, isSetting, isSettingCode } from '@rocket.chat/core-typings';
|
|
import { Box, Button, Tag } from '@rocket.chat/fuselage';
|
|
import { useDebouncedCallback } from '@rocket.chat/fuselage-hooks';
|
|
import { useSettingStructure } from '@rocket.chat/ui-contexts';
|
|
import { useEffect, useMemo, useState, useCallback } from 'react';
|
|
import { Trans, useTranslation } from 'react-i18next';
|
|
|
|
import MemoizedSetting from './MemoizedSetting';
|
|
import MarkdownText from '../../../../components/MarkdownText';
|
|
import { links } from '../../../../lib/links';
|
|
import { getCodeSettingError } from '../../../../lib/utils/getCodeSettingError';
|
|
import { useEditableSetting, useEditableSettingsDispatch, useEditableSettingVisibilityQuery } from '../../EditableSettingsContext';
|
|
import { useHasSettingModule } from '../hooks/useHasSettingModule';
|
|
|
|
const PRICING_URL = links.go.pricing;
|
|
|
|
export type SettingProps = {
|
|
className?: string;
|
|
settingId: string;
|
|
sectionChanged?: boolean;
|
|
};
|
|
|
|
function Setting({ className = undefined, settingId, sectionChanged }: SettingProps) {
|
|
const setting = useEditableSetting(settingId);
|
|
const persistedSetting = useSettingStructure(settingId);
|
|
const hasSettingModule = useHasSettingModule(setting);
|
|
|
|
if (!setting || !persistedSetting) {
|
|
throw new Error(`Setting ${settingId} not found`);
|
|
}
|
|
|
|
// Checks if setting has at least required fields before doing anything
|
|
if (!isSetting(setting)) {
|
|
throw new Error(`Setting ${settingId} is not valid`);
|
|
}
|
|
|
|
const dispatch = useEditableSettingsDispatch();
|
|
|
|
const settingCode = isSettingCode(persistedSetting) ? persistedSetting.code : undefined;
|
|
|
|
const update = useDebouncedCallback(
|
|
({ value, editor }: { value?: SettingValue; editor?: SettingEditor }) => {
|
|
if (!persistedSetting) {
|
|
return;
|
|
}
|
|
|
|
dispatch([
|
|
{
|
|
_id: persistedSetting._id,
|
|
...(value !== undefined && { value }),
|
|
...(editor !== undefined && { editor }),
|
|
changed:
|
|
JSON.stringify(persistedSetting.value) !== JSON.stringify(value) ||
|
|
(isSettingColor(persistedSetting) && JSON.stringify(persistedSetting.editor) !== JSON.stringify(editor)),
|
|
...(value !== undefined && { invalid: getCodeSettingError(settingCode, value) !== undefined }),
|
|
},
|
|
]);
|
|
},
|
|
230,
|
|
[persistedSetting, dispatch, settingCode],
|
|
);
|
|
|
|
const { t, i18n } = useTranslation();
|
|
|
|
const [value, setValue] = useState(setting.value);
|
|
const [editor, setEditor] = useState(isSettingColor(setting) ? setting.editor : undefined);
|
|
|
|
useEffect(() => {
|
|
setValue(setting.value);
|
|
}, [setting.value]);
|
|
|
|
useEffect(() => {
|
|
setEditor(isSettingColor(setting) ? setting.editor : undefined);
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [(setting as ISettingColor).editor]);
|
|
|
|
const onChangeValue = useCallback(
|
|
(value: SettingValue) => {
|
|
setValue(value);
|
|
if (settingCode !== undefined) {
|
|
dispatch([{ _id: persistedSetting._id, invalid: getCodeSettingError(settingCode, value) !== undefined }]);
|
|
}
|
|
update({ value });
|
|
},
|
|
[update, dispatch, settingCode, persistedSetting._id],
|
|
);
|
|
|
|
const onChangeEditor = useCallback(
|
|
(editor: SettingEditor) => {
|
|
setEditor(editor);
|
|
update({ editor });
|
|
},
|
|
[update],
|
|
);
|
|
|
|
const onResetButtonClick = useCallback(() => {
|
|
setValue(setting.value);
|
|
setEditor(isSettingColor(setting) ? setting.editor : undefined);
|
|
update({
|
|
value: persistedSetting.packageValue,
|
|
...(isSettingColor(persistedSetting) && { editor: persistedSetting.packageEditor }),
|
|
});
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [setting.value, (setting as ISettingColor).editor, update, persistedSetting]);
|
|
|
|
const { _id, readonly, type, packageValue, i18nLabel, i18nDescription, alert } = setting;
|
|
|
|
const disabled = !useEditableSettingVisibilityQuery(persistedSetting.enableQuery);
|
|
const invisible = !useEditableSettingVisibilityQuery(persistedSetting.displayQuery);
|
|
|
|
const labelText = (i18n.exists(i18nLabel) && t(i18nLabel)) || (i18n.exists(_id) && t(_id)) || i18nLabel || _id;
|
|
|
|
const hint = useMemo(
|
|
() => (i18nDescription && i18n.exists(i18nDescription) ? <MarkdownText variant='inline' content={t(i18nDescription)} /> : undefined),
|
|
[i18n, i18nDescription, t],
|
|
);
|
|
|
|
const callout = useMemo(
|
|
() =>
|
|
alert && (
|
|
<Trans
|
|
i18nKey={i18n.exists(alert) ? alert : undefined}
|
|
defaults={alert}
|
|
components={{
|
|
b: <b />,
|
|
strong: <strong />,
|
|
br: <br />,
|
|
ul: <ul />,
|
|
li: <li />,
|
|
}}
|
|
/>
|
|
),
|
|
[alert, i18n],
|
|
);
|
|
|
|
const shouldDisableEnterprise = setting.enterprise && !hasSettingModule;
|
|
|
|
const showUpgradeButton = useMemo(
|
|
() =>
|
|
shouldDisableEnterprise ? (
|
|
<Button marginBlockStart={4} is='a' href={PRICING_URL} target='_blank'>
|
|
{t('See_Paid_Plan')}
|
|
</Button>
|
|
) : undefined,
|
|
[shouldDisableEnterprise, t],
|
|
);
|
|
|
|
const label = useMemo(() => {
|
|
if (!shouldDisableEnterprise) {
|
|
return labelText;
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<Box is='span' marginInlineEnd={4}>
|
|
{labelText}
|
|
</Box>
|
|
<Tag variant='featured'>{t('Premium')}</Tag>
|
|
</>
|
|
);
|
|
}, [labelText, shouldDisableEnterprise, t]);
|
|
|
|
const hasResetButton =
|
|
!shouldDisableEnterprise &&
|
|
!readonly &&
|
|
type !== 'asset' &&
|
|
((isSettingColor(setting) && JSON.stringify(setting.packageEditor) !== JSON.stringify(editor)) ||
|
|
JSON.stringify(value) !== JSON.stringify(packageValue)) &&
|
|
!disabled;
|
|
|
|
// @todo: type check props based on setting type
|
|
|
|
return (
|
|
<MemoizedSetting
|
|
className={className}
|
|
label={label}
|
|
hint={hint}
|
|
callout={callout}
|
|
showUpgradeButton={showUpgradeButton}
|
|
sectionChanged={sectionChanged}
|
|
{...setting}
|
|
disabled={disabled || shouldDisableEnterprise}
|
|
value={value}
|
|
editor={editor}
|
|
hasResetButton={hasResetButton}
|
|
onChangeValue={onChangeValue}
|
|
onChangeEditor={onChangeEditor}
|
|
onResetButtonClick={onResetButtonClick}
|
|
invisible={invisible}
|
|
/>
|
|
);
|
|
}
|
|
|
|
export default Setting;
|