From 5cb878b9ae63267a52d3b4422500fa80bf8ff7d0 Mon Sep 17 00:00:00 2001 From: Montassar Ghanmy Date: Wed, 27 Nov 2024 12:50:42 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20anonymous=20features=20hoo?= =?UTF-8?q?k=20(#750)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../feature-toggles-hooks.tsx | 37 +++++++++++++++++-- .../app/views/client/body/drive/shared.tsx | 2 +- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/tdrive/frontend/src/app/components/locked-features-components/feature-toggles-hooks.tsx b/tdrive/frontend/src/app/components/locked-features-components/feature-toggles-hooks.tsx index d14827fdd..d1a32bc12 100644 --- a/tdrive/frontend/src/app/components/locked-features-components/feature-toggles-hooks.tsx +++ b/tdrive/frontend/src/app/components/locked-features-components/feature-toggles-hooks.tsx @@ -1,18 +1,47 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ -import { useEffect } from 'react'; +import { useEffect, useState } from 'react'; +import Api from '@features/global/framework/api-service'; import FeatureTogglesService, { FeatureNames, } from '@features/global/services/feature-toggles-service'; import { FeatureToggles, Feature, withFeatures } from '@paralleldrive/react-feature-toggles'; +import RouterService from '@features/router/services/router-service'; import { useCurrentCompany } from '@features/companies/hooks/use-companies'; +import { CompanyType } from 'app/features/companies/types/company'; +import Logger from '@features/global/framework/logger-service'; -export const useFeatureToggles = () => { +export const useFeatureToggles = (anonymous = false) => { + const routeState = RouterService.getStateFromRoute(); const { activeFeatureNames } = FeatureTogglesService; - const { company } = useCurrentCompany(); + const [fetchedCompany, setFetchedCompany] = useState(); + const company = anonymous ? fetchedCompany : useCurrentCompany()?.company; + + useEffect(() => { + if (anonymous) { + const fetchCompany = async () => { + try { + if (!routeState?.companyId) return; + + const res = (await Api.get( + `/internal/services/users/v1/companies/${routeState.companyId}`, + )) as any; + if (res?.resource) { + setFetchedCompany(res.resource); // Assuming `res.resource` is structured correctly. + } + } catch (error) { + Logger.error('Error fetching company', error); + } + }; + + fetchCompany(); + } + }, [anonymous, routeState?.companyId]); useEffect(() => { const companyPlan = company?.plan; - companyPlan && FeatureTogglesService.setFeaturesFromCompanyPlan(companyPlan as any); + if (companyPlan) { + FeatureTogglesService.setFeaturesFromCompanyPlan(companyPlan as any); + } }, [JSON.stringify(company)]); return { diff --git a/tdrive/frontend/src/app/views/client/body/drive/shared.tsx b/tdrive/frontend/src/app/views/client/body/drive/shared.tsx index 77b75c62c..f8ee4fa27 100755 --- a/tdrive/frontend/src/app/views/client/body/drive/shared.tsx +++ b/tdrive/frontend/src/app/views/client/body/drive/shared.tsx @@ -29,7 +29,7 @@ import LocalStorage from 'app/features/global/framework/local-storage-service'; export default () => { const companyId = useRouterCompany(); - const { FeatureToggles, activeFeatureNames } = useFeatureToggles(); + const { FeatureToggles, activeFeatureNames } = useFeatureToggles(true); // use toggle for anonymous user on public view //Create a different local storage for shared view useEffect(() => {