Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions Composer/packages/client/src/shell/lgApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import formatMessage from 'format-message';
import { useResolvers } from '../hooks/useResolver';
import { Dispatcher } from '../recoilModel/dispatchers';

import { dispatcherState, focusPathState } from './../recoilModel';
import { designPageLocationState, dispatcherState } from './../recoilModel';

const fileNotFound = (id: string) => formatMessage('LG file {id} not found', { id });
const TEMPLATE_ERROR = formatMessage('templateName is missing or empty');
Expand All @@ -31,13 +31,13 @@ const memoizedDebounce = (func, wait, options = {}) => {
};

function createLgApi(
state: { focusPath: string; projectId: string },
state: { dialogId: string; projectId: string },
actions: Dispatcher,
lgFileResolver: (id: string) => LgFile | undefined
): LgContextApi {
const getLgTemplates = (id) => {
if (id === undefined) throw new Error('must have a file id');
const focusedDialogId = state.focusPath.split('#').shift() || id;
const focusedDialogId = state.dialogId || id;
const file = lgFileResolver(focusedDialogId);
if (!file) throw new Error(fileNotFound(id));
return file.templates;
Expand Down Expand Up @@ -148,23 +148,22 @@ function createLgApi(
}

export function useLgApi(projectId: string) {
const focusPath = useRecoilValue(focusPathState(projectId));
const { dialogId } = useRecoilValue(designPageLocationState(projectId));
const actions: Dispatcher = useRecoilValue(dispatcherState);
const { lgFileResolver } = useResolvers(projectId);
const [api, setApi] = useState(createLgApi({ focusPath, projectId }, actions, lgFileResolver));
const [api, setApi] = useState(createLgApi({ dialogId, projectId }, actions, lgFileResolver));

useEffect(() => {
const newApi = createLgApi({ focusPath, projectId }, actions, lgFileResolver);
const newApi = createLgApi({ dialogId, projectId }, actions, lgFileResolver);
setApi(newApi);

return () => {
Object.keys(newApi).forEach((apiName) => {
if (typeof newApi[apiName].flush === 'function') {
newApi[apiName].flush();
}
});
};
}, [projectId, focusPath]);
}, [projectId, dialogId]);

return api;
}
14 changes: 7 additions & 7 deletions Composer/packages/client/src/shell/luApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import formatMessage from 'format-message';
import debounce from 'lodash/debounce';

import { useResolvers } from '../hooks/useResolver';
import { dispatcherState, focusPathState } from '../recoilModel';
import { designPageLocationState, dispatcherState } from '../recoilModel';
import { Dispatcher } from '../recoilModel/dispatchers';

const fileNotFound = (id: string) => formatMessage(`LU file {id} not found`, { id });
const INTENT_ERROR = formatMessage('intentName is missing or empty');

function createLuApi(
state: { focusPath: string; projectId: string },
state: { dialogId: string; projectId: string },
dispatchers: Dispatcher,
luFileResolver: (id: string) => LuFile | undefined
): LuContextApi {
Expand Down Expand Up @@ -65,7 +65,7 @@ function createLuApi(

const getLuIntents = (id: string): LuIntentSection[] => {
if (id === undefined) throw new Error('must have a file id');
const focusedDialogId = state.focusPath.split('#').shift() || id;
const focusedDialogId = state.dialogId || id;
const file = luFileResolver(focusedDialogId);
if (!file) throw new Error(fileNotFound(id));
return file.intents;
Expand All @@ -90,13 +90,13 @@ function createLuApi(
}

export function useLuApi(projectId: string) {
const focusPath = useRecoilValue(focusPathState(projectId));
const { dialogId } = useRecoilValue(designPageLocationState(projectId));
const dispatchers = useRecoilValue(dispatcherState);
const { luFileResolver } = useResolvers(projectId);
const [api, setApi] = useState(createLuApi({ focusPath, projectId }, dispatchers, luFileResolver));
const [api, setApi] = useState(createLuApi({ dialogId, projectId }, dispatchers, luFileResolver));

useEffect(() => {
const newApi = createLuApi({ focusPath, projectId }, dispatchers, luFileResolver);
const newApi = createLuApi({ dialogId, projectId }, dispatchers, luFileResolver);
setApi(newApi);

return () => {
Expand All @@ -106,7 +106,7 @@ export function useLuApi(projectId: string) {
}
});
};
}, [projectId, focusPath]);
}, [projectId, dialogId]);

return api;
}