From 51340f2b0e33772dc4d05b9f61e6d5d8424c627b Mon Sep 17 00:00:00 2001 From: neo773 <62795688+neo773@users.noreply.github.com> Date: Fri, 1 Aug 2025 12:21:25 +0530 Subject: [PATCH] enhance globalComponentInstanceContextMap type safety (#13544) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Félix Malfait --- packages/twenty-front/src/config/index.ts | 7 ------- .../captcha/hooks/useRequestFreshCaptchaToken.ts | 7 ------- .../twenty-front/src/modules/types/custom.d.ts | 3 --- .../twenty-front/src/modules/types/global.d.ts | 15 +++++++++++++++ .../utils/globalComponentInstanceContextMap.ts | 11 ++++++----- packages/twenty-ui/src/global.d.ts | 7 +++++++ packages/twenty-ui/src/utilities/config.ts | 7 ------- 7 files changed, 28 insertions(+), 29 deletions(-) delete mode 100644 packages/twenty-front/src/modules/types/custom.d.ts create mode 100644 packages/twenty-front/src/modules/types/global.d.ts create mode 100644 packages/twenty-ui/src/global.d.ts diff --git a/packages/twenty-front/src/config/index.ts b/packages/twenty-front/src/config/index.ts index a61b4ef5335..dce2f5efd2c 100644 --- a/packages/twenty-front/src/config/index.ts +++ b/packages/twenty-front/src/config/index.ts @@ -1,10 +1,3 @@ -declare global { - interface Window { - _env_?: Record; - __APOLLO_CLIENT__?: any; - } -} - const getDefaultUrl = () => { if ( window.location.hostname === 'localhost' || diff --git a/packages/twenty-front/src/modules/captcha/hooks/useRequestFreshCaptchaToken.ts b/packages/twenty-front/src/modules/captcha/hooks/useRequestFreshCaptchaToken.ts index 6b41e61c6e6..35c3b9dcfef 100644 --- a/packages/twenty-front/src/modules/captcha/hooks/useRequestFreshCaptchaToken.ts +++ b/packages/twenty-front/src/modules/captcha/hooks/useRequestFreshCaptchaToken.ts @@ -7,13 +7,6 @@ import { captchaState } from '@/client-config/states/captchaState'; import { CaptchaDriverType } from '~/generated-metadata/graphql'; import { isUndefinedOrNull } from '~/utils/isUndefinedOrNull'; -declare global { - interface Window { - grecaptcha?: any; - turnstile?: any; - } -} - export const useRequestFreshCaptchaToken = () => { const setCaptchaToken = useSetRecoilState(captchaTokenState); const setIsRequestingCaptchaToken = useSetRecoilState( diff --git a/packages/twenty-front/src/modules/types/custom.d.ts b/packages/twenty-front/src/modules/types/custom.d.ts deleted file mode 100644 index bc54aabebfb..00000000000 --- a/packages/twenty-front/src/modules/types/custom.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -declare interface Window { - FrontChat?: (method: string, ...args: any[]) => void; -} diff --git a/packages/twenty-front/src/modules/types/global.d.ts b/packages/twenty-front/src/modules/types/global.d.ts new file mode 100644 index 00000000000..8927ab3b871 --- /dev/null +++ b/packages/twenty-front/src/modules/types/global.d.ts @@ -0,0 +1,15 @@ +import { ComponentInstanceStateContext } from '@/ui/utilities/state/component-state/types/ComponentInstanceStateContext'; + +declare global { + interface Window { + _env_?: Record; + __APOLLO_CLIENT__?: any; + grecaptcha?: any; + turnstile?: any; + componentComponentStateContextMap: Map< + string, + ComponentInstanceStateContext + >; + FrontChat?: (method: string, ...args: any[]) => void; + } +} diff --git a/packages/twenty-front/src/modules/ui/utilities/state/component-state/utils/globalComponentInstanceContextMap.ts b/packages/twenty-front/src/modules/ui/utilities/state/component-state/utils/globalComponentInstanceContextMap.ts index f1b233167a3..d181b18a526 100644 --- a/packages/twenty-front/src/modules/ui/utilities/state/component-state/utils/globalComponentInstanceContextMap.ts +++ b/packages/twenty-front/src/modules/ui/utilities/state/component-state/utils/globalComponentInstanceContextMap.ts @@ -3,17 +3,18 @@ import { isDefined } from 'twenty-shared/utils'; class ComponentInstanceContextMap { constructor() { - if (!isDefined((window as any).componentComponentStateContextMap)) { - (window as any).componentComponentStateContextMap = new Map(); + if (!isDefined(window.componentComponentStateContextMap)) { + window.componentComponentStateContextMap = new Map(); } } - public get(key: string): ComponentInstanceStateContext { - return (window as any).componentComponentStateContextMap.get(key); + public get(key: string): ComponentInstanceStateContext | undefined { + const context = window.componentComponentStateContextMap.get(key); + return context; } public set(key: string, context: ComponentInstanceStateContext) { - (window as any).componentComponentStateContextMap.set(key, context); + window.componentComponentStateContextMap.set(key, context); } } diff --git a/packages/twenty-ui/src/global.d.ts b/packages/twenty-ui/src/global.d.ts new file mode 100644 index 00000000000..e7b65519422 --- /dev/null +++ b/packages/twenty-ui/src/global.d.ts @@ -0,0 +1,7 @@ +declare global { + interface Window { + _env_?: Record; + } +} + +export {}; diff --git a/packages/twenty-ui/src/utilities/config.ts b/packages/twenty-ui/src/utilities/config.ts index 5a9b2ce3986..91782a631ec 100644 --- a/packages/twenty-ui/src/utilities/config.ts +++ b/packages/twenty-ui/src/utilities/config.ts @@ -1,10 +1,3 @@ -declare global { - interface Window { - _env_?: Record; - __APOLLO_CLIENT__?: any; - } -} - const getDefaultUrl = () => { if ( window.location.hostname === 'localhost' ||