Skip to content

Commit

Permalink
🐛 Fix anonymous features hook (#750)
Browse files Browse the repository at this point in the history
  • Loading branch information
MontaGhanmy authored Nov 27, 2024
1 parent 9fefe93 commit 5cb878b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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<CompanyType>();
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 {
Expand Down
2 changes: 1 addition & 1 deletion tdrive/frontend/src/app/views/client/body/drive/shared.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down

0 comments on commit 5cb878b

Please sign in to comment.