@telia-ace/widget-conversation 1.2.8 → 1.2.9
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.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("react/jsx-runtime"),l=require("@emotion/styled"),n=require("@telia-ace/widget-ui"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("react/jsx-runtime"),l=require("@emotion/styled"),n=require("@telia-ace/widget-ui"),S=require("@telia-ace/widget-utilities"),v=require("@emotion/react"),g=require("react"),J=require("@telia-ace/widget-forms"),j=require("./index.js");require("@telia-ace/widget-core");require("@telia-ace/widget-services");require("@telia-ace/widget-plugins");const Q=({className:t})=>{const[e,o]=g.useState(""),[a,i]=g.useState(0),[r,u]=g.useState(0),h=n.useIsTabbing(),c=n.useDispatch(),{inputPlaceholder:m="Type your message",inputHidden:p=!1,inputDisabled:d=!1,sendButtonLabel:y="Send message",inputMultiline:b=!0,secondaryAction:x}=n.useProperties(),[k,$]=n.useScroll(!0),T=n.useContainer();g.useEffect(()=>{if($.current){const{lineHeight:f,maxHeight:C}=getComputedStyle($.current);if(f&&f.indexOf("px")>-1){const z=f.split("px"),X=parseInt(z[0],10);i(X);const G=C.split("px");u(parseInt(G[0],10))}}},[$.current]);const I=f=>{!f.shiftKey||!b?(f.preventDefault(),e&&e.length>0&&(c("user-submit",{text:e}),o(""),B(""))):e.length<1&&f.preventDefault()},_=f=>{if(!x)return;f.preventDefault(),c("action",{actionKey:x.action,...x});const{events:C}=T.get("$widget");C.dispatch("tracking:secondary-clicked",{})},Y=f=>{if(b&&$.current){const C=$.current.scrollHeight;if(C>a){$.current.style.height="auto";let z=C;f.length<1&&(z=a),$.current.style.height=`${z}px`,z>=r?$.current.style.overflowY="scroll":$.current.style.overflowY="hidden"}}},B=f=>{Y(f),o(f),f&&f.length>0&&c("user-typing",{textLength:f.length})};return p?null:s.jsxs(se,{grow:b,className:S.appendClassNames("humany-conversation-input",t),scrollableCss:k,isTabbing:h,children:[x&&s.jsx(te,{className:"humany-secondary-button",disabled:d,onClick:_,title:x.label,"aria-label":x.label,children:s.jsx(n.SymbolBadge,{size:24,symbol:{type:"Svg",content:x.icon||"browse"}})}),b?s.jsx(Z,{value:e,onChange:B,onEnter:I,disabled:d,"aria-label":m,placeholder:m,type:"textarea",rows:1,ref:$}):s.jsx(ee,{value:e,onChange:B,onEnter:I,disabled:d,"aria-label":m,placeholder:m}),s.jsx(F,{className:"humany-send-button",disabled:!1,onClick:I,title:y,"aria-label":y,children:s.jsx(ne,{size:22,symbol:{type:"Svg",content:"send"},hasContent:!!e&&!!e.length})})]})},Z=l(n.Input)`
|
|
2
2
|
resize: none;
|
|
3
3
|
flex: 1;
|
|
4
4
|
flex-shrink: 1;
|
|
@@ -46,10 +46,10 @@
|
|
|
46
46
|
border-right: ${t=>t.theme.border};
|
|
47
47
|
margin: ${t=>{var e;return(e=t.theme.sizes)==null?void 0:e.normal}} 0;
|
|
48
48
|
`,se=l.div`
|
|
49
|
-
${t=>t.grow?
|
|
49
|
+
${t=>t.grow?v.css`
|
|
50
50
|
min-height: 50px;
|
|
51
51
|
max-height: 150px;
|
|
52
|
-
`:
|
|
52
|
+
`:v.css`
|
|
53
53
|
height: 50px;
|
|
54
54
|
`}
|
|
55
55
|
flex: 0 0 auto;
|
|
@@ -102,7 +102,7 @@
|
|
|
102
102
|
stroke: ${t=>{var e;return(e=t.theme.colors)==null?void 0:e.text}};
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
|
-
`,oe=
|
|
105
|
+
`,oe=v.keyframes`
|
|
106
106
|
0% {
|
|
107
107
|
opacity: 0;
|
|
108
108
|
}
|
|
@@ -129,7 +129,7 @@
|
|
|
129
129
|
> div:nth-of-type(3) {
|
|
130
130
|
animation-delay: 200ms;
|
|
131
131
|
}
|
|
132
|
-
`,
|
|
132
|
+
`,H=l.div`
|
|
133
133
|
grid-area: content;
|
|
134
134
|
max-width: 100%;
|
|
135
135
|
overflow: hidden;
|
|
@@ -137,12 +137,12 @@
|
|
|
137
137
|
font-size: ${t=>{var e;return(e=t.theme.fonts)==null?void 0:e.normal}};
|
|
138
138
|
font-weight: normal;
|
|
139
139
|
margin: 0;
|
|
140
|
-
`,
|
|
140
|
+
`,E=l.div`
|
|
141
141
|
display: flex;
|
|
142
142
|
${t=>t.left&&"justify-content: start;"};
|
|
143
143
|
${t=>t.center&&"justify-content: center;"};
|
|
144
144
|
${t=>t.right&&"justify-content: end;"};
|
|
145
|
-
`,
|
|
145
|
+
`,R=l.div`
|
|
146
146
|
${t=>{var e;return t.padded&&`padding: ${(e=t.theme.sizes)==null?void 0:e.medium};`}}
|
|
147
147
|
${t=>!t.user&&t.stacked&&le(t.stacked,t.theme)}
|
|
148
148
|
${t=>t.user&&t.stacked&&ce(t.stacked,t.theme)}
|
|
@@ -158,11 +158,11 @@
|
|
|
158
158
|
}
|
|
159
159
|
max-width: 100%;
|
|
160
160
|
${t=>t.fullWidth&&"width: 100%;"}
|
|
161
|
-
`,le=(t,e)=>{var a,i;const o=
|
|
161
|
+
`,le=(t,e)=>{var a,i;const o=v.css`
|
|
162
162
|
background-color: ${(a=e.colors)==null?void 0:a.accent};
|
|
163
163
|
color: ${(i=e.colors)==null?void 0:i.text};
|
|
164
164
|
${e.border&&`border: ${e.border};`};
|
|
165
|
-
`;return e.borderRadius?
|
|
165
|
+
`;return e.borderRadius?v.css`
|
|
166
166
|
${o}
|
|
167
167
|
|
|
168
168
|
border-top-right-radius: ${e.borderRadius};
|
|
@@ -175,10 +175,10 @@
|
|
|
175
175
|
${t==="last"&&`
|
|
176
176
|
border-bottom-left-radius: ${e.borderRadius};
|
|
177
177
|
`}
|
|
178
|
-
`:o},ce=(t,e)=>{var a,i;const o=
|
|
178
|
+
`:o},ce=(t,e)=>{var a,i;const o=v.css`
|
|
179
179
|
background-color: ${(a=e.colors)==null?void 0:a.primary};
|
|
180
180
|
color: ${(i=e.colors)==null?void 0:i.accent};
|
|
181
|
-
`;return e.borderRadius?
|
|
181
|
+
`;return e.borderRadius?v.css`
|
|
182
182
|
${o}
|
|
183
183
|
|
|
184
184
|
border-top-left-radius: ${e.borderRadius};
|
|
@@ -196,7 +196,7 @@
|
|
|
196
196
|
&:last-child {
|
|
197
197
|
margin-bottom: 0;
|
|
198
198
|
}
|
|
199
|
-
`,
|
|
199
|
+
`,w=t=>{var e,o;return v.css`
|
|
200
200
|
margin: ${(e=t.theme.sizes)==null?void 0:e.medium} 0;
|
|
201
201
|
padding: 0 ${(o=t.theme.sizes)==null?void 0:o.medium};
|
|
202
202
|
`},de=l(J.FormComponent)`
|
|
@@ -238,7 +238,7 @@
|
|
|
238
238
|
${n.borderTabStyle}
|
|
239
239
|
background-color: transparent;
|
|
240
240
|
}
|
|
241
|
-
`,
|
|
241
|
+
`,K=g.forwardRef(({boxed:t,className:e,agentData:o},a)=>s.jsx(H,{ref:a,children:s.jsx(E,{left:t,center:!t,children:s.jsx(R,{padded:!!t,stacked:t?"first":void 0,children:s.jsxs(ae,{children:[s.jsx("div",{}),s.jsx("div",{}),s.jsx("div",{})]})})})})),W=g.createContext(null),ge=l.p`
|
|
242
242
|
&& {
|
|
243
243
|
font-size: ${t=>{var e;return(e=t.theme.fonts)==null?void 0:e.small}};
|
|
244
244
|
font-weight: 300;
|
|
@@ -247,24 +247,24 @@
|
|
|
247
247
|
line-height: 1.5em;
|
|
248
248
|
grid-area: info;
|
|
249
249
|
color: ${t=>{var e;return(e=t.theme.colors)==null?void 0:e.text}};
|
|
250
|
-
`,q=(t,e)=>new Date(t).toLocaleTimeString(e||[],{hour:"numeric",minute:"2-digit"}),L=({className:t,name:e,timestamp:o,ariaHidden:a=!1})=>{const i=
|
|
250
|
+
`,q=(t,e)=>new Date(t).toLocaleTimeString(e||[],{hour:"numeric",minute:"2-digit"}),L=({className:t,name:e,timestamp:o,ariaHidden:a=!1})=>{const i=g.useContext(W);if(!e&&!o)return null;let r="";return e?o?r=`${e}: ${q(o,i)}`:r=e:o&&(r=q(o,i)),s.jsx(ge,{"aria-hidden":a,children:r})},U=({className:t,children:e,sender:o,timestamp:a})=>{const{avatar:i,name:r}=o,{avatarSize:u,avatarLabel:h="Avatar"}=n.useProperties();return s.jsxs(ye,{hasAvatar:!!i,className:S.appendClassNames(t,"humany-conversation-agent-message"),children:[i&&s.jsx(re,{className:"humany-agent-avatar",avatarSize:u,children:typeof i=="string"?s.jsx("img",{src:i,alt:h}):s.jsx(n.SymbolBadge,{symbol:i})}),e,s.jsx(L,{name:r,timestamp:a,ariaHidden:!0})]})},ye=l.div`
|
|
251
251
|
display: grid;
|
|
252
252
|
margin-right: 10%;
|
|
253
253
|
width: 90%;
|
|
254
|
-
${t=>t.hasAvatar?
|
|
254
|
+
${t=>t.hasAvatar?v.css`
|
|
255
255
|
grid-template-columns: auto 1fr;
|
|
256
256
|
grid-template-rows: 1fr auto;
|
|
257
257
|
grid-template-areas:
|
|
258
258
|
'avatar content'
|
|
259
259
|
'. info';
|
|
260
|
-
`:
|
|
260
|
+
`:v.css`
|
|
261
261
|
grid-template-columns: 1fr;
|
|
262
262
|
grid-template-rows: 1fr auto;
|
|
263
263
|
grid-template-areas:
|
|
264
264
|
'content'
|
|
265
265
|
'info';
|
|
266
266
|
`};
|
|
267
|
-
`,
|
|
267
|
+
`,fe=g.forwardRef(({className:t,children:e,timestamp:o,discrete:a,fullWidth:i},r)=>s.jsx(pe,{ref:r,className:S.appendClassNames(t,"humany-conversation-system-message"),fullWidth:i,children:a?s.jsxs(be,{className:"humany-conversation-discrete-message",children:[e,s.jsx(L,{timestamp:o})]}):s.jsxs(s.Fragment,{children:[e,s.jsx(L,{timestamp:o})]})})),pe=l.div`
|
|
268
268
|
${t=>t.fullWidth&&"width: 100%"};
|
|
269
269
|
padding: 0 ${t=>{var e;return(e=t.theme.sizes)==null?void 0:e.large}};
|
|
270
270
|
overflow: auto;
|
|
@@ -276,13 +276,13 @@
|
|
|
276
276
|
> p {
|
|
277
277
|
margin: 0;
|
|
278
278
|
}
|
|
279
|
-
`,xe=({className:t,children:e,timestamp:o})=>{const{userLabel:a=""}=n.useProperties();return s.jsxs(ve,{className:
|
|
279
|
+
`,xe=({className:t,children:e,timestamp:o})=>{const{userLabel:a=""}=n.useProperties();return s.jsxs(ve,{className:S.appendClassNames(t,"humany-conversation-user-message"),children:[e,s.jsx(L,{name:a,timestamp:o,ariaHidden:!0})]})},ve=l.div`
|
|
280
280
|
margin-left: 10%;
|
|
281
281
|
width: 90%;
|
|
282
282
|
display: flex;
|
|
283
283
|
flex-direction: column;
|
|
284
284
|
align-items: flex-end;
|
|
285
|
-
`,
|
|
285
|
+
`,D=t=>Array.isArray(t)?t:Object.keys(t).map(e=>({label:t[e],actionKey:e})),$e=({header:t,actions:e,dispatch:o})=>{const a=n.useContainer(),{events:i}=a.get("$widget");return s.jsx(je,{className:"humany-conversation-button-list",header:t,items:D(e),renderItem:r=>{var h;const u=!!((h=r==null?void 0:r.data)!=null&&h.href);return s.jsx("li",{children:u?s.jsxs(Se,{href:r.data.href,target:r.data.target,"aria-label":r.label,title:r.label,children:[s.jsx(n.SymbolBadge,{symbol:r.symbol}),r.label]}):s.jsxs(we,{onClick:()=>{o("action",r),i.dispatch("tracking:button-list-item-clicked",r)},"aria-label":r.label,title:r.label,children:[s.jsx(n.SymbolBadge,{symbol:r.symbol}),r.label]})},r.actionKey)}})},je=l(n.ItemList)`
|
|
286
286
|
${n.itemList}
|
|
287
287
|
> ul {
|
|
288
288
|
display: flex;
|
|
@@ -292,8 +292,8 @@
|
|
|
292
292
|
margin: 0;
|
|
293
293
|
}
|
|
294
294
|
}
|
|
295
|
-
${
|
|
296
|
-
`,
|
|
295
|
+
${w}
|
|
296
|
+
`,Se=l(n.Link)`
|
|
297
297
|
${n.buttonCss};
|
|
298
298
|
|
|
299
299
|
display: block;
|
|
@@ -312,7 +312,7 @@
|
|
|
312
312
|
svg {
|
|
313
313
|
margin-right: ${t=>{var e;return(e=t.theme.sizes)==null?void 0:e.small}};
|
|
314
314
|
}
|
|
315
|
-
`,
|
|
315
|
+
`,we=l(n.Button)`
|
|
316
316
|
${n.buttonCss};
|
|
317
317
|
|
|
318
318
|
display: block;
|
|
@@ -331,11 +331,11 @@
|
|
|
331
331
|
svg {
|
|
332
332
|
margin-right: ${t=>{var e;return(e=t.theme.sizes)==null?void 0:e.small}};
|
|
333
333
|
}
|
|
334
|
-
`,
|
|
334
|
+
`,ke=({type:t,content:e={}})=>{const o=n.useComponent(t);return!o||!t?null:s.jsx(g.Suspense,{fallback:null,children:s.jsx(o,{...e})})},Ce=({body:t})=>s.jsx(Le,{className:"humany-html",html:t}),Le=l(n.Html)`
|
|
335
335
|
${n.genericHtml}
|
|
336
336
|
color: inherit;
|
|
337
337
|
white-space: normal;
|
|
338
|
-
${
|
|
338
|
+
${w}
|
|
339
339
|
a {
|
|
340
340
|
color: ${t=>t.theme.colors.link};
|
|
341
341
|
&:focus-visible {
|
|
@@ -345,11 +345,11 @@
|
|
|
345
345
|
p {
|
|
346
346
|
word-break: auto-phrase;
|
|
347
347
|
}
|
|
348
|
-
`,ze=({header:t,actions:e,dispatch:o})=>s.jsx(Te,{className:"humany-conversation-item-list",header:t,items:
|
|
348
|
+
`,ze=({header:t,actions:e,dispatch:o})=>s.jsx(Te,{className:"humany-conversation-item-list",header:t,items:D(e),renderItem:a=>{var i;return s.jsx("li",{children:s.jsxs(Ne,{children:[((i=a.symbol)==null?void 0:i.content)&&s.jsx("div",{className:"item-icon",children:s.jsx(n.SymbolBadge,{size:22,symbol:{type:a.symbol.type,content:a.symbol.content}})}),s.jsxs("div",{className:"item-body",children:[s.jsx(n.Button,{onClick:()=>{o("action",a)},"aria-label":a.label,title:a.label,children:a.label}),a.description&&s.jsx("p",{children:a.description})]})]})},a.actionKey)}}),Te=l(n.ItemList)`
|
|
349
349
|
${n.itemList}
|
|
350
350
|
|
|
351
351
|
> .humany-paragraph {
|
|
352
|
-
${
|
|
352
|
+
${w}
|
|
353
353
|
margin-bottom: 0;
|
|
354
354
|
|
|
355
355
|
h1 {
|
|
@@ -357,7 +357,7 @@
|
|
|
357
357
|
}
|
|
358
358
|
}
|
|
359
359
|
|
|
360
|
-
${t=>n.headingElement(t)("h1",
|
|
360
|
+
${t=>n.headingElement(t)("h1",v.css`
|
|
361
361
|
white-space: normal;
|
|
362
362
|
`)}
|
|
363
363
|
|
|
@@ -380,7 +380,7 @@
|
|
|
380
380
|
`,Ne=l.div`
|
|
381
381
|
display: flex;
|
|
382
382
|
gap: ${t=>{var e;return(e=t.theme.sizes)==null?void 0:e.medium}};
|
|
383
|
-
${
|
|
383
|
+
${w}
|
|
384
384
|
|
|
385
385
|
.item-icon {
|
|
386
386
|
}
|
|
@@ -414,11 +414,11 @@
|
|
|
414
414
|
}
|
|
415
415
|
}
|
|
416
416
|
}
|
|
417
|
-
`,Me=({header:t,actions:e,dispatch:o})=>{const a=n.usePrevious(e),i=
|
|
417
|
+
`,Me=({header:t,actions:e,dispatch:o})=>{const a=n.usePrevious(e),i=g.useRef(),r=n.useIsTabbing(),u=n.useContainer(),{events:h}=u.get("$widget");return g.useEffect(()=>{if(a&&Array.isArray(e)&&Array.isArray(a)&&a.length<e.length&&i.current){const c=i.current.querySelectorAll(".humany-conversation-link-list > ul > li"),m=c[a.length];if(c.length===e.length&&a.length){const p=c[a.length-1];if(p){const d=p.querySelector(".humany-button");d&&d.focus()}}else m&&new MutationObserver((d,y)=>{d.forEach(b=>{if(b.addedNodes){const x=Array.from(b.addedNodes).find(k=>k.tagName==="BUTTON");x&&S.htmlParentContains(i.current,document.activeElement)&&(r&&x.focus(),y.disconnect())}})}).observe(m,{attributes:!1,characterData:!1,childList:!0,subtree:!0,attributeOldValue:!1,characterDataOldValue:!1})}}),s.jsx(Ie,{ref:i,className:"humany-conversation-link-list",header:t,items:D(e),renderItem:c=>s.jsx("li",{children:s.jsxs(Be,{help:c.actionKey==="help",onClick:()=>{o("action",c),h.dispatch("tracking:link-list-item-clicked",c)},"aria-label":c.label,title:c.label,children:[c.label,c.actionKey==="help"&&s.jsx(n.SymbolBadge,{size:14,symbol:{type:"Svg",content:"caret-down"}})]})},c.actionKey)})},Ie=l(n.ItemList)`
|
|
418
418
|
${n.itemList}
|
|
419
|
-
${
|
|
419
|
+
${w}
|
|
420
420
|
|
|
421
|
-
${t=>n.headingElement(t)("h1",
|
|
421
|
+
${t=>n.headingElement(t)("h1",v.css`
|
|
422
422
|
white-space: normal;
|
|
423
423
|
`)}
|
|
424
424
|
|
|
@@ -439,7 +439,7 @@
|
|
|
439
439
|
text-overflow: ellipsis;
|
|
440
440
|
overflow: hidden;
|
|
441
441
|
|
|
442
|
-
${t=>{var e,o;return t.help&&
|
|
442
|
+
${t=>{var e,o;return t.help&&v.css`
|
|
443
443
|
color: ${(e=t.theme.colors)==null?void 0:e.text};
|
|
444
444
|
text-decoration: underline;
|
|
445
445
|
|
|
@@ -456,12 +456,12 @@
|
|
|
456
456
|
text-decoration: underline;
|
|
457
457
|
}
|
|
458
458
|
}
|
|
459
|
-
`,Ae=()=>s.jsx(
|
|
459
|
+
`,Ae=()=>s.jsx(Oe,{className:"humany-conversation-separator"}),Oe=l.div`
|
|
460
460
|
${t=>t.theme.border&&`border-bottom: ${t.theme.border};`}
|
|
461
461
|
width: 100%;
|
|
462
|
-
`,
|
|
463
|
-
${
|
|
464
|
-
`,
|
|
462
|
+
`,He=({text:t})=>s.jsx(Ee,{className:"humany-conversation-text",variant:"span",children:t}),Ee=l(n.Typography)`
|
|
463
|
+
${w}
|
|
464
|
+
`,Re=({header:t="Video call",acceptLabel:e="Accept",declineLabel:o="Decline",dispatch:a})=>{const i=g.useMemo(()=>[{label:e,actionKey:"accept-video-call",symbol:{type:"Svg",content:"video"}},{label:o,actionKey:"decline-video-call",symbol:{type:"Svg",content:"close"}}],[e,o]);return s.jsx(De,{className:"humany-conversation-video-request",header:t,items:i,renderItem:r=>{const u=r.actionKey==="accept-video-call"?"#70c48e":"#da5c85";return s.jsx("li",{children:s.jsx(Ke,{onClick:()=>{a("action",r)},"aria-label":r.label,title:r.label,bgColor:u,children:s.jsx(n.SymbolBadge,{symbol:r.symbol,size:25})})},r.actionKey)}})},De=l(n.ItemList)`
|
|
465
465
|
${t=>n.itemList(t)}
|
|
466
466
|
> ul {
|
|
467
467
|
display: flex;
|
|
@@ -471,8 +471,8 @@
|
|
|
471
471
|
margin: 0;
|
|
472
472
|
}
|
|
473
473
|
}
|
|
474
|
-
${t=>
|
|
475
|
-
`,
|
|
474
|
+
${t=>w(t)}
|
|
475
|
+
`,Ke=l(n.Button)`
|
|
476
476
|
${n.buttonCss};
|
|
477
477
|
|
|
478
478
|
background-color: ${t=>t.bgColor};
|
|
@@ -506,13 +506,13 @@
|
|
|
506
506
|
stroke: #fff;
|
|
507
507
|
}
|
|
508
508
|
}
|
|
509
|
-
`,M=([t,e])=>({type:t,content:e}),qe=({items:t,dispatch:e})=>{const o=t.map(M).filter(r=>r.type.includes("ace-knowledge")).map(r=>"humany-"+r.type.toString());return!!t.map(M).filter(r=>{const
|
|
509
|
+
`,M=([t,e])=>({type:t,content:e}),qe=({items:t,dispatch:e})=>{const o=t.map(M).filter(r=>r.type.includes("ace-knowledge")).map(r=>"humany-"+r.type.toString());return!!t.map(M).filter(r=>{const u=r==null?void 0:r.content,h=u==null?void 0:u.actions;return!!(h!=null&&h.filter(c=>c.actionKey==="contact").length)}).length&&o.push("humany-secondary-action-list"),!!t.map(M).filter(r=>{var h;const u=r==null?void 0:r.content;return!!((h=u==null?void 0:u.tags)!=null&&h.includes("category"))}).length&&o.push("humany-category-list"),s.jsx(Pe,{role:"none",className:S.appendClassNames("humany-message-list",o.join(" ")),items:t.map(M),renderLi:!0,renderItem:r=>{switch(r.type){case j.ConversationMessageListItemType.Text:return s.jsx(He,{text:r.content});case j.ConversationMessageListItemType.HTML:return s.jsx(Ce,{...r.content});case j.ConversationMessageListItemType.Separator:return s.jsx(Ae,{...r.content});case j.ConversationMessageListItemType.ItemList:return s.jsx(ze,{...r.content,dispatch:e});case j.ConversationMessageListItemType.LinkList:return s.jsx(Me,{...r.content,dispatch:e});case j.ConversationMessageListItemType.ButtonList:return s.jsx($e,{...r.content,dispatch:e});case j.ConversationMessageListItemType.VideoRequest:return s.jsx(Re,{...r.content,dispatch:e});default:return s.jsx(ke,{type:r.type,content:r.content})}}})},Pe=l(n.List)`
|
|
510
510
|
${n.list}
|
|
511
511
|
|
|
512
512
|
> li {
|
|
513
513
|
margin: 0;
|
|
514
514
|
}
|
|
515
|
-
`,Fe=
|
|
515
|
+
`,Fe=g.forwardRef(({className:t,timestamp:e,sender:o,type:a,content:i},r)=>{const u=g.useCallback(d=>{let y="first";return i.length>1&&(y=d===0?"first":d+1===i.length?"last":"middle"),y},[i]),{userLabel:h=""}=n.useProperties(),c=()=>s.jsx(s.Fragment,{children:i.map(d=>{const{key:y,body:b,title:x,form:k,actions:$}=d,T=!!b&&!x&&!k&&!$;return s.jsx(fe,{className:t,discrete:T,ref:r,timestamp:e,fullWidth:!!k||!!$,children:T&&b?s.jsx(n.Html,{html:b}):s.jsx(A,{isSystem:!0,content:d})},y)})}),m=()=>s.jsxs(s.Fragment,{children:[s.jsx(P,{className:"humany-sr-only",children:s.jsx(L,{name:o==null?void 0:o.name,timestamp:e})}),s.jsx(U,{sender:o,timestamp:e,className:t,children:s.jsx(H,{className:"humany-message-content-bundle",children:i.map((d,y)=>s.jsx(E,{className:"humany-message-container",left:!0,ref:r,children:s.jsx(A,{content:d,stacked:u(y)})},d.key))})})]}),p=()=>s.jsxs(s.Fragment,{children:[s.jsx(P,{className:"humany-sr-only",children:s.jsx(L,{name:h,timestamp:e})}),s.jsx(xe,{timestamp:e,className:t,children:s.jsx(H,{className:"humany-message-content-bundle",children:i.map((d,y)=>s.jsx(E,{className:"humany-message-container",right:!0,ref:r,children:s.jsx(A,{isUser:!0,content:d,stacked:u(y)})},d.key))})})]});switch(a){case j.ConversationMessageType.System:return c();case j.ConversationMessageType.Agent:return m();case j.ConversationMessageType.User:return p();default:return null}}),A=({content:t,isUser:e,isSystem:o,stacked:a})=>{const i=n.useDispatch(),{title:r,body:u,actions:h,form:c,items:m}=t;return m?s.jsx(R,{className:"humany-message-content",user:!!e,stacked:a,children:s.jsx(qe,{items:m,dispatch:i})}):s.jsxs(R,{className:"humany-message-content",padded:!o,user:!!e,stacked:a,fullWidth:!!c||!!h,children:[r&&s.jsx(N,{children:s.jsx(ie,{variant:"h2",children:r})}),u&&s.jsx(N,{children:s.jsx(ue,{html:u})}),c&&s.jsx(N,{children:s.jsx(de,{...c,onChange:(d,y)=>{i("form",{data:y,formKey:c.schema.id,actionKey:d})}})}),h&&s.jsx(N,{children:s.jsx(me,{items:Object.keys(h).map(d=>({label:h[d],actionKey:d})),renderItem:d=>s.jsx("li",{children:s.jsx(he,{onClick:()=>{i("action",d.actionKey)},children:d.label})},d.actionKey)})})]})},P=l.div`
|
|
516
516
|
position: absolute !important;
|
|
517
517
|
height: 1px;
|
|
518
518
|
width: 1px;
|
|
@@ -535,7 +535,7 @@
|
|
|
535
535
|
transform: translateX(-50%);
|
|
536
536
|
${n.borderTabStyle};
|
|
537
537
|
}
|
|
538
|
-
`,V=()=>{const t=
|
|
538
|
+
`,V=()=>{const t=n.useContainer(),e=g.useCallback(m=>{m&&setTimeout(()=>{m.scrollIntoView({block:"start",behavior:"smooth"})},250)},[]),[o,a]=n.useScroll(!0),{messages:i,loading:r,typingActors:u=[]}=n.useProperties(),h=()=>u&&u.length?u.map((m,p)=>s.jsx(O,{id:`typing_${m.name}`,children:s.jsx(U,{timestamp:Date.now(),sender:m,children:s.jsx(K,{boxed:!0,ref:e})})},`typing_${m.name}`)):null,c=g.useRef((i==null?void 0:i.length)||0);return g.useEffect(()=>{const m=t.get("widgetDOMElement");if(m&&m.id==="humany_bot"){const p=m.querySelectorAll(".humany-conversation-entry");p.length&&p.length>c.current&&(c.current=p.length,p[p.length-1].focus())}}),s.jsxs(s.Fragment,{children:[s.jsxs(Ve,{tabIndex:0,scrollableCss:o,ref:a,children:[i&&i.length?i.map((m,p)=>{const{type:d,timestamp:y,sender:b,content:x}=m;return s.jsx(O,{className:"humany-conversation-entry",tabIndex:-1,children:s.jsx(Fe,{...m,ref:e})},`${d}_${b==null?void 0:b.name}_${y}_${x[0].key}`)}):null,h(),r&&s.jsx(O,{children:s.jsx(K,{ref:e})})]}),s.jsx(Q,{}),s.jsx(We,{})]})},Ve=l.div`
|
|
539
539
|
display: flex;
|
|
540
540
|
flex-direction: column;
|
|
541
541
|
align-items: stretch;
|
|
@@ -545,7 +545,7 @@
|
|
|
545
545
|
&:focus-visible {
|
|
546
546
|
${t=>n.borderTabStyle(t)};
|
|
547
547
|
}
|
|
548
|
-
`,
|
|
548
|
+
`,O=l.div`
|
|
549
549
|
margin: 0;
|
|
550
550
|
margin-bottom: ${t=>{var e;return(e=t.theme.sizes)==null?void 0:e.medium}};
|
|
551
551
|
|
|
@@ -573,10 +573,10 @@
|
|
|
573
573
|
${n.borderTabStyle}
|
|
574
574
|
border-radius: ${t=>{var e;return(e=t.theme.sizes)==null?void 0:e.small}};
|
|
575
575
|
}
|
|
576
|
-
${t=>n.headingElement(t)("h1",
|
|
576
|
+
${t=>n.headingElement(t)("h1",v.css`
|
|
577
577
|
font-weight: normal;
|
|
578
578
|
`)}
|
|
579
|
-
`,_e=({dispatch:t})=>{const e=
|
|
579
|
+
`,_e=({dispatch:t})=>{const e=g.useRef(null);return g.useEffect(()=>{e.current&&e.current.parentNode&&t("overlay-mounted",e.current)},[e]),s.jsx(n.WidgetOverlayOutlet,{portalId:"humany-conversation-overlay-portal",className:"humany-conversation-overlay-outlet",ref:e})},Ye=({dispatch:t,header:e,buttonLabel:o})=>s.jsxs(Xe,{portalId:"humany-conversation-portal",className:"humany-conversation-outlet",children:[s.jsxs(Ge,{className:"humany-conversation-outlet-head",children:[s.jsx(Qe,{className:"humany-conversation-outlet-head-button","aria-label":o,title:o,onClick:()=>t("hide-overlay-conversation"),children:s.jsx(n.SymbolBadge,{symbol:{type:"Svg",content:"caret-down"},size:20})}),e&&s.jsx(Je,{className:"humany-conversation-outlet-header",variant:"h1",children:e})]}),s.jsx(V,{})]}),Xe=l(n.WidgetOverlayOutlet)`
|
|
580
580
|
z-index: 11;
|
|
581
581
|
top: auto;
|
|
582
582
|
height: 65%;
|
|
@@ -611,7 +611,7 @@
|
|
|
611
611
|
svg line {
|
|
612
612
|
stroke: ${t=>{var e;return(e=t.theme.colors)==null?void 0:e.text}};
|
|
613
613
|
}
|
|
614
|
-
`,Ze=({className:t,...e})=>{const{overlay:o,overlayConversationHeader:a,overlayConversationCloseLabel:i,conversationControllerId:r}=n.useProperties(),
|
|
614
|
+
`,Ze=({className:t,...e})=>{const{overlay:o,overlayConversationHeader:a,overlayConversationCloseLabel:i,conversationControllerId:r}=n.useProperties(),u=n.useDispatch(),c=n.useContainer().get("$settings");return s.jsx(n.ConversationContext.Provider,{value:r?{id:r}:null,children:s.jsxs(W.Provider,{value:c==null?void 0:c.language,children:[s.jsx(et,{"aria-live":"polite",...e,className:S.appendClassNames(t,"humany-conversation-component"),children:s.jsx(V,{})}),o&&s.jsxs(s.Fragment,{children:[s.jsx(_e,{dispatch:u}),o.conversation&&s.jsx(Ye,{dispatch:u,header:a,buttonLabel:i})]})]})})},et=l.div`
|
|
615
615
|
position: absolute;
|
|
616
616
|
right: 0;
|
|
617
617
|
left: 0;
|
package/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
"use strict";var _=Object.defineProperty;var q=(o,e,t)=>e in o?_(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t;var h=(o,e,t)=>(q(o,typeof e!="symbol"?e+"":e,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const D=require("@telia-ace/widget-core"),p=require("@telia-ace/widget-services"),E=require("@telia-ace/widget-utilities"),L=require("@telia-ace/widget-forms"),M=require("@telia-ace/widget-plugins"),N=require("@telia-ace/widget-ui"),z=require("react");class U{constructor(e){h(this,"cache",new Map);h(this,"subscriptions",[]);this.controller=e;const{events:t}=e.container.get("$widget"),s=t.subscribe("data-client:fetched",(r,n)=>{n.type==="contact-method"&&this.cache.set(n.response.id,n.response)});this.subscriptions.push(s)}getConversationLogs(){return this.controller.messages.reduce((t,s)=>{const r=k(s),{key:n,content:a,type:i,sender:c,timestamp:d}=r.content,u=this.parseEntry(a,i,c,d);return u&&t.push(u),t},[])}parseEntry(e,t,s,r){const n=d=>{let u=[],l=[],m;if(Array.isArray(d)){const[g,f]=d;m=f}else m=d;return this.createMessageContent(m).length&&(l=[...l,this.createMessageContent(m)]),u=[...u,...this.createMessageOptions(m)],{messagesAsList:l,options:u}};let a=[],i=[];if(Array.isArray(e.items)){const{items:d}=e;d.forEach(u=>{const l=n(u);a=[...a,...l.options],i=[...i,...l.messagesAsList]})}else{const d=n(e);a=[...a,...d.options],i=[...i,...d.messagesAsList]}return{options:a,message:i.length?i.join(`
|
|
2
2
|
`):void 0,source:this.createMessageSource(t),alias:this.createAlias(t,s),timestamp:r}}createMessageSource(e){return e}createAlias(e,t){return e===y.System?"system":(t==null?void 0:t.name)||"Me"}handleContactMethod(e){if(!this.cache.has(e))return;const t=this.cache.get(e);let s="";return s=this.appendText(s,`${t.clientName}:`),s=this.appendText(s,t.title),t.description&&(s=this.appendText(s,t.title)),s}getDetails(e,t=!0){if(!e)return"";const{header:s,title:r,label:n,description:a,text:i,html:c,body:d,contactMethodId:u}=e;let l="";if(u){const m=this.handleContactMethod(u.toString());m&&(l=this.appendText(l,m,t))}return s&&(l=this.appendText(l,s,t)),r&&(l=this.appendText(l,r,t)),a&&(l=this.appendText(l,a,t)),i&&(l=this.appendText(l,i,t)),n&&(l=this.appendText(l,n,t)),c&&(l=this.appendText(l,c,t)),d&&typeof d=="string"&&(l=this.appendText(l,d,t)),l}createMessageContent(e){let t="";return t=this.getDetails(e),t}createMessageOptions(e){let t=[],s=[];return Array.isArray(e==null?void 0:e.actions)?s=e.actions:typeof(e==null?void 0:e.actions)=="object"&&(s=Object.entries(e.actions).map(([r,n])=>({actionKey:r,label:n}))),s.length&&(t=s.map(r=>this.getDetails(r,!1))),t}appendText(e,t,s=!0){return s&&e.length?`${e}
|
|
3
|
-
${t}`:e.length?`${e} ${t}`:t}dispose(){this.subscriptions.forEach(e=>{e()})}}class P{constructor(e,t){h(this,"id");h(this,"name");h(this,"avatar");h(this,"print",(e,t,s=null)=>{var i,c,d;const r={name:this.name,avatar:this.avatar,id:this.id},n=k(e,t);s=s??n.timestamp;const a=(c=(i=this.provider)==null?void 0:i.controller)==null?void 0:c.container.get("$widget");return(d=a==null?void 0:a.events)==null||d.dispatch("tracking:conversation-message",{sender:"agent"}),this.provider.createEntry(n.key,n.content,y.Agent,r,s)});h(this,"typing",()=>this.provider.setTypingState({name:this.name,avatar:this.avatar}));this.provider=e,this.id=t.id||E.uuid(),this.name=t.name,this.avatar=t.avatar}update(e){var r;const{name:t,avatar:s}=e;s===null?this.avatar=void 0:s&&(this.avatar=s),t===null?this.name=void 0:t&&(this.name=t),(r=this.provider.controller)==null||r.events.dispatch("conversation:agent-updated",{agent:this})}}class A{constructor(e,t,s){h(this,"subscriptions",[]);h(this,"options");h(this,"user");this.name=e,this.container=t,this.controller=s,this.user=s==null?void 0:s.user,this.options={silent:!1}}static create(e,t,s){return new A(e,t,s)}createAgent(e={}){var t;if(this.controller){const s=this.controller.getAgentById(e.id),r=s?new P(this,{...s,...e}):new P(this,e);return s||(t=this.controller)==null||t.events.dispatch("conversation:agent-created",{agent:r}),r}}print(e){var t;return(t=this.controller)==null?void 0:t.print(e)}setTypingState(e,t){var s;return(s=this.controller)==null?void 0:s.setTypingState(e,t)}loading(){return this.controller?this.controller.loader():()=>()=>{}}createEntry(e,t,s,r,n){var a;return(a=this.controller)==null?void 0:a.createEntry(e,t,s,r,n)}dispose(){var e;process.env.NODE_ENV!=="production"&&console.log(`conversation-provider: dispose (${this.name})`),(e=this.controller)==null||e.disposeProvider(this),this.controller=null,this.subscriptions.forEach(t=>t())}isRehydrated(){var e;return((e=this.controller)==null?void 0:e.isRehydrated())||Promise.resolve(!1)}getHistory(){var e;return((e=this.controller)==null?void 0:e.getHistory())||[]}complete(){var e;(e=this.controller)==null||e.onCompleteProvider()}setOptions(e={silent:!1}){this.options=e}onUnreadMessage(e){var t;if(this.controller){const s=(t=this.controller)==null?void 0:t.onUnreadMessage(e);return this.subscriptions.push(s),()=>{s()}}}onMounted(e){var t;if(this.controller){const s=(t=this.controller)==null?void 0:t.events.subscribe("conversation:component-mount-changed",e);return this.subscriptions.push(s),e({},this.controller.getComponentMountedState()),()=>{s()}}}}const O=o=>`conversation-${o}`;class K{constructor(){h(this,"_locking",Promise.resolve());h(this,"_locks",0)}isLocked(){return this._locks>0}lock(){this._locks+=1;let e;const t=new Promise(r=>e=()=>{this._locks-=1,r()}),s=this._locking.then(()=>e);return this._locking=this._locking.then(()=>t),s}}class Q{constructor(e,t,s){h(this,"sessionStorageKey");h(this,"locker",new K);h(this,"initialStoredAgents",[]);this.container=e,this.conversation=t,this.enabled=s,this.sessionStorageKey=O(t.id),t.onStarted(async()=>{if(t.messages.length>0)return;const{agents:r}=await this.read(),n=[];r&&r.forEach(i=>{const c=t.providerIndex(i.providerName),d=c>-1?t.registeredProviders[c]:t.createProvider(i.providerName),u=new P(d,{name:i.name,avatar:i.avatar,id:i.id});n.push(u)});const{messages:a}=await this.read();a.forEach(i=>{switch(i.type){case"agent":{const c=n.find(d=>{var u;return d.id===((u=i.sender)==null?void 0:u.id)});c&&c.print(i.content.items||i.content,{key:i.key},i.timestamp);return}case"user":{t.user.print(i.content.items||i.content,{key:i.key},i.timestamp);return}case"system":default:t.print(i.content.items||i.content,i.timestamp)}})}),t.onMessageCreated(async(r,n)=>{this.write(n.message,"messages")}),t.events.subscribe("conversation:agent-created",async(r,n)=>{const a={id:n.agent.id,name:n.agent.name,avatar:n.agent.avatar,providerName:n.agent.provider.name};await this.write(a,"agents")}),t.events.subscribe("conversation:agent-updated",async(r,n)=>{const{agents:a}=await this.read();await this.write([...a.map(i=>i.id===n.agent.id?{...i,name:n.agent.name,avatar:n.agent.avatar}:i)],"agents","update")})}setInitialData(e){this.initialStoredAgents=e.agents}async isRehydrated(){const{messages:e}=await this.read();return e.length>0}getStoredAgent(e){if(this.enabled)return this.initialStoredAgents.find(t=>t.id===e)}async write(e,t,s="add"){if(!this.enabled)return;const r=await this.locker.lock();return p.createStorageWriter(this.container,this.sessionStorageKey,p.StorageCategory.Necessary).then(async n=>{if(s==="add"){const a=await this.read();if(t==="messages"&&a.messages.findIndex(c=>c.key===e.key)>-1){r();return}const i={...a};return t==="messages"?i.messages=[...i.messages,e]:t==="agents"&&(i.agents=[...i.agents,e]),n({...a,...i}).then(()=>{r()})}if(s==="update"&&t==="agents"){const a=await this.read();return n({...a,agents:e}).then(()=>{r()})}return n(e).then(()=>{r()})})}async read(){return p.readStorage(this.container,this.sessionStorageKey).then(e=>e||{messages:[],agents:[]})}}class j{constructor(e){h(this,"print",(e,t,s=null)=>{var a,i;const r=k(e,t);return s=s??r.timestamp,(i=((a=this.controller)==null?void 0:a.container.get("$widget")).events)==null||i.dispatch("tracking:conversation-message",{sender:"user"}),this.controller.createEntry(r.key,r.content,y.User,void 0,s)});this.controller=e}}const x=async(o,e,t)=>{const s=await o.getAsync("conversation"),r=await o.getAsync("components"),n=s.controllers.find(a=>a.conversation.id===e);if(n){const a=[...n.conversation.providers];a.forEach(c=>{n.conversation.setProviderOptions(c,{silent:!0})}),n.conversation.queueProviders(a),n.conversation.setProviders([t]);const i=r.components().ofType("conversation");r.write({key:"setConversationProvider",attributes:{properties:{providers:[t]}},target:i,provider:"localStorage"})}};class ${constructor(e){this.writer=e}update(e){const t=this.getNormalizedSettings(e);this.writer(t)}remove(){this.writer(null)}getNormalizedSettings(e){return typeof e=="string"?{body:e}:e.length>-1&&typeof e=="object"?{items:e}:e}}var y=(o=>(o.User="user",o.Agent="agent",o.System="system",o))(y||{});const k=(o,e)=>{let t,s=(e==null?void 0:e.key)||E.uuid(),r=Date.now();if(Array.isArray(o)&&o.length>-1)typeof o[0]=="string"?t={items:[o]}:t={items:o};else if(typeof o=="string")t={body:o};else if(o=o||{},o.type)t={...o};else{if(t={body:o.body,actions:o.actions,title:o.title},o.key&&(s=o.key),o.timestamp&&(r=o.timestamp),typeof o.form=="function"){const n=L.FormBuilder.create(s);o.form(n);const a=n.get();t.form=a}o.timestamp===null&&(r=null)}return{content:t,key:s,timestamp:r}},W=(o,e)=>{if(!e)return;const t=JSON.stringify(e),s=o.findIndex(r=>t===JSON.stringify(r));if(s>-1)return o.splice(s,1)};class H{constructor(e,t,s){h(this,"formHandlers",{});h(this,"user",new j(this));h(this,"events");h(this,"providers",[]);h(this,"active",!1);h(this,"queuedProviders",[]);h(this,"registeredProviders",[]);h(this,"loaders",0);h(this,"componentMounted",!1);h(this,"store");h(this,"messages",[]);h(this,"typingActors",[]);h(this,"conversationHistory");h(this,"print",(e,t=null)=>{const s=k(e);t=t??s.timestamp;const r=this.container.get("$widget");return r==null||r.events.dispatch("tracking:conversation-message",{sender:"system"}),this.createEntry(s.key,s.content,"system",void 0,t)});this.container=e,this.id=t;const r=e.get("$widget");this.events=r.events.createChild(this),this.store=new Q(e,this,s.rehydrate),this.conversationHistory=new U(this)}setInitialStorage(e){this.store.setInitialData(e)}setProviders(e){const t=[],s=[];this.providers.reduce((r,n)=>(e.indexOf(n)===-1&&r.push(n),r),[]).forEach(r=>{const n=this.providers.indexOf(r);n>-1&&(t.push(r),this.providers.splice(n,1))}),e.forEach(r=>{this.providers.indexOf(r)===-1&&(s.push(r),this.providers.push(r))}),(s.length||t.length)&&this.events.dispatch("conversation:providers-changed",{added:s,removed:t})}createProvider(e){const t=this.providerIndex(e);if(t>-1)return this.registeredProviders[t];const s=A.create(e,this.container,this);return this.registeredProviders.push(s),s}providerIndex(e){return this.registeredProviders.findIndex(t=>t.name===e)}getAgentById(e){if(e)return this.store.getStoredAgent(e)}dispose(){this.conversationHistory.dispose(),this.registeredProviders.forEach(e=>e.dispose()),this.messages=[],this.events.dispatch("conversation:controller-disposed",this),this.active=!1}onDispose(e){return this.events.subscribe("conversation:controller-disposed",e)}onStarted(e){return this.events.subscribe("conversation:started",e)}onMessageCreated(e){return this.events.subscribe("conversation:message-created",e)}isRehydrated(){return this.store.isRehydrated()}disposeProvider(e){const t=this.providerIndex(e.name);t>-1&&this.registeredProviders.splice(t,1),this.events.dispatch("conversation:providers-changed",{removed:[e.name]}),this.registeredProviders.length||this.dispose()}onProvidersChange(e){return this.events.subscribe("conversation:providers-changed",e)}setComponentMountedState(e){this.componentMounted!==e&&(this.componentMounted=e,e&&(this.events.dispatch("conversation:unread-message",!1),this.events.dispatch("conversation:started",!0),this.active=!0),this.events.dispatch("conversation:component-mount-changed",e))}setProviderOptions(e,t){const s=this.providerIndex(e);s>-1&&this.registeredProviders[s].setOptions(t)}changeProvider(e){this.registeredProviders.find(s=>s.name===e)&&x(this.container,this.id,e)}queueProviders(e){this.queuedProviders=e}onCompleteProvider(){const e=this.queuedProviders.pop();e&&x(this.container,this.id,e)}getComponentMountedState(){return this.componentMounted}onUnreadMessage(e){return this.events.subscribe("conversation:unread-message",e)}createEntry(e,t,s,r,n){const a={key:e,content:t,type:s,sender:r||null,timestamp:n||n===null?n:Date.now()};this.messages.push(a),a.type==="agent"&&W(this.typingActors,r);const i=new $(c=>{if(c!==null){const{timestamp:d=void 0,...u}=c;a.content=u,d&&(a.timestamp=d)}else{const d=this.messages.indexOf(a);a.content=null,d>-1&&this.messages.splice(d,1)}this.events.dispatch("conversation:message-updated",{message:a}),this.update()});return this.events.dispatch("conversation:message-created",{message:a}),this.update(),this.componentMounted||this.events.dispatch("conversation:unread-message",!0),i}update(){this.events.dispatch("conversation:updated",{messages:this.messages,loading:this.loaders>0,typingActors:this.typingActors})}getMessageStream(){return{messages:this.messages,loading:this.loaders>0,typingActors:this.typingActors}}setTypingState(e,t){return this.typingActors.indexOf(e)===-1&&(this.typingActors.push(e),this.update()),()=>{W(this.typingActors,e),this.update()}}loader(){this.loaders=this.loaders+1;let e=!1;return this.loaders===1&&this.update(),()=>{e||(e=!0,this.loaders=this.loaders-1,this.loaders===0&&this.update())}}submitForm(e,t){this.formHandlers[e]={...this.formHandlers[e],submit:t}}validateForm(e,t){this.formHandlers[e]={...this.formHandlers[e],validate:t}}getHistory(){return this.conversationHistory.getConversationLogs()}}const F=(o,e)=>{const t=o.split("_contact");return e.widgets.get(t[0])},I=o=>{const e=o.get("$widget"),{name:t,implementation:s}=o.get("$widget"),r=t.indexOf("_contact")>-1?F(t,s):e,{data:n}=o.get("$settings"),{trigger:a}=r.container.get("$settings"),i=`${t.replace("_contact-method","").replace("_contact","")}_contact-method`,c=s.widgets.get(i);return c&&!a?{chatWidget:c,mainWidget:r,data:n}:{mainWidget:r,data:n}},G=o=>{const{chatWidget:e}=I(o);return!!e},R=(o,e,t)=>{const{mainWidget:s,data:r,chatWidget:n}=I(o);return n?n.state==="activated"?n.invoke("open").then(()=>{n.container.getAsync("conversation").then(a=>{a.navigateToConversationIfActive(e.id).then(()=>M.closeAllModals(o))})}):n.activate().then(()=>{const{trigger:a}=n.container.get("$settings");return a.action="hide",n.invoke("setSettings",()=>({data:r,trigger:a,activate:!0})).then(()=>n.container.getAsync("components").then(i=>{const c=i.components().ofType("contact-method");i.write({key:"setContactMethodId",attributes:{properties:{id:e.id,guideId:e.guideId}},target:c,provider:"localStorage"});const d=i.components().ofType("conversation");i.write({key:"setConversationProvider",attributes:{properties:{providers:[e.clientName]}},target:d,provider:"localStorage"})}))}).then(()=>n.invoke("attach",{key:"contact-method",triggerDOMElement:t,withRenderState:"open"})).then(()=>(p.createStorageWriter(n.container,"activeStickyWidget",p.StorageCategory.Necessary,{medium:p.StorageMedium.Local,scope:p.StorageScope.Implementation}).then(a=>a(n.name)),n.container.getAsync("conversation").then(a=>{a.setConversationEndedBehavior(S.Deactivate)}).then(()=>M.closeAllModals(o)).then(()=>!1))):(s.container.getAsync("conversation").then(a=>a.navigateToConversationIfActive(e.id).then(()=>a.setConversationEndedBehavior(S.Navigate))),!n)},J=(o,e,t="conversation")=>{const{mainWidget:s,chatWidget:r}=I(o);return r?r.container.getAsync("router").then(n=>{n.navigate(t,{id:e.id})}):s.container.getAsync("conversation").then(n=>{var a;(a=n.setConversationEndedBehavior(S.Navigate))==null||a.then(()=>{s.container.getAsync("router").then(i=>{s.container.getAsync("components").then(c=>{const d=c.components().ofType("conversation");c.write({key:"setConversationProvider",attributes:{properties:{providers:[e.clientName]}},target:d,provider:"localStorage"})}).then(()=>{i.navigate(t,{id:e.id})})})})})},b="conversations";var S=(o=>(o.Deactivate="deactivate",o.Navigate="navigate",o.None="none",o))(S||{});class w{constructor(e,t){h(this,"events");h(this,"controllers",[]);h(this,"providers",[]);h(this,"disposeProviders",{});h(this,"subscriptions",[]);h(this,"storageWriter",null);h(this,"router",null);h(this,"buttonQuery",null);h(this,"sessionInfo",null);this.container=e,this.components=t;const s=e.get("$widget");this.events=s.events.createChild(this)}initialize(){p.createStorageWriter(this.container,b,p.StorageCategory.Necessary,{medium:p.StorageMedium.Local}).then(e=>this.storageWriter=e),p.readStorage(this.container,b,p.StorageMedium.Local).then(e=>{this.buttonQuery=this.components.components().ofType("conversation-return-button"),e&&(this.sessionInfo=e,e.id&&e.route&&this.writeToComponents({active:!0,alert:{show:!1}}))}),this.container.getAsync("router").then(e=>this.router=e),this.events.subscribe("conversation:ended",(e,t)=>{var s,r,n;switch((s=this.sessionInfo)==null?void 0:s.conversationEndedBehavior){case"deactivate":this.container.get("$widget").deactivate();break;case"navigate":const i=(r=this.router)==null?void 0:r.getInitialRoute();i&&((n=this.router)==null||n.navigate(i.name));break}this.writeToComponents({active:!1}),this.sessionInfo=null,this.storageWriter&&this.storageWriter()})}addSubscription(e){this.subscriptions.push(e)}static getInstance(e,t="conversation"){return E.lock(e)(()=>e.getAsync(t).then(s=>{let r=s;return r||D.ComponentPlatform.getInstance(e).then(n=>(r=new w(e,n),r.initialize(),e.registerAsync(t,()=>r),r))}))}navigateToConversationIfActive(e){return p.readStorage(this.container,b,p.StorageMedium.Local).then(t=>{var s;if(t&&t.id===e&&t.route){const{name:r,params:n}=t.route;(s=this.router)==null||s.navigate(r,n)}})}setConversationEndedBehavior(e){return p.readStorage(this.container,b,p.StorageMedium.Local).then(t=>{let s=t;return s?s.conversationEndedBehavior=e:s={conversationEndedBehavior:e},this.sessionInfo=s,this.storageWriter&&this.storageWriter(this.sessionInfo)||Promise.resolve()})}async createController(e,t){const s=O(e),{agents:r}=await p.readStorage(this.container,s)||{agents:[]},n=this.controllers.findIndex(i=>i.conversation.id===e);let a;return n>-1?a=this.controllers[n].conversation:a=new H(this.container,e,t),a.setInitialStorage({agents:r||[]}),a}registerProvider(e,t){const s={name:e,handler:t};this.providers.push(s),this.controllers.filter(r=>r.conversation.providers.indexOf(e)>-1).forEach(r=>{this.invokeHandler(s,r)})}registerController(e,t){const s=this.controllers.findIndex(n=>n.conversation.id===e.id);let r;if(s<0){r={conversation:e,component:t},this.controllers.push(r);const n=a=>{a&&this.providers.filter(i=>a.indexOf(i.name)>-1).forEach(i=>{this.invokeHandler(i,r)})};return this.subscriptions.push(e.onDispose((a,i)=>{const c=this.controllers.findIndex(d=>d.conversation===i);c>-1&&this.controllers.splice(c,1),this.components.components().ofType("conversation-return-button").select().writeProperties({active:!1}),this.events.dispatch("conversation:ended",{},{bubbles:!0})})),this.subscriptions.push(e.onProvidersChange((a,i)=>{const{added:c,removed:d=[]}=i;d.forEach(u=>{(this.disposeProviders[u]||[]).forEach(l=>{l()})}),n(c)})),this.subscriptions.push(e.onStarted((a,i)=>{var d,u;const c={id:e.id,route:(d=this.router)==null?void 0:d.getRouteData(),conversationEndedBehavior:(u=this.sessionInfo)==null?void 0:u.conversationEndedBehavior};this.sessionInfo=c,this.storageWriter&&this.storageWriter(c),this.writeToComponents({active:i})})),this.subscriptions.push(e.onUnreadMessage((a,i)=>{this.events.dispatch("conversation:unread-message",i),this.writeToComponents({alert:{show:i}})})),n(e.providers),!0}return r=this.controllers[s],!1}writeToComponents(e,t="conversationReturnButtonState"){this.buttonQuery&&this.components.write({key:"conversationReturnButtonState",attributes:{properties:e},target:this.buttonQuery,provider:"localStorage"})}invokeHandler(e,t){const s=e.handler(t.conversation.createProvider(e.name),t.component);s&&(this.disposeProviders[e.name]||(this.disposeProviders[e.name]=[]),this.disposeProviders[e.name].push(s))}getComponentRoute(){return p.readStorage(this.container,b,p.StorageMedium.Local).then(e=>e==null?void 0:e.route)}dispose(){Object.keys(this.disposeProviders).forEach(e=>{this.disposeProviders[e].forEach(t=>{t()})}),this.subscriptions.forEach(e=>{e()}),this.controllers=[],this.providers=[]}static async handleContactMethodClick(e,t,s,r){if(G(t)){e.preventDefault();let n=document.getElementById("humany-dynamic-anchor");n||(n=document.createElement("a"),n.setAttribute("href","#"),n.id="humany-dynamic-anchor",document.body.appendChild(n)),R(t,s,n)}R(t,s)}static async handleContactMethodSubmit(e,t){var s,r;return(r=(s=t==null?void 0:t.body.form.meta)==null?void 0:s.conversation)!=null&&r.id?x(e,t.body.form.meta.conversation.id,t.clientName):J(e,t)}}const V=(o,e)=>o.reduce((t,s,r)=>{var a,i;const n={...s,content:[{...s.content,key:s.key}]};if(t.length>0&&s.type!==y.System){const c=t[t.length-1];if((!e&&e!==0||Math.abs(c.timestamp-s.timestamp)<e)&&((a=c.sender)==null?void 0:a.name)===((i=s.sender)==null?void 0:i.name)&&c.type===s.type)return c.timestamp=s.timestamp,c.content.push({...s.content,key:s.key}),t}return t.push(n),t},[]),Y=o=>Promise.all([w.getInstance(o),o.getAsync("router")]).then(([e,t])=>N.createReactComponent(o,"conversation",Promise.resolve().then(()=>require("./conversation-eaa1e841.js")),async s=>{const{rehydrate:r=!1}=s.properties(),a=t.getRouteData().params.id||s.node.name||"component",i=await e.createController(a,{rehydrate:r}),c=e.registerController(i,s),d=3*60*1e3,u=v=>{const{providers:g=[]}=v;if(g){const f=Array.isArray(g)?g:[g];return i.setProviders(f),f}};u(s.properties(u)),i.setComponentMountedState(!0);const l=v=>{const{loading:g,typingActors:f,messages:C}=v;s.writeProperties({loading:g,typingActors:f,messages:V(C,d)})};l(i.getMessageStream()),c&&(s.writeProperties({conversationControllerId:i.id}),s.actions.create("user-submit",(v,g)=>{if(g){const{preventDefault:f,validationPromise:C}=g;if(f){C&&C().then(T=>{T&&i.user.print(T)});return}}i.user.print(v.text)}),s.actions.create("user-typing",(v,g)=>{}),s.actions.create("form",(v,g)=>{}),s.actions.create("action",(v,g)=>{}),s.actions.create("overlay-mounted",(v,g)=>{}),s.actions.create("show-overlay",(v,g)=>{s.writeProperties({overlay:{conversation:!1,secondary:!1}})}),s.actions.create("hide-overlay",(v,g)=>{s.writeProperties({overlay:null})}),s.actions.create("show-overlay-conversation",(v,g)=>{s.writeProperties({overlay:{conversation:!0,secondary:!1}})}),s.actions.create("hide-overlay-conversation",(v,g)=>{s.writeProperties({overlay:{conversation:!1,secondary:!1}})}));const m=i.events.subscribe("conversation:updated",(v,g)=>{l(g)});return()=>{i.setComponentMountedState(!1),m(),i.active||l({messages:[],typingActors:[],loading:!1})}},{reuseController:!0})),X=o=>Promise.all([D.ComponentPlatform.getInstance(o),w.getInstance(o),o.getAsync("router")]).then(([e,t,s])=>N.createReactComponent(o,"conversation-return-button",Promise.resolve().then(()=>require("./return-button-f1f43984.js")),r=>{r.actions.create("toggle",(n={})=>{var a;(a=t.getComponentRoute())==null||a.then(i=>{i&&s.navigate(i.name,i.params)})})}));var B=(o=>(o.LinkList="link-list",o.Separator="separator",o.ButtonList="button-list",o.ItemList="item-list",o.Text="text",o.HTML="html",o.VideoRequest="video-request",o))(B||{});const Z=(o,e,t)=>o.getAsync("componentResolver").then(async s=>{(await w.getInstance(o)).addSubscription(s.registerComponent(e,z.lazy(()=>t)))});exports.Agent=P;exports.ConversationComponent=Y;exports.ConversationController=H;exports.ConversationEndedBehavior=S;exports.ConversationEntry=$;exports.ConversationMessageListItemType=B;exports.ConversationMessageType=y;exports.ConversationPlatform=w;exports.ConversationProvider=A;exports.ConversationReturnButtonComponent=X;exports.registerCustomMessageComponent=Z;
|
|
3
|
+
${t}`:e.length?`${e} ${t}`:t}dispose(){this.subscriptions.forEach(e=>{e()})}}class P{constructor(e,t){h(this,"id");h(this,"name");h(this,"avatar");h(this,"print",(e,t,s=null)=>{var i,c,d;const r={name:this.name,avatar:this.avatar,id:this.id},n=k(e,t);s=s??n.timestamp;const a=(c=(i=this.provider)==null?void 0:i.controller)==null?void 0:c.container.get("$widget");return(d=a==null?void 0:a.events)==null||d.dispatch("tracking:conversation-message",{sender:"agent"}),this.provider.createEntry(n.key,n.content,y.Agent,r,s)});h(this,"typing",()=>this.provider.setTypingState({name:this.name,avatar:this.avatar}));this.provider=e,this.id=t.id||E.uuid(),this.name=t.name,this.avatar=t.avatar}update(e){var r;const{name:t,avatar:s}=e;s===null?this.avatar=void 0:s&&(this.avatar=s),t===null?this.name=void 0:t&&(this.name=t),(r=this.provider.controller)==null||r.events.dispatch("conversation:agent-updated",{agent:this})}}class A{constructor(e,t,s){h(this,"subscriptions",[]);h(this,"options");h(this,"user");this.name=e,this.container=t,this.controller=s,this.user=s==null?void 0:s.user,this.options={silent:!1}}static create(e,t,s){return new A(e,t,s)}createAgent(e={}){var t;if(this.controller){const s=this.controller.getAgentById(e.id),r=s?new P(this,{...s,...e}):new P(this,e);return s||(t=this.controller)==null||t.events.dispatch("conversation:agent-created",{agent:r}),r}}print(e){var t;return(t=this.controller)==null?void 0:t.print(e)}setTypingState(e,t){var s;return(s=this.controller)==null?void 0:s.setTypingState(e,t)}loading(){return this.controller?this.controller.loader():()=>()=>{}}createEntry(e,t,s,r,n){var a;return(a=this.controller)==null?void 0:a.createEntry(e,t,s,r,n)}dispose(){var e;process.env.NODE_ENV!=="production"&&console.log(`conversation-provider: dispose (${this.name})`),(e=this.controller)==null||e.disposeProvider(this),this.controller=null,this.subscriptions.forEach(t=>t())}isRehydrated(){var e;return((e=this.controller)==null?void 0:e.isRehydrated())||Promise.resolve(!1)}getHistory(){var e;return((e=this.controller)==null?void 0:e.getHistory())||[]}complete(){var e;(e=this.controller)==null||e.onCompleteProvider()}setOptions(e={silent:!1}){this.options=e}onUnreadMessage(e){var t;if(this.controller){const s=(t=this.controller)==null?void 0:t.onUnreadMessage(e);return this.subscriptions.push(s),()=>{s()}}}onMounted(e){var t;if(this.controller){const s=(t=this.controller)==null?void 0:t.events.subscribe("conversation:component-mount-changed",e);return this.subscriptions.push(s),e({},this.controller.getComponentMountedState()),()=>{s()}}}}const O=o=>`conversation-${o}`;class K{constructor(){h(this,"_locking",Promise.resolve());h(this,"_locks",0)}isLocked(){return this._locks>0}lock(){this._locks+=1;let e;const t=new Promise(r=>e=()=>{this._locks-=1,r()}),s=this._locking.then(()=>e);return this._locking=this._locking.then(()=>t),s}}class Q{constructor(e,t,s){h(this,"sessionStorageKey");h(this,"locker",new K);h(this,"initialStoredAgents",[]);this.container=e,this.conversation=t,this.enabled=s,this.sessionStorageKey=O(t.id),t.onStarted(async()=>{if(t.messages.length>0)return;const{agents:r}=await this.read(),n=[];r&&r.forEach(i=>{const c=t.providerIndex(i.providerName),d=c>-1?t.registeredProviders[c]:t.createProvider(i.providerName),u=new P(d,{name:i.name,avatar:i.avatar,id:i.id});n.push(u)});const{messages:a}=await this.read();a.forEach(i=>{switch(i.type){case"agent":{const c=n.find(d=>{var u;return d.id===((u=i.sender)==null?void 0:u.id)});c&&c.print(i.content.items||i.content,{key:i.key},i.timestamp);return}case"user":{t.user.print(i.content.items||i.content,{key:i.key},i.timestamp);return}case"system":default:t.print(i.content.items||i.content,i.timestamp)}})}),t.onMessageCreated(async(r,n)=>{this.write(n.message,"messages")}),t.events.subscribe("conversation:agent-created",async(r,n)=>{const a={id:n.agent.id,name:n.agent.name,avatar:n.agent.avatar,providerName:n.agent.provider.name};await this.write(a,"agents")}),t.events.subscribe("conversation:agent-updated",async(r,n)=>{const{agents:a}=await this.read();await this.write([...a.map(i=>i.id===n.agent.id?{...i,name:n.agent.name,avatar:n.agent.avatar}:i)],"agents","update")})}setInitialData(e){this.initialStoredAgents=e.agents}async isRehydrated(){const{messages:e}=await this.read();return e.length>0}getStoredAgent(e){if(this.enabled)return this.initialStoredAgents.find(t=>t.id===e)}async write(e,t,s="add"){if(!this.enabled)return;const r=await this.locker.lock();return p.createStorageWriter(this.container,this.sessionStorageKey,p.StorageCategory.Necessary).then(async n=>{if(s==="add"){const a=await this.read();if(t==="messages"&&a.messages.findIndex(c=>c.key===e.key)>-1){r();return}const i={...a};return t==="messages"?i.messages=[...i.messages,e]:t==="agents"&&(i.agents=[...i.agents,e]),n({...a,...i}).then(()=>{r()})}if(s==="update"&&t==="agents"){const a=await this.read();return n({...a,agents:e}).then(()=>{r()})}return n(e).then(()=>{r()})})}async read(){return p.readStorage(this.container,this.sessionStorageKey).then(e=>e||{messages:[],agents:[]})}}class j{constructor(e){h(this,"print",(e,t,s=null)=>{var a,i;const r=k(e,t);return s=s??r.timestamp,(i=((a=this.controller)==null?void 0:a.container.get("$widget")).events)==null||i.dispatch("tracking:conversation-message",{sender:"user"}),this.controller.createEntry(r.key,r.content,y.User,void 0,s)});this.controller=e}}const x=async(o,e,t)=>{const s=await o.getAsync("conversation"),r=await o.getAsync("components"),n=s.controllers.find(a=>a.conversation.id===e);if(n){const a=[...n.conversation.providers];a.forEach(c=>{n.conversation.setProviderOptions(c,{silent:!0})}),n.conversation.queueProviders(a),n.conversation.setProviders([t]);const i=r.components().ofType("conversation");r.write({key:"setConversationProvider",attributes:{properties:{providers:[t]}},target:i,provider:"localStorage"})}};class ${constructor(e){this.writer=e}update(e){const t=this.getNormalizedSettings(e);this.writer(t)}remove(){this.writer(null)}getNormalizedSettings(e){return typeof e=="string"?{body:e}:e.length>-1&&typeof e=="object"?{items:e}:e}}var y=(o=>(o.User="user",o.Agent="agent",o.System="system",o))(y||{});const k=(o,e)=>{let t,s=(e==null?void 0:e.key)||E.uuid(),r=Date.now();if(Array.isArray(o)&&o.length>-1)typeof o[0]=="string"?t={items:[o]}:t={items:o};else if(typeof o=="string")t={body:o};else if(o=o||{},o.type)t={...o};else{if(t={body:o.body,actions:o.actions,title:o.title},o.key&&(s=o.key),o.timestamp&&(r=o.timestamp),typeof o.form=="function"){const n=L.FormBuilder.create(s);o.form(n);const a=n.get();t.form=a}o.timestamp===null&&(r=null)}return{content:t,key:s,timestamp:r}},W=(o,e)=>{if(!e)return;const t=JSON.stringify(e),s=o.findIndex(r=>t===JSON.stringify(r));if(s>-1)return o.splice(s,1)};class H{constructor(e,t,s){h(this,"formHandlers",{});h(this,"user",new j(this));h(this,"events");h(this,"providers",[]);h(this,"active",!1);h(this,"queuedProviders",[]);h(this,"registeredProviders",[]);h(this,"loaders",0);h(this,"componentMounted",!1);h(this,"store");h(this,"messages",[]);h(this,"typingActors",[]);h(this,"conversationHistory");h(this,"print",(e,t=null)=>{const s=k(e);t=t??s.timestamp;const r=this.container.get("$widget");return r==null||r.events.dispatch("tracking:conversation-message",{sender:"system"}),this.createEntry(s.key,s.content,"system",void 0,t)});this.container=e,this.id=t;const r=e.get("$widget");this.events=r.events.createChild(this),this.store=new Q(e,this,s.rehydrate),this.conversationHistory=new U(this)}setInitialStorage(e){this.store.setInitialData(e)}setProviders(e){const t=[],s=[];this.providers.reduce((r,n)=>(e.indexOf(n)===-1&&r.push(n),r),[]).forEach(r=>{const n=this.providers.indexOf(r);n>-1&&(t.push(r),this.providers.splice(n,1))}),e.forEach(r=>{this.providers.indexOf(r)===-1&&(s.push(r),this.providers.push(r))}),(s.length||t.length)&&this.events.dispatch("conversation:providers-changed",{added:s,removed:t})}createProvider(e){const t=this.providerIndex(e);if(t>-1)return this.registeredProviders[t];const s=A.create(e,this.container,this);return this.registeredProviders.push(s),s}providerIndex(e){return this.registeredProviders.findIndex(t=>t.name===e)}getAgentById(e){if(e)return this.store.getStoredAgent(e)}dispose(){this.conversationHistory.dispose(),this.registeredProviders.forEach(e=>e.dispose()),this.messages=[],this.events.dispatch("conversation:controller-disposed",this),this.active=!1}onDispose(e){return this.events.subscribe("conversation:controller-disposed",e)}onStarted(e){return this.events.subscribe("conversation:started",e)}onMessageCreated(e){return this.events.subscribe("conversation:message-created",e)}isRehydrated(){return this.store.isRehydrated()}disposeProvider(e){const t=this.providerIndex(e.name);t>-1&&this.registeredProviders.splice(t,1),this.events.dispatch("conversation:providers-changed",{removed:[e.name]}),this.registeredProviders.length||this.dispose()}onProvidersChange(e){return this.events.subscribe("conversation:providers-changed",e)}setComponentMountedState(e){this.componentMounted!==e&&(this.componentMounted=e,e&&(this.events.dispatch("conversation:unread-message",!1),this.events.dispatch("conversation:started",!0),this.active=!0),this.events.dispatch("conversation:component-mount-changed",e))}setProviderOptions(e,t){const s=this.providerIndex(e);s>-1&&this.registeredProviders[s].setOptions(t)}changeProvider(e){this.registeredProviders.find(s=>s.name===e)&&x(this.container,this.id,e)}queueProviders(e){this.queuedProviders=e}onCompleteProvider(){const e=this.queuedProviders.pop();e&&x(this.container,this.id,e)}getComponentMountedState(){return this.componentMounted}onUnreadMessage(e){return this.events.subscribe("conversation:unread-message",e)}createEntry(e,t,s,r,n){const a={key:e,content:t,type:s,sender:r||null,timestamp:n||n===null?n:Date.now()};this.messages.push(a),a.type==="agent"&&W(this.typingActors,r);const i=new $(c=>{if(c!==null){const{timestamp:d=void 0,...u}=c;a.content=u,d&&(a.timestamp=d)}else{const d=this.messages.indexOf(a);a.content=null,d>-1&&this.messages.splice(d,1)}this.events.dispatch("conversation:message-updated",{message:a}),this.update()});return this.events.dispatch("conversation:message-created",{message:a}),this.update(),this.componentMounted||this.events.dispatch("conversation:unread-message",!0),i}update(){this.events.dispatch("conversation:updated",{messages:this.messages,loading:this.loaders>0,typingActors:this.typingActors})}getMessageStream(){return{messages:this.messages,loading:this.loaders>0,typingActors:this.typingActors}}setTypingState(e,t){return this.typingActors.indexOf(e)===-1&&(this.typingActors.push(e),this.update()),()=>{W(this.typingActors,e),this.update()}}loader(){this.loaders=this.loaders+1;let e=!1;return this.loaders===1&&this.update(),()=>{e||(e=!0,this.loaders=this.loaders-1,this.loaders===0&&this.update())}}submitForm(e,t){this.formHandlers[e]={...this.formHandlers[e],submit:t}}validateForm(e,t){this.formHandlers[e]={...this.formHandlers[e],validate:t}}getHistory(){return this.conversationHistory.getConversationLogs()}}const F=(o,e)=>{const t=o.split("_contact");return e.widgets.get(t[0])},I=o=>{const e=o.get("$widget"),{name:t,implementation:s}=o.get("$widget"),r=t.indexOf("_contact")>-1?F(t,s):e,{data:n}=o.get("$settings"),{trigger:a}=r.container.get("$settings"),i=`${t.replace("_contact-method","").replace("_contact","")}_contact-method`,c=s.widgets.get(i);return c&&!a?{chatWidget:c,mainWidget:r,data:n}:{mainWidget:r,data:n}},G=o=>{const{chatWidget:e}=I(o);return!!e},R=(o,e,t)=>{const{mainWidget:s,data:r,chatWidget:n}=I(o);return n?n.state==="activated"?n.invoke("open").then(()=>{n.container.getAsync("conversation").then(a=>{a.navigateToConversationIfActive(e.id).then(()=>M.closeAllModals(o))})}):n.activate().then(()=>{const{trigger:a}=n.container.get("$settings");return a.action="hide",n.invoke("setSettings",()=>({data:r,trigger:a,activate:!0})).then(()=>n.container.getAsync("components").then(i=>{const c=i.components().ofType("contact-method");i.write({key:"setContactMethodId",attributes:{properties:{id:e.id,guideId:e.guideId}},target:c,provider:"localStorage"});const d=i.components().ofType("conversation");i.write({key:"setConversationProvider",attributes:{properties:{providers:[e.clientName]}},target:d,provider:"localStorage"})}))}).then(()=>n.invoke("attach",{key:"contact-method",triggerDOMElement:t,withRenderState:"open"})).then(()=>(p.createStorageWriter(n.container,"activeStickyWidget",p.StorageCategory.Necessary,{medium:p.StorageMedium.Local,scope:p.StorageScope.Implementation}).then(a=>a(n.name)),n.container.getAsync("conversation").then(a=>{a.setConversationEndedBehavior(S.Deactivate)}).then(()=>M.closeAllModals(o)).then(()=>!1))):(s.container.getAsync("conversation").then(a=>a.navigateToConversationIfActive(e.id).then(()=>a.setConversationEndedBehavior(S.Navigate))),!n)},J=(o,e,t="conversation")=>{const{mainWidget:s,chatWidget:r}=I(o);return r?r.container.getAsync("router").then(n=>{n.navigate(t,{id:e.id})}):s.container.getAsync("conversation").then(n=>{var a;(a=n.setConversationEndedBehavior(S.Navigate))==null||a.then(()=>{s.container.getAsync("router").then(i=>{s.container.getAsync("components").then(c=>{const d=c.components().ofType("conversation");c.write({key:"setConversationProvider",attributes:{properties:{providers:[e.clientName]}},target:d,provider:"localStorage"})}).then(()=>{i.navigate(t,{id:e.id})})})})})},b="conversations";var S=(o=>(o.Deactivate="deactivate",o.Navigate="navigate",o.None="none",o))(S||{});class w{constructor(e,t){h(this,"events");h(this,"controllers",[]);h(this,"providers",[]);h(this,"disposeProviders",{});h(this,"subscriptions",[]);h(this,"storageWriter",null);h(this,"router",null);h(this,"buttonQuery",null);h(this,"sessionInfo",null);this.container=e,this.components=t;const s=e.get("$widget");this.events=s.events.createChild(this)}initialize(){p.createStorageWriter(this.container,b,p.StorageCategory.Necessary,{medium:p.StorageMedium.Local}).then(e=>this.storageWriter=e),p.readStorage(this.container,b,p.StorageMedium.Local).then(e=>{this.buttonQuery=this.components.components().ofType("conversation-return-button"),e&&(this.sessionInfo=e,e.id&&e.route&&this.writeToComponents({active:!0,alert:{show:!1}}))}),this.container.getAsync("router").then(e=>this.router=e),this.events.subscribe("conversation:ended",(e,t)=>{var s,r,n;switch((s=this.sessionInfo)==null?void 0:s.conversationEndedBehavior){case"deactivate":this.container.get("$widget").deactivate();break;case"navigate":const i=(r=this.router)==null?void 0:r.getInitialRoute();i&&((n=this.router)==null||n.navigate(i.name));break}this.writeToComponents({active:!1}),this.sessionInfo=null,this.storageWriter&&this.storageWriter()})}addSubscription(e){this.subscriptions.push(e)}static getInstance(e,t="conversation"){return E.lock(e)(()=>e.getAsync(t).then(s=>{let r=s;return r||D.ComponentPlatform.getInstance(e).then(n=>(r=new w(e,n),r.initialize(),e.registerAsync(t,()=>r),r))}))}navigateToConversationIfActive(e){return p.readStorage(this.container,b,p.StorageMedium.Local).then(t=>{var s;if(t&&t.id===e&&t.route){const{name:r,params:n}=t.route;(s=this.router)==null||s.navigate(r,n)}})}setConversationEndedBehavior(e){return p.readStorage(this.container,b,p.StorageMedium.Local).then(t=>{let s=t;return s?s.conversationEndedBehavior=e:s={conversationEndedBehavior:e},this.sessionInfo=s,this.storageWriter&&this.storageWriter(this.sessionInfo)||Promise.resolve()})}async createController(e,t){const s=O(e),{agents:r}=await p.readStorage(this.container,s)||{agents:[]},n=this.controllers.findIndex(i=>i.conversation.id===e);let a;return n>-1?a=this.controllers[n].conversation:a=new H(this.container,e,t),a.setInitialStorage({agents:r||[]}),a}registerProvider(e,t){const s={name:e,handler:t};this.providers.push(s),this.controllers.filter(r=>r.conversation.providers.indexOf(e)>-1).forEach(r=>{this.invokeHandler(s,r)})}registerController(e,t){const s=this.controllers.findIndex(n=>n.conversation.id===e.id);let r;if(s<0){r={conversation:e,component:t},this.controllers.push(r);const n=a=>{a&&this.providers.filter(i=>a.indexOf(i.name)>-1).forEach(i=>{this.invokeHandler(i,r)})};return this.subscriptions.push(e.onDispose((a,i)=>{const c=this.controllers.findIndex(d=>d.conversation===i);c>-1&&this.controllers.splice(c,1),this.components.components().ofType("conversation-return-button").select().writeProperties({active:!1}),this.events.dispatch("conversation:ended",{},{bubbles:!0})})),this.subscriptions.push(e.onProvidersChange((a,i)=>{const{added:c,removed:d=[]}=i;d.forEach(u=>{(this.disposeProviders[u]||[]).forEach(l=>{l()})}),n(c)})),this.subscriptions.push(e.onStarted((a,i)=>{var d,u;const c={id:e.id,route:(d=this.router)==null?void 0:d.getRouteData(),conversationEndedBehavior:(u=this.sessionInfo)==null?void 0:u.conversationEndedBehavior};this.sessionInfo=c,this.storageWriter&&this.storageWriter(c),this.writeToComponents({active:i})})),this.subscriptions.push(e.onUnreadMessage((a,i)=>{this.events.dispatch("conversation:unread-message",i),this.writeToComponents({alert:{show:i}})})),n(e.providers),!0}return r=this.controllers[s],!1}writeToComponents(e,t="conversationReturnButtonState"){this.buttonQuery&&this.components.write({key:"conversationReturnButtonState",attributes:{properties:e},target:this.buttonQuery,provider:"localStorage"})}invokeHandler(e,t){const s=e.handler(t.conversation.createProvider(e.name),t.component);s&&(this.disposeProviders[e.name]||(this.disposeProviders[e.name]=[]),this.disposeProviders[e.name].push(s))}getComponentRoute(){return p.readStorage(this.container,b,p.StorageMedium.Local).then(e=>e==null?void 0:e.route)}dispose(){Object.keys(this.disposeProviders).forEach(e=>{this.disposeProviders[e].forEach(t=>{t()})}),this.subscriptions.forEach(e=>{e()}),this.controllers=[],this.providers=[]}static async handleContactMethodClick(e,t,s,r){if(G(t)){e.preventDefault();let n=document.getElementById("humany-dynamic-anchor");n||(n=document.createElement("a"),n.setAttribute("href","#"),n.id="humany-dynamic-anchor",document.body.appendChild(n)),R(t,s,n)}R(t,s)}static async handleContactMethodSubmit(e,t){var s,r;return(r=(s=t==null?void 0:t.body.form.meta)==null?void 0:s.conversation)!=null&&r.id?x(e,t.body.form.meta.conversation.id,t.clientName):J(e,t)}}const V=(o,e)=>o.reduce((t,s,r)=>{var a,i;const n={...s,content:[{...s.content,key:s.key}]};if(t.length>0&&s.type!==y.System){const c=t[t.length-1];if((!e&&e!==0||Math.abs(c.timestamp-s.timestamp)<e)&&((a=c.sender)==null?void 0:a.name)===((i=s.sender)==null?void 0:i.name)&&c.type===s.type)return c.timestamp=s.timestamp,c.content.push({...s.content,key:s.key}),t}return t.push(n),t},[]),Y=o=>Promise.all([w.getInstance(o),o.getAsync("router")]).then(([e,t])=>N.createReactComponent(o,"conversation",Promise.resolve().then(()=>require("./conversation-dae76c70.js")),async s=>{const{rehydrate:r=!1}=s.properties(),a=t.getRouteData().params.id||s.node.name||"component",i=await e.createController(a,{rehydrate:r}),c=e.registerController(i,s),d=3*60*1e3,u=v=>{const{providers:g=[]}=v;if(g){const f=Array.isArray(g)?g:[g];return i.setProviders(f),f}};u(s.properties(u)),i.setComponentMountedState(!0);const l=v=>{const{loading:g,typingActors:f,messages:C}=v;s.writeProperties({loading:g,typingActors:f,messages:V(C,d)})};l(i.getMessageStream()),c&&(s.writeProperties({conversationControllerId:i.id}),s.actions.create("user-submit",(v,g)=>{if(g){const{preventDefault:f,validationPromise:C}=g;if(f){C&&C().then(T=>{T&&i.user.print(T)});return}}i.user.print(v.text)}),s.actions.create("user-typing",(v,g)=>{}),s.actions.create("form",(v,g)=>{}),s.actions.create("action",(v,g)=>{}),s.actions.create("overlay-mounted",(v,g)=>{}),s.actions.create("show-overlay",(v,g)=>{s.writeProperties({overlay:{conversation:!1,secondary:!1}})}),s.actions.create("hide-overlay",(v,g)=>{s.writeProperties({overlay:null})}),s.actions.create("show-overlay-conversation",(v,g)=>{s.writeProperties({overlay:{conversation:!0,secondary:!1}})}),s.actions.create("hide-overlay-conversation",(v,g)=>{s.writeProperties({overlay:{conversation:!1,secondary:!1}})}));const m=i.events.subscribe("conversation:updated",(v,g)=>{l(g)});return()=>{i.setComponentMountedState(!1),m(),i.active||l({messages:[],typingActors:[],loading:!1})}},{reuseController:!0})),X=o=>Promise.all([D.ComponentPlatform.getInstance(o),w.getInstance(o),o.getAsync("router")]).then(([e,t,s])=>N.createReactComponent(o,"conversation-return-button",Promise.resolve().then(()=>require("./return-button-f1f43984.js")),r=>{r.actions.create("toggle",(n={})=>{var a;(a=t.getComponentRoute())==null||a.then(i=>{i&&s.navigate(i.name,i.params)})})}));var B=(o=>(o.LinkList="link-list",o.Separator="separator",o.ButtonList="button-list",o.ItemList="item-list",o.Text="text",o.HTML="html",o.VideoRequest="video-request",o))(B||{});const Z=(o,e,t)=>o.getAsync("componentResolver").then(async s=>{(await w.getInstance(o)).addSubscription(s.registerComponent(e,z.lazy(()=>t)))});exports.Agent=P;exports.ConversationComponent=Y;exports.ConversationController=H;exports.ConversationEndedBehavior=S;exports.ConversationEntry=$;exports.ConversationMessageListItemType=B;exports.ConversationMessageType=y;exports.ConversationPlatform=w;exports.ConversationProvider=A;exports.ConversationReturnButtonComponent=X;exports.registerCustomMessageComponent=Z;
|
package/index.mjs
CHANGED
|
@@ -953,7 +953,7 @@ const ee = (o, e) => o.reduce((t, s, r) => {
|
|
|
953
953
|
]).then(([e, t]) => H(
|
|
954
954
|
o,
|
|
955
955
|
"conversation",
|
|
956
|
-
import("./conversation-
|
|
956
|
+
import("./conversation-43344a01.mjs"),
|
|
957
957
|
async (s) => {
|
|
958
958
|
const { rehydrate: r = !1 } = s.properties(), a = t.getRouteData().params.id || s.node.name || "component", i = await e.createController(a, {
|
|
959
959
|
rehydrate: r
|