### What problem does this PR solve? Right now we cannot embed a chat in website when it has variables in the begin component. This PR tries to read the variables values from the query string via a data_ prefixed variable. #5016 ### Type of change - [x] New Feature (non-breaking change which adds functionality) --------- Co-authored-by: gstrat88 <gstrat@innews.gr>tags/v0.17.0
| export const useGetSharedChatSearchParams = () => { | export const useGetSharedChatSearchParams = () => { | ||||
| const [searchParams] = useSearchParams(); | const [searchParams] = useSearchParams(); | ||||
| const data_prefix = 'data_'; | |||||
| const data = Object.fromEntries( | |||||
| searchParams | |||||
| .entries() | |||||
| .filter(([key, value]) => key.startsWith(data_prefix)) | |||||
| .map(([key, value]) => [key.replace(data_prefix, ''), value]), | |||||
| ); | |||||
| return { | return { | ||||
| from: searchParams.get('from') as SharedFrom, | from: searchParams.get('from') as SharedFrom, | ||||
| sharedId: searchParams.get('shared_id'), | sharedId: searchParams.get('shared_id'), | ||||
| locale: searchParams.get('locale'), | locale: searchParams.get('locale'), | ||||
| data: data, | |||||
| visibleAvatar: searchParams.get('visible_avatar') | visibleAvatar: searchParams.get('visible_avatar') | ||||
| ? searchParams.get('visible_avatar') !== '1' | ? searchParams.get('visible_avatar') !== '1' | ||||
| : true, | : true, | ||||
| }; | }; | ||||
| export const useSendSharedMessage = () => { | export const useSendSharedMessage = () => { | ||||
| const { from, sharedId: conversationId } = useGetSharedChatSearchParams(); | |||||
| const { | |||||
| from, | |||||
| sharedId: conversationId, | |||||
| data: data, | |||||
| } = useGetSharedChatSearchParams(); | |||||
| const { createSharedConversation: setConversation } = | const { createSharedConversation: setConversation } = | ||||
| useCreateNextSharedConversation(); | useCreateNextSharedConversation(); | ||||
| const { handleInputChange, value, setValue } = useHandleMessageInputChange(); | const { handleInputChange, value, setValue } = useHandleMessageInputChange(); | ||||
| ); | ); | ||||
| const fetchSessionId = useCallback(async () => { | const fetchSessionId = useCallback(async () => { | ||||
| const ret = await send({ question: '' }); | |||||
| const payload = { question: '' }; | |||||
| const ret = await send({ ...payload, ...data }); | |||||
| if (isCompletionError(ret)) { | if (isCompletionError(ret)) { | ||||
| message.error(ret?.data.message); | message.error(ret?.data.message); | ||||
| setHasError(true); | setHasError(true); |
| import { Link, useParams } from 'umi'; | import { Link, useParams } from 'umi'; | ||||
| import { | import { | ||||
| useGetBeginNodeDataQuery, | useGetBeginNodeDataQuery, | ||||
| useGetBeginNodeDataQueryIsEmpty, | |||||
| useGetBeginNodeDataQueryIsSafe, | |||||
| } from '../hooks/use-get-begin-query'; | } from '../hooks/use-get-begin-query'; | ||||
| import { | import { | ||||
| useSaveGraph, | useSaveGraph, | ||||
| const getBeginNodeDataQuery = useGetBeginNodeDataQuery(); | const getBeginNodeDataQuery = useGetBeginNodeDataQuery(); | ||||
| const { showEmbedModal, hideEmbedModal, embedVisible, beta } = | const { showEmbedModal, hideEmbedModal, embedVisible, beta } = | ||||
| useShowEmbedModal(); | useShowEmbedModal(); | ||||
| const isBeginNodeDataQueryEmpty = useGetBeginNodeDataQueryIsEmpty(); | |||||
| const isBeginNodeDataQuerySafe = useGetBeginNodeDataQueryIsSafe(); | |||||
| const handleShowEmbedModal = useCallback(() => { | const handleShowEmbedModal = useCallback(() => { | ||||
| showEmbedModal(); | showEmbedModal(); | ||||
| <Button | <Button | ||||
| type="primary" | type="primary" | ||||
| onClick={handleShowEmbedModal} | onClick={handleShowEmbedModal} | ||||
| disabled={!isBeginNodeDataQueryEmpty} | |||||
| disabled={!isBeginNodeDataQuerySafe} | |||||
| > | > | ||||
| <b>{t('embedIntoSite', { keyPrefix: 'common' })}</b> | <b>{t('embedIntoSite', { keyPrefix: 'common' })}</b> | ||||
| </Button> | </Button> |
| return getBeginNodeDataQuery; | return getBeginNodeDataQuery; | ||||
| }; | }; | ||||
| export const useGetBeginNodeDataQueryIsEmpty = () => { | |||||
| const [isBeginNodeDataQueryEmpty, setIsBeginNodeDataQueryEmpty] = | |||||
| export const useGetBeginNodeDataQueryIsSafe = () => { | |||||
| const [isBeginNodeDataQuerySafe, setIsBeginNodeDataQuerySafe] = | |||||
| useState(false); | useState(false); | ||||
| const getBeginNodeDataQuery = useGetBeginNodeDataQuery(); | const getBeginNodeDataQuery = useGetBeginNodeDataQuery(); | ||||
| const nodes = useGraphStore((state) => state.nodes); | const nodes = useGraphStore((state) => state.nodes); | ||||
| useEffect(() => { | useEffect(() => { | ||||
| const query: BeginQuery[] = getBeginNodeDataQuery(); | const query: BeginQuery[] = getBeginNodeDataQuery(); | ||||
| setIsBeginNodeDataQueryEmpty(query.length === 0); | |||||
| const isSafe = !query.some((q) => !q.optional && q.type === 'file'); | |||||
| setIsBeginNodeDataQuerySafe(isSafe); | |||||
| }, [getBeginNodeDataQuery, nodes]); | }, [getBeginNodeDataQuery, nodes]); | ||||
| return isBeginNodeDataQueryEmpty; | |||||
| return isBeginNodeDataQuerySafe; | |||||
| }; | }; | ||||
| // exclude nodes with branches | // exclude nodes with branches |