45 lines
1.7 KiB
TypeScript
45 lines
1.7 KiB
TypeScript
import type { ReactNode } from "react";
|
|
import React, { createContext } from "react";
|
|
import { observer } from "mobx-react";
|
|
import { useParams } from "next/navigation";
|
|
// plane imports
|
|
import { EProductSubscriptionEnum } from "@plane/types";
|
|
// hooks
|
|
import { useFlag, useWorkspaceSubscription } from "@/plane-web/hooks/store";
|
|
import { useUserProfile } from "../store/user";
|
|
|
|
export interface AppRailContextType {
|
|
isEnabled: boolean;
|
|
shouldRenderAppRail: boolean;
|
|
toggleAppRail: (value?: boolean) => void;
|
|
}
|
|
|
|
export const AppRailContext = createContext<AppRailContextType | undefined>(undefined);
|
|
|
|
interface AppRailProviderProps {
|
|
children: ReactNode;
|
|
}
|
|
|
|
export const AppRailProvider = observer(function AppRailProvider({ children }: AppRailProviderProps) {
|
|
const { workspaceSlug } = useParams();
|
|
const { currentWorkspaceSubscribedPlanDetail: subscriptionDetail } = useWorkspaceSubscription();
|
|
|
|
const isFeatureFlagEnabled = useFlag(workspaceSlug?.toString(), "APP_RAIL");
|
|
const { updateUserProfile, data: userProfile } = useUserProfile();
|
|
const isAppRailVisible = userProfile?.is_app_rail_docked ?? true;
|
|
|
|
const isSelfManaged = !!subscriptionDetail?.is_self_managed;
|
|
const currentSubscription = subscriptionDetail?.product;
|
|
const isEnabled = isFeatureFlagEnabled && (!isSelfManaged || currentSubscription !== EProductSubscriptionEnum.FREE);
|
|
|
|
const toggleAppRail = (value?: boolean) => updateUserProfile({ is_app_rail_docked: value ?? !isAppRailVisible });
|
|
|
|
const contextValue: AppRailContextType = {
|
|
isEnabled,
|
|
shouldRenderAppRail: !!isAppRailVisible && isEnabled,
|
|
toggleAppRail,
|
|
};
|
|
|
|
return <AppRailContext.Provider value={contextValue}>{children}</AppRailContext.Provider>;
|
|
});
|