Iframe should support input variables (#5156)
### 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>
This commit is contained in:
parent
a4f9aa2172
commit
0c6d787f92
@ -22,11 +22,18 @@ export const useSendButtonDisabled = (value: string) => {
|
|||||||
|
|
||||||
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,
|
||||||
@ -34,7 +41,11 @@ export const useGetSharedChatSearchParams = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
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();
|
||||||
@ -84,7 +95,8 @@ export const useSendSharedMessage = () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
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);
|
||||||
|
|||||||
@ -9,7 +9,7 @@ import { useCallback } from 'react';
|
|||||||
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,
|
||||||
@ -35,7 +35,7 @@ const FlowHeader = ({ showChatDrawer, chatDrawerVisible }: IProps) => {
|
|||||||
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();
|
||||||
@ -79,7 +79,7 @@ const FlowHeader = ({ showChatDrawer, chatDrawerVisible }: IProps) => {
|
|||||||
<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>
|
||||||
|
|||||||
@ -16,18 +16,19 @@ export const useGetBeginNodeDataQuery = () => {
|
|||||||
return getBeginNodeDataQuery;
|
return getBeginNodeDataQuery;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useGetBeginNodeDataQueryIsEmpty = () => {
|
export const useGetBeginNodeDataQueryIsSafe = () => {
|
||||||
const [isBeginNodeDataQueryEmpty, setIsBeginNodeDataQueryEmpty] =
|
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
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user