{"version":3,"file":"MessageTranslationViewContext.mjs","names":[],"sources":["../../../src/context/MessageTranslationViewContext.tsx"],"sourcesContent":["// Whether each message shows its original or translated text. State is per message ID and the\n// provider sits in each message list, so a channel and its threads do not share it. Messages with\n// `message.i18n` default to 'translated'. The translation shown is the `${userLanguage}_text`\n// entry of `message.i18n`, falling back to `message.text`.\n\nimport React, { createContext, useCallback, useContext, useState } from 'react';\nimport type { LocalMessage, TranslationLanguage } from 'stream-chat';\n\n/** Returns the text of `message.i18n` for the language, or undefined when there is none. */\nexport const getTranslatedMessageText = ({\n  language,\n  message,\n}: {\n  language: string;\n  message?: LocalMessage;\n}): string | undefined =>\n  message?.i18n?.[`${language}_text` as `${TranslationLanguage}_text`];\n\n/**\n * Which message text to show: `'original'` is `message.text`, `'translated'` is the translation\n * for `userLanguage`, falling back to `message.text`.\n */\nexport type TranslationView = 'original' | 'translated';\n\nexport type MessageTranslationViewContextValue = {\n  getTranslationView: (messageId: string, hasI18n: boolean) => TranslationView;\n  setTranslationView: (messageId: string, view: TranslationView) => void;\n};\n\nconst defaultContextValue: MessageTranslationViewContextValue = {\n  getTranslationView: (_messageId, hasI18n) => (hasI18n ? 'translated' : 'original'),\n  // eslint-disable-next-line @typescript-eslint/no-empty-function\n  setTranslationView: () => {},\n};\n\nexport const MessageTranslationViewContext =\n  createContext<MessageTranslationViewContextValue>(defaultContextValue);\n\nexport const MessageTranslationViewProvider = ({\n  children,\n}: {\n  children: React.ReactNode;\n}) => {\n  const [viewByMessageId, setViewByMessageId] = useState<Record<string, TranslationView>>(\n    {},\n  );\n\n  const setTranslationView = useCallback((messageId: string, view: TranslationView) => {\n    setViewByMessageId((prev) => ({ ...prev, [messageId]: view }));\n  }, []);\n\n  const getTranslationView = useCallback(\n    (messageId: string, hasI18n: boolean): TranslationView =>\n      viewByMessageId[messageId] ?? (hasI18n ? 'translated' : 'original'),\n    [viewByMessageId],\n  );\n\n  const stableValue = React.useMemo(\n    () => ({ getTranslationView, setTranslationView }),\n    [getTranslationView, setTranslationView],\n  );\n\n  return (\n    <MessageTranslationViewContext.Provider value={stableValue}>\n      {children}\n    </MessageTranslationViewContext.Provider>\n  );\n};\n\n// The context has a default value, so there is no missing-provider case.\nexport const useMessageTranslationViewContext = () =>\n  useContext(MessageTranslationViewContext);\n"],"mappings":";;;;AASA,IAAa,4BAA4B,EACvC,UACA,cAKA,SAAS,OAAO,GAAG,SAAS;AAmB9B,IAAa,gCACX,cAAkD;CANlD,qBAAqB,YAAY,YAAa,UAAU,eAAe;CAEvE,0BAA0B,CAAC;AAIuB,CAAmB;AAEvE,IAAa,kCAAkC,EAC7C,eAGI;CACJ,MAAM,CAAC,iBAAiB,sBAAsB,SAC5C,CAAC,CACH;CAEA,MAAM,qBAAqB,aAAa,WAAmB,SAA0B;EACnF,oBAAoB,UAAU;GAAE,GAAG;IAAO,YAAY;EAAK,EAAE;CAC/D,GAAG,CAAC,CAAC;CAEL,MAAM,qBAAqB,aACxB,WAAmB,YAClB,gBAAgB,eAAe,UAAU,eAAe,aAC1D,CAAC,eAAe,CAClB;CAEA,MAAM,cAAc,MAAM,eACjB;EAAE;EAAoB;CAAmB,IAChD,CAAC,oBAAoB,kBAAkB,CACzC;CAEA,OACE,oBAAC,8BAA8B,UAA/B;EAAwC,OAAO;EAC5C;CACqC,CAAA;AAE5C;AAGA,IAAa,yCACX,WAAW,6BAA6B"}