Skip to content

The open alternative to Salesforce, designed for AI.

5.6 KB
Raw Blame
import {
  type AppPath,
  type SidePanelPages,
  type EnqueueSnackbarParams,
  type NavigateOptions,
} from 'twenty-shared/types';
import { type getAppPath } from 'twenty-shared/utils';

import { type FrontComponentStorageType } from '../types/FrontComponentStorageType';

export type NavigateFunction = <T extends AppPath>(
  to: T,
  params?: Parameters<typeof getAppPath<T>>[1],
  queryParams?: Record<string, any>,
  options?: NavigateOptions,
) => Promise<void>;

type OpenRoutedSidePanelPageParams<T extends AppPath> = {
  to: T;
  queryParams?: Record<string, any>;
  hash?: string;
  pageTitle?: string;
  resetNavigationStack?: boolean;
} & (T extends `${string}:${string}`
  ? { params: NonNullable<Parameters<typeof getAppPath<T>>[1]> }
  : { params?: Parameters<typeof getAppPath<T>>[1] });

type OpenPurposeBuiltSidePanelPageParams =
  | {
      // Deprecated: use `to: AppPath.RecordShowPage` with typed `params`.
      page: SidePanelPages.ViewRecord;
      recordId: string;
      objectNameSingular: string;
      tab?: string;
      resetNavigationStack?: boolean;
    }
  | {
      page: SidePanelPages.EditRichText;
      recordId: string;
      objectNameSingular: string;
      fieldName?: string;
    }
  | {
      page: SidePanelPages.ComposeEmail;
      connectedAccountId: string;
      threadId?: string;
      defaultTo?: string;
      defaultSubject?: string;
      defaultInReplyTo?: string;
      pageTitle?: string;
      pageIcon?: string;
    }
  | {
      page: SidePanelPages.ViewFrontComponent;
      frontComponentId: string;
      recordId?: string;
      selectedRecordIds?: string[];
      objectNameSingular?: string;
      pageTitle: string;
      pageIcon?: string;
      resetNavigationStack?: boolean;
    }
  | {
      page: SidePanelPages.AskAI;
      pageTitle: string;
      pageIcon?: string;
      shouldResetSearchState?: boolean;
      preprompt?: {
        text: string;
        mode?: 'PREFILL' | 'SEND';
        model?: 'FAST' | 'SMART';
      };
    }
  | {
      page: Exclude<
        SidePanelPages,
        | SidePanelPages.RoutedPage
        | SidePanelPages.ViewRecord
        | SidePanelPages.EditRichText
        | SidePanelPages.ComposeEmail
        | SidePanelPages.ViewFrontComponent
        | SidePanelPages.AskAI
        | SidePanelPages.ViewRecords
        | SidePanelPages.Copilot
      >;
      pageTitle: string;
      pageIcon?: string;
      shouldResetSearchState?: boolean;
    };

export type OpenSidePanelPageParams<T extends AppPath = AppPath> =
  | OpenRoutedSidePanelPageParams<T>
  | OpenPurposeBuiltSidePanelPageParams;

export type OpenSidePanelPageFunction = <T extends AppPath>(
  params: OpenSidePanelPageParams<T>,
) => Promise<void>;

export type CommandConfirmationModalResult = 'confirm' | 'cancel';

export type CommandConfirmationModalAccent = 'default' | 'blue' | 'danger';

export type OpenCommandConfirmationModalFunction = (params: {
  title: string;
  subtitle: string;
  confirmButtonText?: string;
  confirmButtonAccent?: CommandConfirmationModalAccent;
}) => Promise<CommandConfirmationModalResult>;

export type UnmountFrontComponentFunction = () => Promise<void>;

export type EnqueueSnackbarFunction = (
  params: EnqueueSnackbarParams,
) => Promise<void>;

export type CloseSidePanelFunction = () => Promise<void>;

export type UpdateProgressFunction = (progress: number) => Promise<void>;

export type RequestAccessTokenRefreshFunction = () => Promise<string>;

export type CopyToClipboardFunction = (text: string) => Promise<void>;

export type UploadedFrontComponentFile = {
  fileId: string;
  path: string;
  url: string;
  size: number;
  mimeType: string;
};

export type UploadFileParams = {
  // Must be a FILES field so the file attaches to a record; anything else would strand it as a temporary orphan
  fieldMetadataId: string;
  fileName?: string;
};

export type UploadFileFailureReason = 'invalid-params' | 'upload-failed';

export type UploadFileResult =
  | { status: 'uploaded'; file: UploadedFrontComponentFile }
  | { status: 'failed'; reason: UploadFileFailureReason };

export type UploadFileFunction = (
  file: Blob,
  params: UploadFileParams,
) => Promise<UploadFileResult>;

export type OpenCommandConfirmationModalHostFunction = (
  params: Parameters<OpenCommandConfirmationModalFunction>[0],
) => Promise<void>;

export type StorageSetFunction = (params: {
  storageType: FrontComponentStorageType;
  key: string;
  serializedValue: string;
}) => Promise<void>;

export type StorageDeleteFunction = (params: {
  storageType: FrontComponentStorageType;
  key: string;
}) => Promise<void>;

export type StorageClearFunction = (params: {
  storageType: FrontComponentStorageType;
}) => Promise<void>;

export type FrontComponentHostCommunicationApiStore = {
  navigate?: NavigateFunction;
  requestAccessTokenRefresh?: RequestAccessTokenRefreshFunction;
  openSidePanelPage?: OpenSidePanelPageFunction;
  openCommandConfirmationModal?: OpenCommandConfirmationModalFunction;
  unmountFrontComponent?: UnmountFrontComponentFunction;
  enqueueSnackbar?: EnqueueSnackbarFunction;
  closeSidePanel?: CloseSidePanelFunction;
  updateProgress?: UpdateProgressFunction;
  copyToClipboard?: CopyToClipboardFunction;
  uploadFile?: UploadFileFunction;
  storageSet?: StorageSetFunction;
  storageDelete?: StorageDeleteFunction;
  storageClear?: StorageClearFunction;
};

import { FRONT_COMPONENT_HOST_COMMUNICATION_API_KEY } from '../constants/front-component-host-communication-api-key';

declare global {
  var frontComponentHostCommunicationApi: FrontComponentHostCommunicationApiStore;
}

globalThis[FRONT_COMPONENT_HOST_COMMUNICATION_API_KEY] ??= {};

export const frontComponentHostCommunicationApi =
  globalThis.frontComponentHostCommunicationApi;