UNPKG

@lobehub/ui

Version:

Lobe UI is an open-source UI component library for building AIGC web apps

1 lines 5.6 kB
{"version":3,"file":"index.mjs","names":["cdnFallback: CdnFn"],"sources":["../../src/ConfigProvider/index.tsx"],"sourcesContent":["'use client';\n\nimport {\n ElementType,\n ReactNode,\n createContext,\n memo,\n use,\n useEffect,\n useMemo,\n useRef,\n useState,\n} from 'react';\n\nimport { MotionComponent, type MotionComponentType } from '@/MotionProvider';\nimport type {\n I18nContextValue,\n TranslationKey,\n TranslationResourcesInput,\n TranslationResourcesMap,\n} from '@/i18n/types';\nimport { CDN, CdnApi, genCdnUrl } from '@/utils/genCdnUrl';\n\nexport interface Config {\n aAs?: ElementType;\n customCdnFn?: CdnFn;\n imgAs?: ElementType;\n imgUnoptimized?: boolean;\n proxy?: CDN | 'custom';\n}\n\nexport const ConfigContext = createContext<Config | null>(null);\n\n// Internal i18n context\nconst I18nContextInternal = createContext<I18nContextValue>({\n locale: 'en',\n t: (key: TranslationKey) => key,\n});\n\nexport interface ConfigProviderProps {\n children: ReactNode;\n config?: Config;\n // i18n props - flattened at top level\n locale?: string;\n motion: MotionComponentType;\n resources?: TranslationResourcesInput;\n}\n\nconst isThenable = (value: unknown): value is Promise<TranslationResourcesMap> =>\n typeof (value as { then?: unknown })?.then === 'function';\n\nconst ConfigProvider = memo<ConfigProviderProps>(\n ({ children, config, locale, resources, motion }) => {\n const fallbackLocale = locale ?? 'en';\n const [resolvedResources, setResolvedResources] = useState<TranslationResourcesMap | undefined>(\n () => (resources && !isThenable(resources) ? resources : undefined),\n );\n const [resolvedLocale, setResolvedLocale] = useState(fallbackLocale);\n const latestRequestId = useRef(0);\n\n useEffect(() => {\n const requestId = ++latestRequestId.current;\n\n if (!resources) {\n setResolvedResources(undefined);\n setResolvedLocale(fallbackLocale);\n return;\n }\n\n if (isThenable(resources)) {\n const targetLocale = fallbackLocale;\n resources\n .then((nextResources) => {\n if (latestRequestId.current !== requestId) return;\n setResolvedResources(nextResources);\n setResolvedLocale(targetLocale);\n })\n .catch(() => {\n if (latestRequestId.current !== requestId) return;\n });\n return;\n }\n\n setResolvedResources(resources);\n setResolvedLocale(fallbackLocale);\n }, [fallbackLocale, resources]);\n\n const currentResources = isThenable(resources) ? resolvedResources : resources;\n const currentLocale = isThenable(resources) ? resolvedLocale : fallbackLocale;\n\n const i18nValue = useMemo((): I18nContextValue => {\n const resourceList = Array.isArray(currentResources)\n ? currentResources\n : currentResources\n ? Object.values(currentResources)\n : [];\n const mergedResources = Object.assign({}, ...resourceList);\n const t = (key: TranslationKey): string => mergedResources[key] || key;\n return { locale: currentLocale, t };\n }, [currentLocale, currentResources]);\n\n return (\n <I18nContextInternal value={i18nValue}>\n <ConfigContext value={config ?? null}>\n <MotionComponent value={motion}>{children}</MotionComponent>\n </ConfigContext>\n </I18nContextInternal>\n );\n },\n);\n\n// useCdnFn\nexport type CdnFn = ({ pkg, version, path }: CdnApi) => string;\n\nconst cdnFallback: CdnFn = ({ pkg, version, path }) =>\n genCdnUrl({ path, pkg, proxy: 'aliyun', version });\n\nexport const useCdnFn = (): CdnFn => {\n const config = use(ConfigContext);\n if (!config) return cdnFallback;\n if (config?.proxy !== 'custom')\n return ({ pkg, version, path }) =>\n genCdnUrl({ path, pkg, proxy: config.proxy as any, version });\n return config?.customCdnFn || cdnFallback;\n};\n\n// useI18n hook\nexport const useI18n = () => use(I18nContextInternal);\n\n// Export I18nContext for external reference\nexport { I18nContextInternal as I18nContext };\n\nexport default ConfigProvider;\n"],"mappings":";;;;;;;;AA+BA,MAAa,gBAAgB,cAA6B,KAAK;AAG/D,MAAM,sBAAsB,cAAgC;CAC1D,QAAQ;CACR,IAAI,QAAwB;CAC7B,CAAC;AAWF,MAAM,cAAc,UAClB,OAAQ,OAA8B,SAAS;AAEjD,MAAM,iBAAiB,MACpB,EAAE,UAAU,QAAQ,QAAQ,WAAW,aAAa;CACnD,MAAM,iBAAiB,UAAU;CACjC,MAAM,CAAC,mBAAmB,wBAAwB,eACzC,aAAa,C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