(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[931],{599:function(e,t,r){Promise.resolve().then(r.bind(r,17320))},88239:function(e,t,r){"use strict";r.d(t,{P:function(){return l}});var a=r(2265),n=r(6881),s=r(93073);function l(){let[e,t]=(0,a.useState)([]),{user:r,isSignedIn:l}=(0,s.aF)(),o=(0,a.useCallback)(async()=>{if(!l||!r){t([]);return}try{let{data:e,error:a}=await n.O.from("users").select("id").eq("clerkid",r.id).single();if(a)throw a;let{data:s,error:l}=await n.O.from("chats").select("*").eq("user_id",e.id).eq("deleted",!1).order("updated_at",{ascending:!1});if(l)throw l;let o=s.map(e=>({...e,createdAt:e.created_at,updatedAt:e.updated_at}));t(o)}catch(e){console.error("Error fetching chats:",e)}},[l,r]);return(0,a.useEffect)(()=>{o()},[o]),{chats:e,fetchChats:o,setChats:t}}},17320:function(e,t,r){"use strict";r.r(t),r.d(t,{default:function(){return C}});var a=r(57437),n=r(2265),s=r(63541),l=r(52022),o=r(45764),i=r(1255),c=r(94046),d=r(6881),u=r(93073),m=r(6285),h=r(98775),f=r(27776),x=r(34168),g=r(88239),p=r(22445),b=r(90837),w=r(16463);function j(e){let{handleCreateChat:t}=e,[r,l]=n.useState(!1),[o,c]=n.useState(""),[d,u]=n.useState(!1);(0,w.useRouter)(),n.useEffect(()=>{let e=e=>{"k"===e.key&&(e.metaKey||e.ctrlKey)&&(e.preventDefault(),l(e=>!e))};return document.addEventListener("keydown",e),()=>document.removeEventListener("keydown",e)},[]);let m=async e=>{if(e.preventDefault(),console.log("CommandBar handleSubmit called with input:",o),o.trim()&&!d){u(!0);try{await t(o.trim()),console.log("handleCreateChat completed successfully"),c(""),l(!1)}catch(e){console.error("Error in CommandBar handleSubmit:",e)}finally{u(!1)}}};return(0,a.jsx)(b.Vq,{open:r,onOpenChange:l,children:(0,a.jsx)(b.cZ,{className:"p-0 sm:max-w-[800px] w-full border-0 shadow-none bg-transparent",hideCloseButton:!0,children:(0,a.jsxs)("form",{onSubmit:m,className:"flex items-center p-2 bg-neutral-900 rounded-lg",children:[(0,a.jsx)(i.Z,{value:o,onChange:e=>{c(e.target.value)},onKeyDown:e=>{"Enter"!==e.key||e.shiftKey||(e.preventDefault(),m(e))},placeholder:"Ask anything...",className:"w-full bg-neutral-800 text-white rounded-lg py-3 pl-4 pr-12 resize-none focus:outline-none",minRows:1,maxRows:5,disabled:d}),(0,a.jsx)("div",{className:"absolute right-5 flex items-center h-full",children:(0,a.jsx)("button",{type:"submit",className:"h-8 w-8 flex items-center justify-center rounded-full transition-colors duration-200 ".concat(o.trim()&&!d?"bg-white text-neutral-900 hover:bg-neutral-200":"bg-neutral-700 text-neutral-400 cursor-not-allowed"),disabled:!o.trim()||d,children:(0,a.jsx)(s.Z,{size:16})})})]})})})}var v=r(19666),y=r(58631),N=r(37440),k=r(94456);function C(){let{isSidebarOpen:e,toggleSidebar:t}=(0,k.A)(),{user:r,isSignedIn:b}=(0,u.aF)(),{chats:C,fetchChats:S,setChats:_}=(0,g.P)(),[E,I]=(0,n.useState)(null),[O,Z]=(0,n.useState)(!0),z=(0,w.useRouter)(),[F,P]=(0,n.useState)(""),[A,D]=(0,n.useState)(!1),q=(0,n.useRef)(null);(0,n.useEffect)(()=>{(async()=>{if(b&&r){Z(!0);try{let{data:e,error:t}=await d.O.from("users").select("id").eq("clerkid",r.id).single();if(t)throw t;let{data:a,error:n}=await d.O.from("subscriptions").select("plan").eq("user_id",e.id).order("created_at",{ascending:!1}).limit(1).single();if(n)throw n;if(a&&a.plan){let e=y.z.find(e=>e.monthlyPriceId===a.plan||e.annualPriceId===a.plan);I(e?e.title:"Free"),localStorage.setItem("userPlan",e?e.title:"Free")}else I("Free"),localStorage.setItem("userPlan","Free")}catch(e){console.error("Error fetching user plan:",e),I("Free"),localStorage.setItem("userPlan","Free")}finally{Z(!1)}}else I("Free"),Z(!1)})()},[b,r]);let R=async e=>{console.log("handleCreateChat called with message:",e);try{let t=null;if(b&&r){let{data:e,error:a}=await d.O.from("users").select("id").eq("clerkid",r.id).single();if(a)throw a;t=null==e?void 0:e.id,console.log("User ID fetched:",t)}let{data:a,error:n}=await d.O.from("chats").insert({name:e?e.slice(0,30)+(e.length>30?"...":""):"New Chat",user_id:t}).select().single();if(n)throw n;if(console.log("New chat created:",a),_(e=>[a,...e]),z.push("/chat/".concat(a.id)),console.log("Redirected to new chat page"),e){console.log("Inserting initial message into the chat");let{error:t}=await d.O.from("messages").insert({chat_id:a.id,role:"user",content:e});if(t)throw console.error("Error inserting initial message:",t),t;console.log("Initial message inserted successfully"),console.log("Triggering AI response");let{streamId:r}=await (0,c.w)(a.id,e,[]);console.log("Stream initiated with streamId:",r),z.push("/chat/".concat(a.id,"?streamId=").concat(r))}}catch(e){console.error("Error creating chat:",e),f.Am.error("Error",{description:"Failed to create a new chat. Please try again."})}},K=(0,n.useCallback)(e=>{z.push("/chat/".concat(e))},[z]),B=async e=>{e&&e.preventDefault(),F.trim()&&(D(!0),await R(F),P(""),D(!1))},H=async e=>{"Enter"!==e.key||e.shiftKey||(e.preventDefault(),F.trim()?await B(e):f.Am.error("Error",{description:"Please enter a message before sending."}))};return(0,a.jsx)(a.Fragment,{children:(0,a.jsxs)("div",{className:"flex h-screen bg-neutral-900 overflow-hidden",children:[(0,a.jsx)(p.Z,{chats:C,isSidebarOpen:e,setIsSidebarOpen:t,handleCreateChat:R,handleChatSelect:K,setChats:_}),(0,a.jsxs)("div",{className:"flex-1 bg-neutral-900 flex flex-col transition-all duration-300 ease-in-out ".concat(e?"md:ml-[300px]":"ml-0"),children:[(0,a.jsx)("div",{className:"flex-1 flex flex-col overflow-hidden relative",children:(0,a.jsx)("div",{className:"flex-1 overflow-y-auto px-4 md:px-6 pb-32 relative pt-2 custom-scrollbar",children:(0,a.jsx)("div",{className:"w-full max-w-[800px] mx-auto",children:(0,a.jsxs)("div",{className:"flex flex-col items-center justify-center h-full mt-20",children:[!O&&E&&(0,a.jsxs)("div",{className:(0,N.cn)("bg-teal-800 bg-opacity-20 text-teal-400 border border-teal-950 px-4 py-2 rounded-full text-sm font-medium mb-8","animate-shimmer bg-[length:200%_100%] [transition:background-position_2s_cubic-bezier(.6,.6,0,1)_infinite] bg-clip-text text-transparent","bg-gradient-to-r from-teal-400 via-teal-100 to-teal-400"),style:{backgroundSize:"200% 100%"},children:[E," Plan"]}),(0,a.jsxs)("h1",{className:"text-4xl font-medium text-white mb-4 tracking-tight text-center",children:[(()=>{let e=new Date().getHours();return e<12?"Good morning":e<18?"Good afternoon":"Good evening"})(),", ",(null==r?void 0:r.firstName)||"friend"]}),(0,a.jsx)("p",{className:"text-xl text-neutral-400 mb-8 text-center",children:"How can I assist you today?"})]})})})}),(0,a.jsx)("div",{className:"fixed bottom-0 left-0 right-0 bg-gradient-to-t from-neutral-900 to-transparent pt-20 pb-4",children:(0,a.jsx)("div",{className:"mx-auto transition-all duration-300 ease-in-out ".concat(e?"md:ml-[300px] md:mr-0":"ml-0 mr-0"),children:(0,a.jsx)("form",{onSubmit:B,className:"flex justify-center px-4",children:(0,a.jsxs)("div",{className:"relative w-full max-w-[800px] flex items-center",children:[(0,a.jsx)(i.Z,{ref:q,className:"w-full bg-neutral-800 text-white rounded-lg py-3 pl-4 pr-12 resize-none focus:outline-none focus:ring-2 focus:ring-neutral-700",minRows:1,maxRows:5,placeholder:"Ask me anything",value:F,onChange:e=>{P(e.target.value)},onKeyDown:H}),(0,a.jsx)("div",{className:"absolute right-3 flex items-center h-full",children:(0,a.jsx)("button",{type:"submit",className:"h-8 w-8 flex items-center justify-center rounded-full transition-colors duration-200 ".concat(F.trim()&&!A?"bg-white text-neutral-900 hover:bg-neutral-200":"bg-neutral-700 text-neutral-400 cursor-not-allowed"),disabled:!F.trim()||A,children:(0,a.jsx)(s.Z,{size:16})})})]})})})})]}),(0,a.jsxs)("div",{className:"absolute top-4 right-4 z-50",children:[(0,a.jsx)(m.CH,{children:(0,a.jsx)(x.Z,{})}),(0,a.jsx)(m.tj,{children:(0,a.jsx)(u.$d,{mode:"modal",children:(0,a.jsx)("button",{className:"w-10 h-10 rounded-lg bg-neutral-800 flex items-center justify-center hover:bg-neutral-700 transition-colors border border-neutral-700",children:(0,a.jsx)(l.Z,{size:20,className:"text-white"})})})})]}),(0,a.jsx)("div",{className:"fixed bottom-4 right-8 z-50 hidden md:block cursor-default",children:(0,a.jsx)(v.pn,{children:(0,a.jsxs)(v.u,{children:[(0,a.jsx)(v.aJ,{asChild:!0,children:(0,a.jsxs)("div",{className:"bg-neutral-800 text-neutral-400 rounded-full p-2 flex items-center space-x-1 text-sm",children:[(0,a.jsx)(o.Z,{size:14}),(0,a.jsx)("span",{children:"K"})]})}),(0,a.jsx)(v._v,{side:"top",className:"bg-neutral-800 text-white border-neutral-700",children:"New chat"})]})})}),(0,a.jsx)(h.Toaster,{theme:"dark",className:"toaster group",toastOptions:{classNames:{toast:"group toast group-[.toaster]:bg-neutral-800 group-[.toaster]:text-neutral-100 group-[.toaster]:border-neutral-700 group-[.toaster]:shadow-lg",description:"group-[.toast]:text-neutral-400",actionButton:"group-[.toast]:bg-neutral-100 group-[.toast]:text-neutral-900",cancelButton:"group-[.toast]:bg-neutral-700 group-[.toast]:text-neutral-100"}}}),(0,a.jsx)(j,{handleCreateChat:R})]})})}},63541:function(e,t,r){"use strict";r.d(t,{Z:function(){return a}});/** * @license lucide-react v0.439.0 - ISC * * This source code is licensed under the ISC license. * See the LICENSE file in the root directory of this source tree. */let a=(0,r(78030).Z)("ArrowUp",[["path",{d:"m5 12 7-7 7 7",key:"hav0vg"}],["path",{d:"M12 19V5",key:"x0mq9r"}]])},45764:function(e,t,r){"use strict";r.d(t,{Z:function(){return a}});/** * @license lucide-react v0.439.0 - ISC * * This source code is licensed under the ISC license. * See the LICENSE file in the root directory of this source tree. */let a=(0,r(78030).Z)("Command",[["path",{d:"M15 6v12a3 3 0 1 0 3-3H6a3 3 0 1 0 3 3V6a3 3 0 1 0-3 3h12a3 3 0 1 0-3-3",key:"11bfej"}]])}},function(e){e.O(0,[484,776,970,285,438,735,731,631,971,23,744],function(){return e(e.s=599)}),_N_E=e.O()}]);