UNPKG

analytica-frontend-lib

Version:

Repositório público dos componentes utilizados nas plataformas da Analytica Ensino

1 lines 3.76 kB
{"version":3,"sources":["../src/utils/chatUtils.ts"],"sourcesContent":["/**\n * Chat utility functions\n */\n\n/**\n * User info for chat from auth store\n */\nexport interface ChatUserInfo {\n userId: string;\n userName: string;\n userPhoto: string | null;\n token: string;\n}\n\n/**\n * Check if chat user info is valid (has userId and token)\n *\n * @param userInfo - Chat user info object\n * @returns boolean indicating if user info is valid for chat\n *\n * @example\n * ```ts\n * const userInfo = getChatUserInfo(user, tokens, sessionInfo);\n * if (!isChatUserInfoValid(userInfo)) {\n * return <ChatLoading />;\n * }\n * ```\n */\nexport const isChatUserInfoValid = (userInfo: ChatUserInfo): boolean => {\n return Boolean(userInfo.userId && userInfo.token);\n};\n\n/**\n * Get WebSocket URL from API URL\n * Converts https:// to wss://, http:// to ws://, and replaces bff domain with chat domain\n *\n * @param apiUrl - The API URL (e.g., 'https://bff-hml.analyticaensino.com.br')\n * @returns WebSocket URL for chat (e.g., 'wss://bff-hml-chat.analyticaensino.com.br')\n *\n * @example\n * ```ts\n * const wsUrl = getChatWsUrl('https://bff-hml.analyticaensino.com.br');\n * // Returns: 'wss://bff-hml-chat.analyticaensino.com.br'\n *\n * const wsUrl = getChatWsUrl('https://bff-prod.analyticaensino.com.br');\n * // Returns: 'wss://bff-prod-chat.analyticaensino.com.br'\n * ```\n */\nexport const getChatWsUrl = (apiUrl: string): string => {\n return apiUrl\n .replace(/^https?/, 'wss')\n .replace(/bff-hml\\./, 'bff-hml-chat.')\n .replace(/bff-prod\\./, 'bff-prod-chat.')\n .replace(/bff\\./, 'bff-chat.');\n};\n\n/**\n * Extract user info for chat from auth store data\n *\n * @param user - User object from auth store\n * @param tokens - Tokens object from auth store\n * @param sessionInfo - Session info object from auth store\n * @param defaultUserName - Default user name if not found (e.g., 'Professor' or 'Aluno')\n * @returns Chat user info\n *\n * @example\n * ```ts\n * const { user, tokens, sessionInfo } = useAuthStore();\n * const userInfo = getChatUserInfo(user, tokens, sessionInfo, 'Aluno');\n *\n * if (!isChatUserInfoValid(userInfo)) {\n * return <ChatLoading />;\n * }\n *\n * const { userId, userName, userPhoto, token } = userInfo;\n * ```\n */\nexport const getChatUserInfo = (\n user:\n | {\n userInstitutionId?: string | number;\n name?: string;\n urlProfilePicture?: string;\n }\n | null\n | undefined,\n tokens: { token?: string } | null | undefined,\n sessionInfo:\n | {\n userId?: string | number;\n userName?: string;\n urlProfilePicture?: string;\n }\n | null\n | undefined,\n defaultUserName: string = 'Usuario'\n): ChatUserInfo => {\n const userId = String(user?.userInstitutionId ?? sessionInfo?.userId ?? '');\n const userName = String(\n user?.name ?? sessionInfo?.userName ?? defaultUserName\n );\n const photoUrl = String(\n user?.urlProfilePicture ?? sessionInfo?.urlProfilePicture ?? ''\n );\n const userPhoto = photoUrl || null;\n const token = tokens?.token || '';\n\n return {\n userId,\n userName,\n userPhoto,\n token,\n };\n};\n"],"mappings":";AA4BO,IAAM,sBAAsB,CAAC,aAAoC;AACtE,SAAO,QAAQ,SAAS,UAAU,SAAS,KAAK;AAClD;AAkBO,IAAM,eAAe,CAAC,WAA2B;AACtD,SAAO,OACJ,QAAQ,WAAW,KAAK,EACxB,QAAQ,aAAa,eAAe,EACpC,QAAQ,cAAc,gBAAgB,EACtC,QAAQ,SAAS,WAAW;AACjC;AAuBO,IAAM,kBAAkB,CAC7B,MAQA,QACA,aAQA,kBAA0B,cACT;AACjB,QAAM,SAAS,OAAO,MAAM,qBAAqB,aAAa,UAAU,EAAE;AAC1E,QAAM,WAAW;AAAA,IACf,MAAM,QAAQ,aAAa,YAAY;AAAA,EACzC;AACA,QAAM,WAAW;AAAA,IACf,MAAM,qBAAqB,aAAa,qBAAqB;AAAA,EAC/D;AACA,QAAM,YAAY,YAAY;AAC9B,QAAM,QAAQ,QAAQ,SAAS;AAE/B,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;","names":[]}