Plugin Directory

Changeset 3471016


Ignore:
Timestamp:
02/27/2026 11:28:23 AM (4 weeks ago)
Author:
bastho
Message:

Add all states to hooks

Location:
faqtastic/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • faqtastic/trunk/build/chatbot/chatbot.asset.php

    r3471008 r3471016  
    1 <?php return array('dependencies' => array('react-dom', 'react-jsx-runtime', 'wp-components', 'wp-element', 'wp-hooks', 'wp-i18n'), 'version' => '13d9f996d4dd57b82e37');
     1<?php return array('dependencies' => array('react-dom', 'react-jsx-runtime', 'wp-components', 'wp-element', 'wp-hooks', 'wp-i18n'), 'version' => 'b5d64d93354ed3ef1e5c');
  • faqtastic/trunk/build/chatbot/chatbot.js

    r3471008 r3471016  
    1 (()=>{"use strict";var t={n:e=>{var a=e&&e.__esModule?()=>e.default:()=>e;return t.d(a,{a}),a},d:(e,a)=>{for(var s in a)t.o(a,s)&&!t.o(e,s)&&Object.defineProperty(e,s,{enumerable:!0,get:a[s]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)};const e=window.ReactDOM;var a=t.n(e);const s=window.wp.element,o=window.wp.components,c=window.wp.i18n,n=window.wp.hooks,i=window.ReactJSXRuntime,r="faqtastic-chatbot-history",l="faqtastic-chatbot-status",d=({dataset:t})=>{const[e,a]=(0,s.useState)(null),n=t.chatbotHeaderText||"Chatbot",r=t.chatbotHeaderSubtitle||"",d=t.chatbotFirstMsg||"Ask me a question",h=t.chatInputPlaceholder||"Type your question...",f=t.chatbotPosition||"bottom-right",g=t.toggleIconType||"dashicon",p=t.toggleIconDashicon||"format-chat",b=t.toggleIconSvg||"",m=t.toggleIconImageUrl||"";(0,s.useEffect)(()=>{"open"===localStorage.getItem(l)&&a(!0)},[]);const q=t=>{localStorage.setItem(l,t?"open":"closed"),a(t)};return(0,i.jsxs)("div",{className:"faqtastic-chatbot-wrapper","data-position":f,children:[e&&(0,i.jsx)(u,{onClose:()=>q(!1),chatbotHeaderText:n,chatbotHeaderSubtitle:r,chatbotFirstMsg:d,chatInputPlaceholder:h,dataset:t}),(0,i.jsx)("button",{className:"faqtastic-chatbot-toggle",onClick:()=>q(!e),"aria-label":(0,c.__)("Open chatbot","faqtastic"),children:"image"===g&&m?(0,i.jsx)("img",{src:m,alt:"",className:"faqtastic-chatbot-custom-image"}):"svg"===g&&b?(0,i.jsx)("span",{className:"faqtastic-chatbot-custom-icon",dangerouslySetInnerHTML:{__html:b}}):(0,i.jsx)(o.Dashicon,{icon:p||"format-chat"})})]})},h=t=>t&&t.$$typeof&&"Symbol(react.element)"===t.$$typeof.toString()?t:null;function u({onClose:t,chatbotHeaderText:e,chatbotHeaderSubtitle:a,chatbotFirstMsg:o,chatInputPlaceholder:l,dataset:d}){const[u,f]=(0,s.useState)(""),[g,p]=(0,s.useState)([]),[b,m]=(0,s.useState)([]),[q,x]=(0,s.useState)(-1),[y,w]=(0,s.useState)(!1),v={type:"bot",text:o};(0,s.useEffect)(()=>{(()=>{let t=JSON.parse(localStorage.getItem(r))||[v];m(t)})()},[]);const j=t=>{m(e=>{const a=[...e,t];return(t=>{let e=JSON.parse(localStorage.getItem(r))||[v];e.push(t),localStorage.setItem(r,JSON.stringify(e))})(t),a})};(0,s.useEffect)(()=>{if(!y){const t=document.querySelector(".faqtastic-chatbot-input");t&&t.focus(),w(!0)}},[y]),(0,s.useEffect)(()=>{let t;if(!(u.length<2))return t=setTimeout(()=>{fetch(`${faqtastic_ajax.ajax_url}?action=faqtastic_chatbot_suggestions&nonce=${faqtastic_ajax.nonce}&term=${encodeURIComponent(u)}`).then(t=>t.json()).then(t=>p(t)).catch(()=>p([]))},300),()=>clearTimeout(t);p([])},[u]);const S=t=>{j({type:"user",text:t.title}),p([]),f(t.title),fetch(`${faqtastic_ajax.ajax_url}?action=faqtastic_chatbot_get_answer&nonce=${faqtastic_ajax.nonce}&id=${t.id}`).then(t=>t.json()).then(t=>{j({type:"bot",text:t.content||(0,c.__)("No answer found.","faqtastic")}),f(""),x(-1)}).catch(()=>{j({type:"bot",text:(0,c.__)("An error occurred. Please try again.","faqtastic")})})};return(0,s.useEffect)(()=>{const t=document.querySelector(".faqtastic-chatbot-conversation");t&&(t.scrollTop=t.scrollHeight)},[b]),(0,s.useEffect)(()=>{const t=t=>{0!==g.length&&("ArrowDown"===t.key?(t.preventDefault(),x(t=>(t+1)%g.length)):"ArrowUp"===t.key?(t.preventDefault(),x(t=>(t-1+g.length)%g.length)):"Enter"===t.key&&(t.preventDefault(),q>=0&&q<g.length&&S(g[q])))};return window.addEventListener("keydown",t),()=>window.removeEventListener("keydown",t)},[g,q]),(0,i.jsxs)("div",{className:"faqtastic-chatbot-window",children:[(0,i.jsxs)("div",{className:"faqtastic-chatbot-header",children:[(0,i.jsxs)("div",{className:"faqtastic-chatbot-header-text",children:[h((0,n.applyFilters)("faqtastic.chatbot.front.header.before",null,{term:u,suggestions:g,addMessage:j,conversation:b,dataset:d})),(0,i.jsx)("span",{className:"faqtastic-chatbot-title",children:e}),a&&(0,i.jsx)("span",{className:"faqtastic-chatbot-subtitle",children:a}),h((0,n.applyFilters)("faqtastic.chatbot.front.header.after",null,{term:u,suggestions:g,addMessage:j,conversation:b,dataset:d}))]}),(0,i.jsx)("button",{onClick:t,className:"faqtastic-close-btn",children:"×"})]}),(0,i.jsxs)("div",{className:"faqtastic-chatbot-conversation",children:[h((0,n.applyFilters)("faqtastic.chatbot.front.conversation.before",null,{term:u,suggestions:g,addMessage:j,conversation:b,dataset:d})),b.map((t,e)=>(0,i.jsx)("div",{className:`faqtastic-chatbot-bubble ${t.type}`,dangerouslySetInnerHTML:{__html:t.text}},e)),h((0,n.applyFilters)("faqtastic.chatbot.front.conversation.after",null,{term:u,suggestions:g,addMessage:j,conversation:b,dataset:d}))]}),(0,i.jsx)("input",{type:"text",className:"faqtastic-chatbot-input",placeholder:l,value:u,onChange:t=>f(t.target.value),autoComplete:"off",onFocus:()=>w(!0)}),h((0,n.applyFilters)("faqtastic.chatbot.front.input.after",null,{term:u,suggestions:g,addMessage:j,conversation:b,dataset:d})),g.length>0&&(0,i.jsx)("ul",{className:"faqtastic-chatbot-suggestions",children:g.map(t=>(0,i.jsx)("li",{onClick:()=>S(t),className:q===g.indexOf(t)?"selected":"",children:t.title},t.id))}),h((0,n.applyFilters)("faqtastic.chatbot.front.end",null,{term:u,suggestions:g,addMessage:j,conversation:b,dataset:d}))]})}document.addEventListener("DOMContentLoaded",()=>{document.body.classList.contains("wp-admin")||document.querySelectorAll(".faqtastic-chatbot").forEach(t=>{a().render((0,i.jsx)(d,{dataset:t.dataset}),t)})})})();
     1(()=>{"use strict";var t={n:e=>{var a=e&&e.__esModule?()=>e.default:()=>e;return t.d(a,{a}),a},d:(e,a)=>{for(var o in a)t.o(a,o)&&!t.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:a[o]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)};const e=window.ReactDOM;var a=t.n(e);const o=window.wp.element,s=window.wp.components,c=window.wp.i18n,n=window.wp.hooks,l=window.ReactJSXRuntime,i="faqtastic-chatbot-history",r="faqtastic-chatbot-status",h=({dataset:t})=>{const[e,a]=(0,o.useState)(null),n=t.chatbotHeaderText||"Chatbot",i=t.chatbotHeaderSubtitle||"",h=t.chatbotFirstMsg||"Ask me a question",d=t.chatInputPlaceholder||"Type your question...",f=t.chatbotPosition||"bottom-right",p=t.toggleIconType||"dashicon",b=t.toggleIconDashicon||"format-chat",g=t.toggleIconSvg||"",m=t.toggleIconImageUrl||"";(0,o.useEffect)(()=>{"open"===localStorage.getItem(r)&&a(!0)},[]);const q=t=>{localStorage.setItem(r,t?"open":"closed"),a(t)};return(0,l.jsxs)("div",{className:"faqtastic-chatbot-wrapper","data-position":f,children:[e&&(0,l.jsx)(u,{onClose:()=>q(!1),chatbotHeaderText:n,chatbotHeaderSubtitle:i,chatbotFirstMsg:h,chatInputPlaceholder:d,dataset:t}),(0,l.jsx)("button",{className:"faqtastic-chatbot-toggle",onClick:()=>q(!e),"aria-label":(0,c.__)("Open chatbot","faqtastic"),children:"image"===p&&m?(0,l.jsx)("img",{src:m,alt:"",className:"faqtastic-chatbot-custom-image"}):"svg"===p&&g?(0,l.jsx)("span",{className:"faqtastic-chatbot-custom-icon",dangerouslySetInnerHTML:{__html:g}}):(0,l.jsx)(s.Dashicon,{icon:b||"format-chat"})})]})},d=t=>t&&t.$$typeof&&"Symbol(react.element)"===t.$$typeof.toString()?t:null;function u({onClose:t,chatbotHeaderText:e,chatbotHeaderSubtitle:a,chatbotFirstMsg:s,chatInputPlaceholder:r,dataset:h}){const[u,f]=(0,o.useState)(""),[p,b]=(0,o.useState)([]),[g,m]=(0,o.useState)([]),[q,x]=(0,o.useState)(-1),[y,w]=(0,o.useState)(!1),j={type:"bot",text:s};(0,o.useEffect)(()=>{(()=>{let t=JSON.parse(localStorage.getItem(i))||[j];m(t)})()},[]);const v=t=>{m(e=>{const a=[...e,t];return(t=>{let e=JSON.parse(localStorage.getItem(i))||[j];e.push(t),localStorage.setItem(i,JSON.stringify(e))})(t),a})};(0,o.useEffect)(()=>{if(!y){const t=document.querySelector(".faqtastic-chatbot-input");t&&t.focus(),w(!0)}},[y]),(0,o.useEffect)(()=>{let t;if(!(u.length<2))return t=setTimeout(()=>{fetch(`${faqtastic_ajax.ajax_url}?action=faqtastic_chatbot_suggestions&nonce=${faqtastic_ajax.nonce}&term=${encodeURIComponent(u)}`).then(t=>t.json()).then(t=>b(t)).catch(()=>b([]))},300),()=>clearTimeout(t);b([])},[u]);const S=t=>{v({type:"user",text:t.title}),b([]),f(t.title),fetch(`${faqtastic_ajax.ajax_url}?action=faqtastic_chatbot_get_answer&nonce=${faqtastic_ajax.nonce}&id=${t.id}`).then(t=>t.json()).then(t=>{v({type:"bot",text:t.content||(0,c.__)("No answer found.","faqtastic")}),f(""),x(-1)}).catch(()=>{v({type:"bot",text:(0,c.__)("An error occurred. Please try again.","faqtastic")})})};(0,o.useEffect)(()=>{const t=document.querySelector(".faqtastic-chatbot-conversation");t&&(t.scrollTop=t.scrollHeight)},[g]),(0,o.useEffect)(()=>{const t=t=>{0!==p.length&&("ArrowDown"===t.key?(t.preventDefault(),x(t=>(t+1)%p.length)):"ArrowUp"===t.key?(t.preventDefault(),x(t=>(t-1+p.length)%p.length)):"Enter"===t.key&&(t.preventDefault(),q>=0&&q<p.length&&S(p[q])))};return window.addEventListener("keydown",t),()=>window.removeEventListener("keydown",t)},[p,q]);const _={term:u,setTerm:f,suggestions:p,setSuggestions:b,conversation:g,setConversation:m,addMessage:v,dataset:h};return(0,l.jsxs)("div",{className:"faqtastic-chatbot-window",children:[(0,l.jsxs)("div",{className:"faqtastic-chatbot-header",children:[(0,l.jsxs)("div",{className:"faqtastic-chatbot-header-text",children:[d((0,n.applyFilters)("faqtastic.chatbot.front.header.before",null,_)),(0,l.jsx)("span",{className:"faqtastic-chatbot-title",children:e}),a&&(0,l.jsx)("span",{className:"faqtastic-chatbot-subtitle",children:a}),d((0,n.applyFilters)("faqtastic.chatbot.front.header.after",null,_))]}),(0,l.jsx)("button",{onClick:t,className:"faqtastic-close-btn",children:"×"})]}),(0,l.jsxs)("div",{className:"faqtastic-chatbot-conversation",children:[d((0,n.applyFilters)("faqtastic.chatbot.front.conversation.before",null,_)),g.map((t,e)=>(0,l.jsx)("div",{className:`faqtastic-chatbot-bubble ${t.type}`,dangerouslySetInnerHTML:{__html:t.text}},e)),d((0,n.applyFilters)("faqtastic.chatbot.front.conversation.after",null,_))]}),(0,l.jsx)("input",{type:"text",className:"faqtastic-chatbot-input",placeholder:r,value:u,onChange:t=>f(t.target.value),autoComplete:"off",onFocus:()=>w(!0)}),d((0,n.applyFilters)("faqtastic.chatbot.front.input.after",null,_)),p.length>0&&(0,l.jsx)("ul",{className:"faqtastic-chatbot-suggestions",children:p.map(t=>(0,l.jsx)("li",{onClick:()=>S(t),className:q===p.indexOf(t)?"selected":"",children:t.title},t.id))}),d((0,n.applyFilters)("faqtastic.chatbot.front.end",null,_))]})}document.addEventListener("DOMContentLoaded",()=>{document.body.classList.contains("wp-admin")||document.querySelectorAll(".faqtastic-chatbot").forEach(t=>{a().render((0,l.jsx)(h,{dataset:t.dataset}),t)})})})();
  • faqtastic/trunk/src/chatbot/chatbot.jsx

    r3471008 r3471016  
    191191    }, [suggestions, keyboardSelectedIndex]);
    192192
     193    const hookData = {
     194      // Local state
     195      term,
     196      setTerm,
     197      suggestions,
     198      setSuggestions,
     199      conversation,
     200      setConversation,
     201      // Global params
     202      addMessage,
     203      dataset,
     204    };
     205
    193206    return (
    194207        <div className="faqtastic-chatbot-window">
     
    198211                        'faqtastic.chatbot.front.header.before',
    199212                        null,
    200                         {
    201                             term,
    202                             suggestions,
    203                             addMessage,
    204                             conversation,
    205                             dataset,
    206                         }
     213                        hookData
    207214                    ))}
    208215                    <span className="faqtastic-chatbot-title">{chatbotHeaderText}</span>
     
    213220                        'faqtastic.chatbot.front.header.after',
    214221                        null,
    215                         {
    216                             term,
    217                             suggestions,
    218                             addMessage,
    219                             conversation,
    220                             dataset,
    221                         }
     222                        hookData
    222223                    ))}
    223224                </div>
     
    229230                    'faqtastic.chatbot.front.conversation.before',
    230231                    null,
    231                     {
    232                         term,
    233                         suggestions,
    234                         addMessage,
    235                         conversation,
    236                             dataset,
    237                     }
     232                    hookData
    238233                ))}
    239234                {conversation.map((msg, i) => (
     
    247242                    'faqtastic.chatbot.front.conversation.after',
    248243                    null,
    249                     {
    250                         term,
    251                         suggestions,
    252                         addMessage,
    253                         conversation,
    254                         dataset,
    255                     }
     244                    hookData
    256245                ))}
    257246            </div>
     
    269258                'faqtastic.chatbot.front.input.after',
    270259                null,
    271                 {
    272                     term,
    273                     suggestions,
    274                     addMessage,
    275                     conversation,
    276                     dataset,
    277                 }
     260                hookData
    278261            ))}
    279262
     
    294277                'faqtastic.chatbot.front.end',
    295278                null,
    296                 {
    297                     term,
    298                     suggestions,
    299                     addMessage,
    300                     conversation,
    301                     dataset,
    302                 }
     279                hookData
    303280            ))}
    304281        </div>
Note: See TracChangeset for help on using the changeset viewer.