Skip to content

Commit

Permalink
fix: Setting not considering license active modules
Browse files Browse the repository at this point in the history
  • Loading branch information
dougfabris committed Nov 22, 2024
1 parent 90aa786 commit 2870a27
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
6 changes: 5 additions & 1 deletion apps/meteor/client/views/admin/EditableSettingsContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ISettingBase, ISettingColor, ISetting } from '@rocket.chat/core-typings';
import type { ISettingBase, ISettingColor, ISetting, LicenseModule } from '@rocket.chat/core-typings';
import type { SettingsContextQuery } from '@rocket.chat/ui-contexts';
import { createContext, useContext, useMemo } from 'react';
import { useSyncExternalStore } from 'use-sync-external-store/shim';
Expand Down Expand Up @@ -29,6 +29,7 @@ export type EditableSettingsContextValue = {
) => [subscribe: (onStoreChange: () => void) => () => void, getSnapshot: () => ISetting['_id'][]];
readonly dispatch: (changes: Partial<EditableSetting>[]) => void;
readonly isEnterprise: boolean;
readonly activeModules: LicenseModule[];
};

export const EditableSettingsContext = createContext<EditableSettingsContextValue>({
Expand All @@ -38,10 +39,13 @@ export const EditableSettingsContext = createContext<EditableSettingsContextValu
queryGroupTabs: () => [(): (() => void) => (): void => undefined, (): ISetting['_id'][] => []],
dispatch: () => undefined,
isEnterprise: false,
activeModules: [],
});

export const useIsEnterprise = (): boolean => useContext(EditableSettingsContext).isEnterprise;

export const useLicenseActiveModules = () => useContext(EditableSettingsContext).activeModules;

export const useEditableSetting = (_id: ISetting['_id']): EditableSetting | undefined => {
const { queryEditableSetting } = useContext(EditableSettingsContext);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { FilterOperators } from 'mongodb';
import type { MutableRefObject, ReactNode } from 'react';
import React, { useEffect, useMemo, useRef } from 'react';

import { useIsEnterprise } from '../../../hooks/useIsEnterprise';
import { useLicenseBase } from '../../../hooks/useLicense';
import { createReactiveSubscriptionFactory } from '../../../lib/createReactiveSubscriptionFactory';
import type { EditableSetting, EditableSettingsContextValue } from '../EditableSettingsContext';
import { EditableSettingsContext } from '../EditableSettingsContext';
Expand Down Expand Up @@ -193,9 +193,12 @@ const EditableSettingsProvider = ({ children, query = defaultQuery, omit = defau
Tracker.flush();
});

const { data } = useIsEnterprise();
const { data } = useLicenseBase({
select: (data) => ({ isEnterprise: Boolean(data?.license.license), activeModules: data?.license.activeModules }),
});

const isEnterprise = data?.isEnterprise ?? false;
const activeModules = useMemo(() => data?.activeModules ?? [], [data]);

const contextValue = useMemo<EditableSettingsContextValue>(
() => ({
Expand All @@ -205,8 +208,9 @@ const EditableSettingsProvider = ({ children, query = defaultQuery, omit = defau
queryGroupTabs,
dispatch,
isEnterprise,
activeModules,
}),
[queryEditableSetting, queryEditableSettings, queryGroupSections, queryGroupTabs, dispatch, isEnterprise],
[queryEditableSetting, queryEditableSettings, queryGroupSections, queryGroupTabs, dispatch, isEnterprise, activeModules],
);

return <EditableSettingsContext.Provider children={children} value={contextValue} />;
Expand Down
16 changes: 13 additions & 3 deletions apps/meteor/client/views/admin/settings/Setting/Setting.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ISettingColor, SettingEditor, SettingValue } from '@rocket.chat/core-typings';
import type { ISettingColor, LicenseModule, SettingEditor, SettingValue } from '@rocket.chat/core-typings';
import { isSettingColor, isSetting } from '@rocket.chat/core-typings';
import { Box, Button, Tag } from '@rocket.chat/fuselage';
import { useDebouncedCallback } from '@rocket.chat/fuselage-hooks';
Expand All @@ -9,7 +9,7 @@ import { useTranslation } from 'react-i18next';

import MemoizedSetting from './MemoizedSetting';
import MarkdownText from '../../../../components/MarkdownText';
import { useEditableSetting, useEditableSettingsDispatch, useIsEnterprise } from '../../EditableSettingsContext';
import { useEditableSetting, useEditableSettingsDispatch, useIsEnterprise, useLicenseActiveModules } from '../../EditableSettingsContext';

type SettingProps = {
className?: string;
Expand All @@ -21,6 +21,7 @@ function Setting({ className = undefined, settingId, sectionChanged }: SettingPr
const setting = useEditableSetting(settingId);
const persistedSetting = useSettingStructure(settingId);
const isEnterprise = useIsEnterprise();
const activeModules = useLicenseActiveModules();

if (!setting || !persistedSetting) {
throw new Error(`Setting ${settingId} not found`);
Expand Down Expand Up @@ -105,12 +106,21 @@ function Setting({ className = undefined, settingId, sectionChanged }: SettingPr
) : undefined,
[i18n, i18nDescription, t],
);

const callout = useMemo(
() => alert && <span dangerouslySetInnerHTML={{ __html: i18n.exists(alert) ? t(alert) : alert }} />,
[alert, i18n, t],
);

const shouldDisableEnterprise = setting.enterprise && !isEnterprise;
const hasSettingModule = useMemo(() => {
if (!setting?.modules) {
return false;
}

return setting.modules.every((module) => activeModules.includes(module as LicenseModule));
}, [activeModules, setting?.modules]);

const shouldDisableEnterprise = setting.enterprise && !isEnterprise && !hasSettingModule;

const PRICING_URL = 'https://go.rocket.chat/i/see-paid-plan-customize-homepage';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ILivechatContact, Serialized } from '@rocket.chat/core-typings';
import { Box, Button, ButtonGroup, Callout, IconButton, Tabs, TabsItem } from '@rocket.chat/fuselage';
import { UserAvatar } from '@rocket.chat/ui-avatar';
import { usePermission, useRouter, useRouteParameter, useSetModal } from '@rocket.chat/ui-contexts';
import { usePermission, useRouteParameter, useSetModal } from '@rocket.chat/ui-contexts';
import React from 'react';
import { useTranslation } from 'react-i18next';

Expand All @@ -22,21 +22,16 @@ type ContactInfoProps = {
const ContactInfo = ({ contact, onClose }: ContactInfoProps) => {
const { t } = useTranslation();

const { getRouteName } = useRouter();
const setModal = useSetModal();
const currentRouteName = getRouteName();
const handleNavigate = useContactRoute();
const context = useRouteParameter('context');

const formatDate = useFormatDate();

const canEditContact = usePermission('edit-omnichannel-contact');

const { name, emails, phones, conflictingFields, createdAt, lastChat, contactManager, customFields: userCustomFields } = contact;

const hasConflicts = conflictingFields && conflictingFields?.length > 0;
const showContactHistory = (currentRouteName === 'live' || currentRouteName === 'omnichannel-directory') && lastChat;

const customFieldEntries = useValidCustomFields(userCustomFields);

return (
Expand Down Expand Up @@ -90,11 +85,9 @@ const ContactInfo = ({ contact, onClose }: ContactInfoProps) => {
<TabsItem onClick={() => handleNavigate({ context: 'channels' })} selected={context === 'channels'}>
{t('Channels')}
</TabsItem>
{showContactHistory && (
<TabsItem onClick={() => handleNavigate({ context: 'history' })} selected={context === 'history'}>
{t('History')}
</TabsItem>
)}
<TabsItem onClick={() => handleNavigate({ context: 'history' })} selected={context === 'history'}>
{t('History')}
</TabsItem>
</Tabs>
{context === 'details' && (
<ContactInfoDetails
Expand All @@ -106,7 +99,7 @@ const ContactInfo = ({ contact, onClose }: ContactInfoProps) => {
/>
)}
{context === 'channels' && <ContactInfoChannels contactId={contact?._id} />}
{context === 'history' && showContactHistory && <ContactInfoHistory contact={contact} />}
{context === 'history' && <ContactInfoHistory contact={contact} />}
</>
);
};
Expand Down

0 comments on commit 2870a27

Please sign in to comment.