import sanitizeHtml from 'sanitize-html';
import type { AuthenticationChatOption, LoadPreviousSessionChatOption } from './types';
export function createPage({
instanceId,
webhookUrl,
showWelcomeScreen,
loadPreviousSession,
i18n: { en },
initialMessages,
authentication,
allowFileUploads,
allowedFilesMimeTypes,
customCss,
enableStreaming,
}: {
instanceId: string;
webhookUrl?: string;
showWelcomeScreen?: boolean;
loadPreviousSession?: LoadPreviousSessionChatOption;
i18n: {
en: Record;
};
initialMessages: string[];
mode: 'test' | 'production';
authentication: AuthenticationChatOption;
allowFileUploads?: boolean;
allowedFilesMimeTypes?: string;
customCss?: string;
enableStreaming?: boolean;
}) {
const validAuthenticationOptions: AuthenticationChatOption[] = [
'none',
'basicAuth',
'n8nUserAuth',
];
const validLoadPreviousSessionOptions: LoadPreviousSessionChatOption[] = [
'manually',
'memory',
'notSupported',
];
const sanitizedAuthentication = validAuthenticationOptions.includes(authentication)
? authentication
: 'none';
const sanitizedShowWelcomeScreen = !!showWelcomeScreen;
const sanitizedAllowFileUploads = !!allowFileUploads;
const sanitizedAllowedFilesMimeTypes = allowedFilesMimeTypes?.toString() ?? '';
const sanitizedCustomCss = sanitizeHtml(``, {
allowedTags: ['style'],
allowedAttributes: false,
});
const sanitizedLoadPreviousSession = validLoadPreviousSessionOptions.includes(
loadPreviousSession as LoadPreviousSessionChatOption,
)
? loadPreviousSession
: 'notSupported';
return `
Chat
${sanitizedCustomCss}
`;
}