-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
1 lines (1 loc) · 5.65 KB
/
index.js
File metadata and controls
1 lines (1 loc) · 5.65 KB
1
(()=>{"use strict";var e,s={809(){const e=window.wp.element,s=window.wp.i18n,o={nonce:"",restBase:"",settingsUrl:"",userName:"",currency:"£",hasKey:!1,...window.heyWooTodayData??{}},a=window.ReactJSXRuntime;function t({message:e}){const s="user"===e.role;return(0,a.jsxs)("div",{className:`hey-woo-bubble hey-woo-bubble--${e.role}`,role:"article","aria-label":s?"You":"Assistant",children:[(0,a.jsx)("span",{className:"hey-woo-bubble__role",children:s?"You":"Hey Woo"}),(0,a.jsx)("p",{className:"hey-woo-bubble__content",children:e.content})]})}function r({onSend:o,disabled:t}){const[r,n]=(0,e.useState)(""),i=(0,e.useRef)(null),l=()=>{const e=r.trim();e&&!t&&(o(e),n(""),i.current?.focus())};return(0,a.jsxs)("div",{className:"hey-woo-input",children:[(0,a.jsx)("textarea",{ref:i,className:"hey-woo-input__textarea",value:r,onChange:e=>n(e.target.value),onKeyDown:e=>{"Enter"!==e.key||e.shiftKey||(e.preventDefault(),l())},placeholder:(0,s.__)("Ask about your store…","hey-woo"),rows:2,disabled:t,"aria-label":(0,s.__)("Chat message","hey-woo")}),(0,a.jsx)("button",{type:"button",className:"button button-primary hey-woo-input__send",onClick:l,disabled:t||!r.trim(),"aria-label":(0,s.__)("Send message","hey-woo"),children:t?(0,s.__)("…","hey-woo"):(0,s.__)("Send","hey-woo")})]})}function n(){const{settingsUrl:e}=o;return(0,a.jsxs)("div",{className:"hey-woo-state hey-woo-state--no-key",children:[(0,a.jsx)("div",{className:"hey-woo-state__icon","aria-hidden":"true",children:"🔑"}),(0,a.jsx)("h2",{className:"hey-woo-state__heading",children:(0,s.__)("Add your Anthropic API key to get started","hey-woo")}),(0,a.jsx)("p",{className:"hey-woo-state__message",children:(0,s.__)("Paste your Anthropic API key and Hey Woo will answer questions about your store's performance, orders, and customers — no Claude Desktop required.","hey-woo")}),(0,a.jsx)("a",{href:e,className:"button button-primary hey-woo-state__cta",children:(0,s.__)("Add API key in Settings","hey-woo")})]})}function i(){const{state:i,sendMessage:l,clearError:c}=function(){const[s,a]=(0,e.useState)({messages:[],status:o.hasKey?"idle":"no_key",errorMessage:""}),t=(0,e.useCallback)(async e=>{const t={role:"user",content:e};a(e=>({...e,messages:[...e.messages,t],status:"sending",errorMessage:""}));try{const t=s.messages,r=await fetch(o.restBase+"/chat",{method:"POST",headers:{"Content-Type":"application/json","X-WP-Nonce":o.nonce},body:JSON.stringify({message:e,history:t})});if(!r.ok)throw new Error(`HTTP ${r.status}`);const n=await r.json();if("no_key"===n.status)return void a(e=>({...e,status:"no_key"}));if("error"===n.status)return void a(e=>({...e,status:"error",errorMessage:n.message}));const i={role:"assistant",content:n.reply};a(e=>({...e,messages:[...e.messages,i],status:"idle"}))}catch(e){const s=e instanceof Error?e.message:"An unexpected error occurred.";a(e=>({...e,status:"error",errorMessage:s}))}},[s.messages]),r=(0,e.useCallback)(()=>{a(e=>({...e,status:"idle",errorMessage:""}))},[]);return{state:s,sendMessage:t,clearError:r}}(),h=(0,e.useRef)(null);if((0,e.useEffect)(()=>{h.current?.scrollIntoView({behavior:"smooth"})},[i.messages]),"no_key"===i.status)return(0,a.jsx)("div",{className:"hey-woo-page",children:(0,a.jsx)(n,{})});const u="sending"===i.status;return(0,a.jsxs)("div",{className:"hey-woo-page hey-woo-page--chat",children:[(0,a.jsxs)("header",{className:"hey-woo-chat-header",children:[(0,a.jsx)("h1",{className:"hey-woo-chat-header__title",children:(0,s.__)("Hey Woo!","hey-woo")}),(0,a.jsx)("p",{className:"hey-woo-chat-header__subtitle",children:(0,s.__)("Ask anything about your store","hey-woo")})]}),(0,a.jsxs)("div",{className:"hey-woo-messages",role:"log","aria-live":"polite",children:[0===i.messages.length&&(0,a.jsx)("p",{className:"hey-woo-messages__empty",children:(0,s.__)("Ask about revenue, orders, products, customers, or anything else about your store.","hey-woo")}),i.messages.map((e,s)=>(0,a.jsx)(t,{message:e},s)),u&&(0,a.jsxs)("div",{className:"hey-woo-bubble hey-woo-bubble--assistant hey-woo-bubble--typing","aria-label":"Thinking",children:[(0,a.jsx)("span",{className:"hey-woo-bubble__role",children:"Hey Woo"}),(0,a.jsxs)("span",{className:"hey-woo-typing-indicator","aria-hidden":"true",children:[(0,a.jsx)("span",{}),(0,a.jsx)("span",{}),(0,a.jsx)("span",{})]})]}),"error"===i.status&&(0,a.jsxs)("div",{className:"hey-woo-error-bar",role:"alert",children:[(0,a.jsx)("span",{children:i.errorMessage||(0,s.__)("Something went wrong.","hey-woo")}),(0,a.jsx)("button",{type:"button",className:"hey-woo-error-bar__dismiss",onClick:c,children:(0,s.__)("Dismiss","hey-woo")})]}),(0,a.jsx)("div",{ref:h,"aria-hidden":"true"})]}),(0,a.jsx)(r,{onSend:l,disabled:u})]})}const l=document.getElementById("hey-woo-today-app")||document.getElementById("hey-woo-today-root");l&&(0,e.createRoot)(l).render((0,a.jsx)(i,{}))}},o={};function a(e){var t=o[e];if(void 0!==t)return t.exports;var r=o[e]={exports:{}};return s[e](r,r.exports,a),r.exports}a.m=s,e=[],a.O=(s,o,t,r)=>{if(!o){var n=1/0;for(h=0;h<e.length;h++){for(var[o,t,r]=e[h],i=!0,l=0;l<o.length;l++)(!1&r||n>=r)&&Object.keys(a.O).every(e=>a.O[e](o[l]))?o.splice(l--,1):(i=!1,r<n&&(n=r));if(i){e.splice(h--,1);var c=t();void 0!==c&&(s=c)}}return s}r=r||0;for(var h=e.length;h>0&&e[h-1][2]>r;h--)e[h]=e[h-1];e[h]=[o,t,r]},a.o=(e,s)=>Object.prototype.hasOwnProperty.call(e,s),(()=>{var e={57:0,350:0};a.O.j=s=>0===e[s];var s=(s,o)=>{var t,r,[n,i,l]=o,c=0;if(n.some(s=>0!==e[s])){for(t in i)a.o(i,t)&&(a.m[t]=i[t]);if(l)var h=l(a)}for(s&&s(o);c<n.length;c++)r=n[c],a.o(e,r)&&e[r]&&e[r][0](),e[r]=0;return a.O(h)},o=globalThis.webpackChunkhey_woo=globalThis.webpackChunkhey_woo||[];o.forEach(s.bind(null,0)),o.push=s.bind(null,o.push.bind(o))})();var t=a.O(void 0,[350],()=>a(809));t=a.O(t)})();