@commercetools-frontend/application-shell-connectors
Version:
Contains complementary tools for @commercetools-frontend/application-shell
110 lines (109 loc) • 5.32 kB
TypeScript
import { ComponentType, ReactNode } from 'react';
import type { ApplicationWindow } from '@commercetools-frontend/constants';
import type { TFetchLoggedInUserQuery, TFetchProjectQuery, TIdTokenUserInfo } from '../../types/generated/mc';
type TFetchedUser = TFetchLoggedInUserQuery['user'];
type TFetchedProject = TFetchProjectQuery['project'];
type TApplicationContextPermissions = {
[key: string]: boolean;
};
type TActionRight = {
[key: string]: boolean;
};
type TApplicationContextActionRights = {
[key: string]: TActionRight;
};
type TApplicationContextGroupedByPermission = {
[key: string]: {
values: string[];
} | null;
};
type TApplicationContextGroupedByResourceType = {
[key: string]: TApplicationContextGroupedByPermission | null;
};
/**
* dataFence: {
* store: {
* orders: {
* canManageOrders: { values: ['usa', 'germany'] },
* canViewOrders: { values: ['canada'] },
* }
* }
* }
*/
type TApplicationContextDataFenceType = 'store';
type TApplicationContextDataFences = Partial<Record<TApplicationContextDataFenceType, TApplicationContextGroupedByResourceType>>;
type TApplicationContextEnvironment = ApplicationWindow['app'];
type TApplicationContextUser = Pick<NonNullable<TFetchedUser>, 'id' | 'email' | 'createdAt' | 'firstName' | 'lastName' | 'businessRole' | 'projects'> & {
locale: string;
timeZone: string;
idTokenUserInfo?: Omit<TIdTokenUserInfo, 'additionalClaims'> & {
additionalClaims: Record<string, unknown>;
};
};
declare const Context: import("react").Context<{}>;
export declare const mapUserToApplicationContextUser: (user?: TFetchedUser) => TApplicationContextUser | null;
export declare const mapEnvironmentToApplicationContextEnvironment: <AdditionalEnvironmentProperties extends {}>(environment: AdditionalEnvironmentProperties & import("@commercetools-frontend/constants").ApplicationRuntimeEnvironment, origin?: string) => AdditionalEnvironmentProperties & {
mcApiUrl: string;
applicationId: string;
applicationIdentifier: string;
applicationName: string;
entryPointUriPath: string;
customViewId?: string | undefined;
revision: string;
env: string;
location: string;
cdnUrl: string;
frontendHost: string;
servedByProxy: boolean;
ldClientSideId?: string | undefined;
trackingSentry?: string | undefined;
__DEVELOPMENT__?: import("@commercetools-frontend/constants").ApplicationRuntimeEnvironmentForDevelopment | undefined;
};
export declare const mapProjectToApplicationContextProject: (project?: TFetchedProject) => {
key: string;
version: number | null | undefined;
name: string;
countries: string[];
currencies: string[];
languages: string[];
ownerId: string;
ownerName: string;
sampleDataImportDataset: string | null | undefined;
isUserAdminOfCurrentProject: boolean | null | undefined;
isProductionProject: boolean;
} | null;
export type TApplicationContext<AdditionalEnvironmentProperties extends {}> = {
environment: AdditionalEnvironmentProperties & TApplicationContextEnvironment;
user: ReturnType<typeof mapUserToApplicationContextUser>;
project: ReturnType<typeof mapProjectToApplicationContextProject>;
permissions: TApplicationContextPermissions | null;
actionRights: TApplicationContextActionRights | null;
dataFences: TApplicationContextDataFences | null;
dataLocale: string | null;
};
export type ProviderProps<AdditionalEnvironmentProperties extends {}> = {
environment: AdditionalEnvironmentProperties & TApplicationContextEnvironment;
user?: TFetchedUser;
project?: TFetchedProject;
projectDataLocale?: string;
children: ReactNode;
};
type ConsumerProps<AdditionalEnvironmentProperties extends {}> = {
render: (context: TApplicationContext<AdditionalEnvironmentProperties>) => ReactNode;
children?: never;
};
declare const ApplicationContextProvider: {
<AdditionalEnvironmentProperties extends {}>(props: ProviderProps<AdditionalEnvironmentProperties>): import("@emotion/react/jsx-runtime").JSX.Element;
displayName: string;
};
declare const ApplicationContext: {
<AdditionalEnvironmentProperties extends {}>(props: ConsumerProps<AdditionalEnvironmentProperties>): import("@emotion/react/jsx-runtime").JSX.Element;
displayName: string;
};
declare function withApplicationContext<OwnProps extends {}, AdditionalEnvironmentProperties extends {}, MappedProps extends {} = {
applicationContext?: TApplicationContext<AdditionalEnvironmentProperties>;
}>(mapApplicationContextToProps?: (context: TApplicationContext<AdditionalEnvironmentProperties>) => MappedProps): (Component: ComponentType<OwnProps>) => ComponentType<OwnProps & MappedProps>;
declare function useApplicationContextHook<AdditionalEnvironmentProperties extends {} = {}>(): TApplicationContext<AdditionalEnvironmentProperties>;
declare function useApplicationContextHook<SelectedContext, AdditionalEnvironmentProperties extends {} = {}>(selector: (context: TApplicationContext<AdditionalEnvironmentProperties>) => SelectedContext): SelectedContext;
declare const useApplicationContext: typeof useApplicationContextHook;
export { Context, ApplicationContext, ApplicationContextProvider, withApplicationContext, useApplicationContext, };