react-intlayer
Version:
Easily internationalize i18n your React applications with type-safe multilingual content management.
1 lines • 3.15 kB
Source Map (JSON)
{"version":3,"sources":["../../../src/server/serverContext.tsx"],"sourcesContent":["/**\n * Creates a new datastore for a given server context.\n * Attempts to closely mimic the `createContext` API.\n *\n * @example\n * const IntlayerServer = createServerContext<string | null>(null);\n *\n * <IntlayerServer value={locale}>\n * {children}\n * </IntlayerServer>\n */\nimport react, { type FC, type PropsWithChildren, type ReactNode } from 'react';\n\ntype CacheType<T> = () => { value: T | undefined };\n\nconst cacheFallback = () => () => ({ value: undefined });\n\nexport const createServerContext = <T,>(defaultValue?: T): ServerContext<T> => {\n throwInClient();\n\n /** @ts-ignore remove error Property 'cache' does not exist on type 'typeof React'. */\n const cache = react.cache<CacheType<T>> ?? cacheFallback;\n\n const getCache = cache(() => ({\n value: undefined,\n }));\n\n const Provider: FC<PropsWithChildren<{ value?: T }>> = ({\n children,\n value,\n }) => {\n getCache().value = value;\n return children;\n };\n\n const ServerContext = Provider as ServerContext<T>;\n ServerContext.Provider = Provider;\n ServerContext.Consumer = (props) => {\n const store = getCache();\n return props.children(store ? store.value : defaultValue);\n };\n ServerContext._storage = getCache;\n ServerContext._defaultValue = defaultValue;\n\n return ServerContext;\n};\n\n/**\n * Fetches a value present in a given server context.\n * Attempts to closely mimic the `useContext` API.\n *\n * @example\n * getServerContext(IntlayerServer);\n */\nexport const getServerContext = <T,>({\n _storage,\n _defaultValue,\n}: ServerContext<T>) => {\n // throwInClient();\n const store = _storage();\n if (!store) return _defaultValue;\n return store.value;\n};\n\ntype ServerContext<T> = FC<PropsWithChildren<{ value?: T }>> & {\n Provider: FC<PropsWithChildren<{ value?: T }>>;\n Consumer: FC<\n PropsWithChildren<{ children: (context: T | undefined) => ReactNode }>\n >;\n _storage: () => { value: T | undefined };\n _defaultValue: T | undefined;\n};\n\n/**\n * Throws if called within a client component environment.\n * Useful to help prevent mistakes.\n */\nconst throwInClient = (): void | never => {\n // If window.document is defined we're in a client component\n if (typeof window !== 'undefined') {\n throw new Error(`createServerContext only works in Server Components`);\n }\n};\n"],"mappings":"AAWA,OAAO,WAAgE;AAIvE,MAAM,gBAAgB,MAAM,OAAO,EAAE,OAAO,OAAU;AAE/C,MAAM,sBAAsB,CAAK,iBAAuC;AAC7E,gBAAc;AAGd,QAAM,QAAQ,MAAM,SAAuB;AAE3C,QAAM,WAAW,MAAM,OAAO;AAAA,IAC5B,OAAO;AAAA,EACT,EAAE;AAEF,QAAM,WAAiD,CAAC;AAAA,IACtD;AAAA,IACA;AAAA,EACF,MAAM;AACJ,aAAS,EAAE,QAAQ;AACnB,WAAO;AAAA,EACT;AAEA,QAAM,gBAAgB;AACtB,gBAAc,WAAW;AACzB,gBAAc,WAAW,CAAC,UAAU;AAClC,UAAM,QAAQ,SAAS;AACvB,WAAO,MAAM,SAAS,QAAQ,MAAM,QAAQ,YAAY;AAAA,EAC1D;AACA,gBAAc,WAAW;AACzB,gBAAc,gBAAgB;AAE9B,SAAO;AACT;AASO,MAAM,mBAAmB,CAAK;AAAA,EACnC;AAAA,EACA;AACF,MAAwB;AAEtB,QAAM,QAAQ,SAAS;AACvB,MAAI,CAAC,MAAO,QAAO;AACnB,SAAO,MAAM;AACf;AAeA,MAAM,gBAAgB,MAAoB;AAExC,MAAI,OAAO,WAAW,aAAa;AACjC,UAAM,IAAI,MAAM,qDAAqD;AAAA,EACvE;AACF;","names":[]}