@inploi/plugin-chatbot 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env +3 -0
- package/CHANGELOG.md +8 -0
- package/package.json +2 -2
- package/src/chatbot.dom.ts +0 -4
- package/src/chatbot.ts +57 -64
- package/src/index.dev.ts +2 -2
package/.env
ADDED
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inploi/plugin-chatbot",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@hookform/resolvers": "^3.3.2",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"typescript": "^5.1.6",
|
|
43
43
|
"vite": "^4.4.5",
|
|
44
44
|
"vite-tsconfig-paths": "^4.2.1",
|
|
45
|
-
"@inploi/sdk": "1.0.
|
|
45
|
+
"@inploi/sdk": "1.0.1",
|
|
46
46
|
"eslint-config-custom": "0.1.0",
|
|
47
47
|
"tsconfig": "0.1.0"
|
|
48
48
|
},
|
package/src/chatbot.dom.ts
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
import { CHATBOT_ELEMENT_ID } from './chatbot.constants';
|
|
2
2
|
|
|
3
|
-
function assertElementExists(chatbotElement: HTMLDivElement | null): asserts chatbotElement is HTMLDivElement {
|
|
4
|
-
if (!chatbotElement) throw new Error('Chatbot element not found');
|
|
5
|
-
}
|
|
6
|
-
|
|
7
3
|
export const overlayClassNames =
|
|
8
4
|
'data-[transition=entering]:opacity-0 data-[transition=exiting]:opacity-0 ease-expo-out duration-500 transition-opacity bg-neutral-12/60 fixed inset-0';
|
|
9
5
|
|
package/src/chatbot.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createPlugin } from '@inploi/sdk';
|
|
2
2
|
import { h, render } from 'preact';
|
|
3
3
|
|
|
4
4
|
import { getApplicationData } from './chatbot.api';
|
|
@@ -6,72 +6,65 @@ import './chatbot.css';
|
|
|
6
6
|
import { ChatbotDomManager, createChatbotDomManager } from './chatbot.dom';
|
|
7
7
|
import { useChatbotStore, useLocalState } from './chatbot.state';
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
9
|
+
export const chatbotPlugin = ({
|
|
10
|
+
_internal_domManager: dom = createChatbotDomManager(),
|
|
11
|
+
geolocationApiKey,
|
|
12
|
+
}: {
|
|
13
|
+
geolocationApiKey?: string;
|
|
14
|
+
_internal_domManager?: ChatbotDomManager;
|
|
15
|
+
}) =>
|
|
16
|
+
createPlugin(({ apiClient, logger }) => {
|
|
17
|
+
const { resetLocalState } = useLocalState.getState();
|
|
18
|
+
const { startApplication, cancelCurrentApplication } = useChatbotStore.getState();
|
|
17
19
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
20
|
+
return {
|
|
21
|
+
/** Optionally eagerly loads the code to handle applications. */
|
|
22
|
+
prepare: async () => {
|
|
23
|
+
try {
|
|
24
|
+
await import('~/ui/chatbot');
|
|
25
|
+
logger?.info('Chatbot plugin prepared');
|
|
26
|
+
} catch (error) {
|
|
27
|
+
console.error(error);
|
|
28
|
+
logger?.error('Error preparing chatbot plugin', error);
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
startApplication: async ({ jobId }: { jobId: string }) => {
|
|
32
|
+
try {
|
|
33
|
+
const chatbotElement = dom.getOrCreateChatbotElement();
|
|
34
|
+
cancelCurrentApplication();
|
|
33
35
|
|
|
34
|
-
|
|
36
|
+
dom.renderLoading(chatbotElement);
|
|
35
37
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
/** We concurrently lazy load the module and data */
|
|
39
|
+
const [dataPromise, chatbotPromise] = await Promise.allSettled([
|
|
40
|
+
getApplicationData({ jobId, apiClient }),
|
|
41
|
+
import('~/ui/chatbot').then(m => m.Chatbot),
|
|
42
|
+
]);
|
|
41
43
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const Chatbot = chatbotPromise.value;
|
|
46
|
-
const application = dataPromise.value;
|
|
47
|
-
startApplication(application);
|
|
48
|
-
logger?.info(`Starting application for job "${application.job.id}" using flow "${application.flow.id}"`);
|
|
49
|
-
chatbotElement.innerHTML = '';
|
|
44
|
+
if (dataPromise.status === 'rejected') throw dataPromise.reason;
|
|
45
|
+
if (chatbotPromise.status === 'rejected') throw chatbotPromise.reason;
|
|
50
46
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
},
|
|
57
|
-
closeApplication: async () => {
|
|
58
|
-
const chatbotElement = dom.getOrCreateChatbotElement();
|
|
59
|
-
chatbotElement.innerHTML = '';
|
|
60
|
-
cancelCurrentApplication();
|
|
61
|
-
},
|
|
62
|
-
/** Resets the user saved data for every application. */
|
|
63
|
-
resetLocalState: async () => {
|
|
64
|
-
resetLocalState();
|
|
65
|
-
cancelCurrentApplication();
|
|
66
|
-
},
|
|
67
|
-
};
|
|
68
|
-
};
|
|
47
|
+
const Chatbot = chatbotPromise.value;
|
|
48
|
+
const application = dataPromise.value;
|
|
49
|
+
startApplication(application);
|
|
50
|
+
logger?.info(`Starting application for job "${application.job.id}" using flow "${application.flow.id}"`);
|
|
51
|
+
chatbotElement.innerHTML = '';
|
|
69
52
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
53
|
+
render(h(Chatbot, { apiClient, logger }), chatbotElement);
|
|
54
|
+
} catch (error) {
|
|
55
|
+
console.error(error);
|
|
56
|
+
logger?.error('Error starting application', error);
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
closeApplication: async () => {
|
|
60
|
+
const chatbotElement = dom.getOrCreateChatbotElement();
|
|
61
|
+
chatbotElement.innerHTML = '';
|
|
62
|
+
cancelCurrentApplication();
|
|
63
|
+
},
|
|
64
|
+
/** Resets the user saved data for every application. */
|
|
65
|
+
resetLocalState: async () => {
|
|
66
|
+
resetLocalState();
|
|
67
|
+
cancelCurrentApplication();
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
});
|
package/src/index.dev.ts
CHANGED
|
@@ -7,7 +7,7 @@ export { chatbotPlugin } from './chatbot';
|
|
|
7
7
|
|
|
8
8
|
declare global {
|
|
9
9
|
interface Window {
|
|
10
|
-
chatbot: ReturnType<typeof chatbotPlugin
|
|
10
|
+
chatbot: ReturnType<ReturnType<typeof chatbotPlugin>>;
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
13
|
|
|
@@ -21,7 +21,7 @@ async function enableMocking() {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
enableMocking().then(() => {
|
|
24
|
-
window.chatbot = chatbotPlugin
|
|
24
|
+
window.chatbot = chatbotPlugin({})({
|
|
25
25
|
apiClient: createApiClient({
|
|
26
26
|
baseUrl: import.meta.env.VITE_BASE_URL,
|
|
27
27
|
publishableKey: import.meta.env.VITE_PUBLISHABLE_KEY,
|