From d607c0886ce593cf9d276bd2bed477a011b86548 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Mon, 23 Sep 2024 20:07:07 -0300 Subject: [PATCH] removing --- .../hooks/useFeaturePreviewEnableQuery.ts | 45 ++++++++++--------- .../tests/e2e/admin-feature-preview.spec.ts | 40 ----------------- .../FeaturePreview/FeaturePreviewBadge.tsx | 2 +- .../useDefaultSettingFeaturePreviewList.ts | 12 +++++ .../ui-client/src/hooks/useFeaturePreview.ts | 3 +- .../src/hooks/useFeaturePreviewList.ts | 33 +++----------- ... usePreferenceFeaturePreviewList.spec.tsx} | 3 +- .../hooks/usePreferenceFeaturePreviewList.ts | 16 +++++++ packages/ui-client/src/index.ts | 2 + 9 files changed, 64 insertions(+), 92 deletions(-) delete mode 100644 apps/meteor/tests/e2e/admin-feature-preview.spec.ts create mode 100644 packages/ui-client/src/hooks/useDefaultSettingFeaturePreviewList.ts rename packages/ui-client/src/hooks/{useFeaturePreviewList.spec.tsx => usePreferenceFeaturePreviewList.spec.tsx} (94%) create mode 100644 packages/ui-client/src/hooks/usePreferenceFeaturePreviewList.ts diff --git a/apps/meteor/client/hooks/useFeaturePreviewEnableQuery.ts b/apps/meteor/client/hooks/useFeaturePreviewEnableQuery.ts index aab037a16435..fd88f0237d29 100644 --- a/apps/meteor/client/hooks/useFeaturePreviewEnableQuery.ts +++ b/apps/meteor/client/hooks/useFeaturePreviewEnableQuery.ts @@ -1,27 +1,28 @@ import type { FeaturePreviewProps } from '@rocket.chat/ui-client'; +import { useMemo } from 'react'; -export const useFeaturePreviewEnableQuery = (features: FeaturePreviewProps[]) => { - return Object.entries( - features - .map((item) => { - if (!item.enableQuery) { - return item; - } - - const expected = item.enableQuery.value; - const received = features.find((el) => el.name === item.enableQuery?.name)?.value; - if (expected !== received) { - item.disabled = true; - item.value = false; - } else { - item.disabled = false; - } +const handleFeaturePreviewEnableQuery = (item: FeaturePreviewProps, _: any, features: FeaturePreviewProps[]) => { + if (item.enableQuery) { + const expected = item.enableQuery.value; + const received = features.find((el) => el.name === item.enableQuery?.name)?.value; + if (expected !== received) { + item.disabled = true; + item.value = false; + } else { + item.disabled = false; + } + } + return item; +}; - return item; - }) - .reduce((result, currentValue) => { - (result[currentValue.group] = result[currentValue.group] || []).push(currentValue); - return result; - }, {} as Record), +const groupFeaturePreview = (features: FeaturePreviewProps[]) => + Object.entries( + features.reduce((result, currentValue) => { + (result[currentValue.group] = result[currentValue.group] || []).push(currentValue); + return result; + }, {} as Record), ); + +export const useFeaturePreviewEnableQuery = (features: FeaturePreviewProps[]) => { + return useMemo(() => groupFeaturePreview(features.map(handleFeaturePreviewEnableQuery)), [features]); }; diff --git a/apps/meteor/tests/e2e/admin-feature-preview.spec.ts b/apps/meteor/tests/e2e/admin-feature-preview.spec.ts deleted file mode 100644 index ef962372e810..000000000000 --- a/apps/meteor/tests/e2e/admin-feature-preview.spec.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { Users } from './fixtures/userStates'; -import { test, expect } from './utils/test'; - -test.use({ storageState: Users.admin.state }); - -test.describe.serial('admin-feature-preview', () => { - test.beforeEach(async ({ page }) => { - await page.goto('/admin/feature-preview'); - }); - - test('should display the correct title', async ({ page }) => { - const title = page.locator('h1'); - await expect(title).toHaveText('Feature preview'); - }); - - test('should enable the feature preview toggle', async ({ page }) => { - const toggle = page.locator('input[name="Accounts_AllowFeaturePreview"]'); - - await toggle.check(); - - await expect(toggle).not.toBeDisabled(); - }); - - test('should save the features when Save button is clicked', async ({ page }) => { - const saveButton = page.locator('button', { hasText: 'Save changes' }); - - const toggle = page.locator('input[name="quickReactions"]'); - await toggle.check(); - - await saveButton.click(); - }); - - test('should expand the accordion and display features', async ({ page }) => { - const accordionToggle = page.locator('button', { hasText: 'Message' }); - await accordionToggle.click(); - - const feature = page.locator('label', { hasText: 'quickReactions' }); - await expect(feature).toBeVisible(); - }); -}); diff --git a/packages/ui-client/src/components/FeaturePreview/FeaturePreviewBadge.tsx b/packages/ui-client/src/components/FeaturePreview/FeaturePreviewBadge.tsx index ffa7c3d08cd6..eece30cc7280 100644 --- a/packages/ui-client/src/components/FeaturePreview/FeaturePreviewBadge.tsx +++ b/packages/ui-client/src/components/FeaturePreview/FeaturePreviewBadge.tsx @@ -1,7 +1,7 @@ import { Badge } from '@rocket.chat/fuselage'; import { useTranslation } from '@rocket.chat/ui-contexts'; -import { usePreferenceFeaturePreviewList } from '../../hooks/useFeaturePreviewList'; +import { usePreferenceFeaturePreviewList } from '../../hooks/usePreferenceFeaturePreviewList'; const FeaturePreviewBadge = () => { const t = useTranslation(); diff --git a/packages/ui-client/src/hooks/useDefaultSettingFeaturePreviewList.ts b/packages/ui-client/src/hooks/useDefaultSettingFeaturePreviewList.ts new file mode 100644 index 000000000000..373862379cc1 --- /dev/null +++ b/packages/ui-client/src/hooks/useDefaultSettingFeaturePreviewList.ts @@ -0,0 +1,12 @@ +import { useSetting } from '@rocket.chat/ui-contexts'; +import { useMemo } from 'react'; + +import { parseSetting, useFeaturePreviewList } from './useFeaturePreviewList'; + +export const useDefaultSettingFeaturePreviewList = () => { + const featurePreviewSettingJSON = useSetting('Accounts_Default_User_Preferences_featuresPreview'); + + const settingFeaturePreview = useMemo(() => parseSetting(featurePreviewSettingJSON), [featurePreviewSettingJSON]); + + return useFeaturePreviewList(settingFeaturePreview ?? []); +}; diff --git a/packages/ui-client/src/hooks/useFeaturePreview.ts b/packages/ui-client/src/hooks/useFeaturePreview.ts index 05d27879990a..bd46adfdefff 100644 --- a/packages/ui-client/src/hooks/useFeaturePreview.ts +++ b/packages/ui-client/src/hooks/useFeaturePreview.ts @@ -1,4 +1,5 @@ -import { type FeaturesAvailable, usePreferenceFeaturePreviewList } from './useFeaturePreviewList'; +import { type FeaturesAvailable } from './useFeaturePreviewList'; +import { usePreferenceFeaturePreviewList } from './usePreferenceFeaturePreviewList'; export const useFeaturePreview = (featureName: FeaturesAvailable) => { const { features } = usePreferenceFeaturePreviewList(); diff --git a/packages/ui-client/src/hooks/useFeaturePreviewList.ts b/packages/ui-client/src/hooks/useFeaturePreviewList.ts index c568f41321d8..08bda4ff81ff 100644 --- a/packages/ui-client/src/hooks/useFeaturePreviewList.ts +++ b/packages/ui-client/src/hooks/useFeaturePreviewList.ts @@ -1,6 +1,4 @@ import type { TranslationKey } from '@rocket.chat/ui-contexts'; -import { useUserPreference, useSetting } from '@rocket.chat/ui-contexts'; -import { useMemo } from 'react'; export type FeaturesAvailable = | 'quickReactions' @@ -88,36 +86,17 @@ export const defaultFeaturesPreview: FeaturePreviewProps[] = [ export const enabledDefaultFeatures = defaultFeaturesPreview.filter((feature) => feature.enabled); // TODO: Remove this logic after we have a way to store object settings. -const parseSetting = (setting: FeaturePreviewProps[] | string) => { +export const parseSetting = (setting?: FeaturePreviewProps[] | string) => { if (typeof setting === 'string') { - return JSON.parse(setting); + try { + return JSON.parse(setting) as FeaturePreviewProps[]; + } catch (_) { + return; + } } return setting; }; -export const usePreferenceFeaturePreviewList = () => { - const featurePreviewEnabled = useSetting('Accounts_AllowFeaturePreview'); - const userFeaturesPreviewPreference = useUserPreference('featuresPreview'); - const userFeaturesPreview = useMemo( - () => parseSetting(userFeaturesPreviewPreference!), - [userFeaturesPreviewPreference], - ); - const { unseenFeatures, features } = useFeaturePreviewList(userFeaturesPreview); - - if (!featurePreviewEnabled) { - return { unseenFeatures: 0, features: [] as FeaturePreviewProps[], featurePreviewEnabled }; - } - return { unseenFeatures, features, featurePreviewEnabled }; -}; - -export const useDefaultSettingFeaturePreviewList = () => { - const featurePreviewSettingJSON = useSetting('Accounts_Default_User_Preferences_featuresPreview'); - - const settingFeaturePreview = useMemo(() => parseSetting(featurePreviewSettingJSON!), [featurePreviewSettingJSON]); - - return useFeaturePreviewList(settingFeaturePreview); -}; - export const useFeaturePreviewList = (featuresList: Pick[]) => { const unseenFeatures = enabledDefaultFeatures.filter( (defaultFeature) => !featuresList?.find((feature) => feature.name === defaultFeature.name), diff --git a/packages/ui-client/src/hooks/useFeaturePreviewList.spec.tsx b/packages/ui-client/src/hooks/usePreferenceFeaturePreviewList.spec.tsx similarity index 94% rename from packages/ui-client/src/hooks/useFeaturePreviewList.spec.tsx rename to packages/ui-client/src/hooks/usePreferenceFeaturePreviewList.spec.tsx index b8782b0a554f..ac3d6f92d51a 100644 --- a/packages/ui-client/src/hooks/useFeaturePreviewList.spec.tsx +++ b/packages/ui-client/src/hooks/usePreferenceFeaturePreviewList.spec.tsx @@ -1,7 +1,8 @@ import { mockAppRoot } from '@rocket.chat/mock-providers'; import { renderHook } from '@testing-library/react'; -import { usePreferenceFeaturePreviewList, enabledDefaultFeatures } from './useFeaturePreviewList'; +import { enabledDefaultFeatures } from './useFeaturePreviewList'; +import { usePreferenceFeaturePreviewList } from './usePreferenceFeaturePreviewList'; it('should return the number of unseen features and Accounts_AllowFeaturePreview enabled ', () => { const { result } = renderHook(() => usePreferenceFeaturePreviewList(), { diff --git a/packages/ui-client/src/hooks/usePreferenceFeaturePreviewList.ts b/packages/ui-client/src/hooks/usePreferenceFeaturePreviewList.ts new file mode 100644 index 000000000000..d7c4c13417d2 --- /dev/null +++ b/packages/ui-client/src/hooks/usePreferenceFeaturePreviewList.ts @@ -0,0 +1,16 @@ +import { useSetting, useUserPreference } from '@rocket.chat/ui-contexts'; +import { useMemo } from 'react'; + +import { FeaturePreviewProps, parseSetting, useFeaturePreviewList } from './useFeaturePreviewList'; + +export const usePreferenceFeaturePreviewList = () => { + const featurePreviewEnabled = useSetting('Accounts_AllowFeaturePreview'); + const userFeaturesPreviewPreference = useUserPreference('featuresPreview'); + const userFeaturesPreview = useMemo(() => parseSetting(userFeaturesPreviewPreference), [userFeaturesPreviewPreference]); + const { unseenFeatures, features } = useFeaturePreviewList(userFeaturesPreview ?? []); + + if (!featurePreviewEnabled) { + return { unseenFeatures: 0, features: [] as FeaturePreviewProps[], featurePreviewEnabled }; + } + return { unseenFeatures, features, featurePreviewEnabled }; +}; diff --git a/packages/ui-client/src/index.ts b/packages/ui-client/src/index.ts index 3e640343da5b..a96ef265aadc 100644 --- a/packages/ui-client/src/index.ts +++ b/packages/ui-client/src/index.ts @@ -1,5 +1,7 @@ export * from './components'; export * from './hooks/useFeaturePreview'; +export * from './hooks/useDefaultSettingFeaturePreviewList'; export * from './hooks/useFeaturePreviewList'; +export * from './hooks/usePreferenceFeaturePreviewList'; export * from './hooks/useDocumentTitle'; export * from './helpers';