PageSourceSearch

https://socialbrilliance.ai/assets/Twin-9MA3gWic.js

js socialbrilliance.ai collected 2026-10-05 15:07:44 UTC 38,338 bytes, 12 lines download raw bytes

1import{c as rs,r as l,s as re,j as e,ap as as,X as ns,b as is,e as Ee,as as os,aq as ds,l as Le,p as cs,i as ls,a9 as xs,g as Ie,t as M,B as k,L as Te,S as Me,Z as ms,A as ps,ac as us,E as $}from"./index-BRZCj0Re.js";import{T as $e}from"./textarea-qYbQ7l9N.js";import{u as hs}from"./use-brand-profiles-BhGXxN34.js";import{r as gs}from"./run-meter-ux-BMC9nY3O.js";import{f as fs,d as bs}from"./brand-drift-Cl51nCoS.js";import{A as ze}from"./activity-qWRmnpaI.js";import{T as js}from"./trending-down--hx7wGvs.js";import{M as De}from"./minus-b9Z8p4tW.js";import{A as Ns}from"./arrow-up-right-Kz86cU9H.js";import{A as ys}from"./arrow-down-right-BPBgiwao.js";import{C as vs}from"./calendar-BKNXgi0b.js";import{L as ws}from"./lightbulb-CQfwAI4X.js";import{F as _s}from"./flame-CrDyI-bY.js";import{f as te}from"./format-QYTbuQ-l.js";import{C as me}from"./chevron-down-CxZl0YU8.js";import{B as Cs}from"./BrandProfileEditor-Cp8B2i6T.js";import{g as Ss}from"./audience-presets-BzG7fYsQ.js";import{A as ks}from"./arrow-left-CZ27GFxq.js";import{P as As}from"./pencil-C4uiWvsP.js";import{M as Bs}from"./message-square-C8rg7zWz.js";import{S as Rs}from"./send-BzrHhqdO.js";import"./trash-2-BcpRJ2AL.js";import"./plus-DfQ5ZUn4.js";/**
2 * @license lucide-react v0.462.0 - ISC
3 *
4 * This source code is licensed under the ISC license.
5 * See the LICENSE file in the root directory of this source tree.
6 */const xe=rs("RotateCw",[["path",{d:"M21 12a9 9 0 1 1-9-9c2.52 0 4.93 1 6.74 2.74L21 8",key:"1p45f6"}],["path",{d:"M21 3v5h-5",key:"1q7to0"}]]);function Is({deviceId:s,profileId:t,currentAnalysis:o,currentScore:d}){const[x,p]=l.useState([]),[b,j]=l.useState([]),[y,w]=l.useState(!0);l.useEffect(()=>{t&&(async()=>{const[i,f]=await Promise.all([re.rpc("get_brand_snapshots_by_device",{p_device_id:s,p_profile_id:t,p_limit:10}),fs(s,t)]);p(i.data||[]),j(f),w(!1)})()},[s,t]);const S=async i=>{try{await bs(i)}catch{}j(f=>f.filter(A=>A.id!==i))},u=x.length>0?x[0]:null,c=u?d-u.brand_score:0,m=(u?Pe(u.analysis,o):[]).filter(i=>Math.abs(i.delta)>=5||i.direction!=="stable");return e.jsxs("div",{className:"space-y-4",children:[b.length>0&&e.jsxs("div",{className:"bg-card border border-border rounded-2xl p-5 space-y-3",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("h3",{className:"text-xs font-bold uppercase tracking-wider text-muted-foreground flex items-center gap-2",children:[e.jsx(as,{className:"w-3.5 h-3.5 text-primary"})," Live Brand Alerts"]}),e.jsxs("span",{className:"text-[10px] px-2 py-0.5 bg-primary/10 text-primary font-bold rounded-full",children:[b.length," new"]})]}),e.jsx("div",{className:"space-y-2",children:b.map(i=>{const f=$s(i);return e.jsxs("div",{className:`flex items-start gap-2.5 p-3 rounded-xl border ${f.bg} ${f.border}`,children:[e.jsx("div",{className:`mt-0.5 ${f.iconColor}`,children:f.icon}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center justify-between gap-2",children:[e.jsx("span",{className:"text-xs font-bold text-foreground",children:i.title}),e.jsx("span",{className:`text-[9px] font-bold uppercase px-1.5 py-0.5 rounded ${f.badge}`,children:i.severity})]}),e.jsx("p",{className:"text-[11px] text-muted-foreground mt-0.5",children:i.detail}),i.content_excerpt&&e.jsxs("p",{className:"text-[10px] text-muted-foreground/60 mt-1 italic truncate",children:['Content: "',i.content_excerpt,'"']}),e.jsxs("div",{className:"flex items-center justify-between mt-1.5",children:[e.jsx("span",{className:"text-[9px] text-muted-foreground/50",children:te(new Date(i.created_at),"MMM d, h:mm a")}),e.jsxs("button",{onClick:()=>S(i.id),className:"text-[10px] text-muted-foreground hover:text-foreground flex items-center gap-0.5 transition-colors",children:[e.jsx(ns,{className:"w-3 h-3"})," Dismiss"]})]})]})]},i.id)})})]}),e.jsxs("div",{className:"bg-card border border-border rounded-2xl p-5 space-y-3",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("h3",{className:"text-xs font-bold uppercase tracking-wider text-muted-foreground flex items-center gap-2",children:[e.jsx(ze,{className:"w-3.5 h-3.5"})," Brand Score Trend"]}),e.jsxs("span",{className:"text-[10px] text-muted-foreground",children:[x.length," snapshot",x.length!==1?"s":""," recorded"]})]}),e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsxs("div",{className:"text-center",children:[e.jsx("div",{className:"text-4xl font-black font-mono text-foreground",children:d}),e.jsx("span",{className:"text-[10px] text-muted-foreground",children:"Current"})]}),u&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:`flex items-center gap-1 px-2.5 py-1 rounded-full text-xs font-bold ${c>0?"bg-success/10 text-success":c<0?"bg-destructive/10 text-destructive":"bg-muted text-muted-foreground"}`,children:[c>0?e.jsx(is,{className:"w-3 h-3"}):c<0?e.jsx(js,{className:"w-3 h-3"}):e.jsx(De,{className:"w-3 h-3"}),c>0?"+":"",c]}),e.jsxs("div",{className:"text-center",children:[e.jsx("div",{className:"text-2xl font-bold font-mono text-muted-foreground",children:u.brand_score}),e.jsx("span",{className:"text-[10px] text-muted-foreground",children:te(new Date(u.created_at),"MMM d")})]})]}),!u&&e.jsx("p",{className:"text-xs text-muted-foreground italic",children:"First analysis — run again later to track changes"})]}),x.length>1&&e.jsxs("div",{className:"flex items-end gap-1 h-12 mt-2",children:[[...x].reverse().map((i,f)=>{const A=Math.max(15,i.brand_score/100*48),R=f===x.length-1;return e.jsxs("div",{className:"flex-1 flex flex-col items-center gap-0.5",children:[e.jsx("div",{className:`w-full rounded-sm transition-all ${R?"bg-primary":"bg-primary/30"}`,style:{height:`${A}px`},title:`${i.brand_score} — ${te(new Date(i.created_at),"MMM d, yyyy")}`}),e.jsx("span",{className:"text-[8px] text-muted-foreground",children:i.brand_score})]},i.id)}),e.jsxs("div",{className:"flex-1 flex flex-col items-center gap-0.5",children:[e.jsx("div",{className:"w-full rounded-sm bg-primary",style:{height:`${Math.max(15,d/100*48)}px`}}),e.jsx("span",{className:"text-[8px] font-bold text-primary",children:d})]})]})]}),m.length>0&&e.jsxs("div",{className:"bg-card border border-border rounded-2xl p-5 space-y-3",children:[e.jsxs("h3",{className:"text-xs font-bold uppercase tracking-wider text-muted-foreground flex items-center gap-2",children:[e.jsx(Ee,{className:"w-3.5 h-3.5 text-warning"})," Perception Shifts Detected"]}),e.jsx("div",{className:"space-y-2",children:m.map((i,f)=>e.jsxs("div",{className:`flex items-start gap-2 p-2.5 rounded-lg ${i.direction==="up"?"bg-success/5 border border-success/15":i.direction==="down"?"bg-destructive/5 border border-destructive/15":"bg-muted/50 border border-border"}`,children:[e.jsx("div",{className:`mt-0.5 ${i.direction==="up"?"text-success":i.direction==="down"?"text-destructive":"text-muted-foreground"}`,children:i.direction==="up"?e.jsx(Ns,{className:"w-4 h-4"}):i.direction==="down"?e.jsx(ys,{className:"w-4 h-4"}):e.jsx(De,{className:"w-4 h-4"})}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("span",{className:"text-xs font-bold text-foreground",children:i.dimension}),e.jsxs("span",{className:`text-xs font-mono font-bold ${i.direction==="up"?"text-success":i.direction==="down"?"text-destructive":"text-muted-foreground"}`,children:[i.delta>0?"+":"",i.delta]})]}),e.jsx("p",{className:"text-[11px] text-muted-foreground mt-0.5",children:i.detail})]})]},f))})]}),u&&e.jsx(Ts,{previous:u.analysis,current:o}),x.length>0&&e.jsxs("div",{className:"bg-card border border-border rounded-2xl p-5 space-y-3",children:[e.jsxs("h3",{className:"text-xs font-bold uppercase tracking-wider text-muted-foreground flex items-center gap-2",children:[e.jsx(vs,{className:"w-3.5 h-3.5"})," Analysis History"]}),e.jsx("div",{className:"space-y-1.5",children:x.map(i=>e.jsxs("div",{className:"flex items-center justify-between py-1.5 border-b border-border/50 last:border-0",children:[e.jsx("span",{className:"text-xs text-muted-foreground",children:te(new Date(i.created_at),"MMM d, yyyy · h:mm a")}),e.jsx("span",{className:"text-sm font-bold font-mono text-foreground",children:i.brand_score})]},i.id))})]})]})}function Ts({previous:s,current:t}){var S,u,c,N;const o=new Set(((S=s==null?void 0:s.audience_perception)==null?void 0:S.association_words)||[]),d=new Set(((u=t==null?void 0:t.audience_perception)==null?void 0:u.association_words)||[]),x=new Set(((c=s==null?void 0:s.audience_perception)==null?void 0:c.missing_associations)||[]),p=new Set(((N=t==null?void 0:t.audience_perception)==null?void 0:N.missing_associations)||[]),b=[...d].filter(m=>!o.has(m)),j=[...o].filter(m=>!d.has(m)),y=[...x].filter(m=>!p.has(m)),w=[...p].filter(m=>!x.has(m));return b.length===0&&j.length===0&&y.length===0&&w.length===0?null:e.jsxs("div",{className:"bg-card border border-border rounded-2xl p-5 space-y-3",children:[e.jsx("h3",{className:"text-xs font-bold uppercase tracking-wider text-muted-foreground",children:"Association 
6Evolution"}),b.length>0&&e.jsxs("div",{children:[e.jsx("span",{className:"text-[10px] font-bold uppercase text-success",children:"New Associations"}),e.jsx("div",{className:"flex flex-wrap gap-1.5 mt-1",children:b.map(m=>e.jsxs("span",{className:"px-2 py-0.5 bg-success/10 rounded-full text-xs text-success font-medium",children:["+ ",m]},m))})]}),j.length>0&&e.jsxs("div",{children:[e.jsx("span",{className:"text-[10px] font-bold uppercase text-destructive",children:"Lost Associations"}),e.jsx("div",{className:"flex flex-wrap gap-1.5 mt-1",children:j.map(m=>e.jsxs("span",{className:"px-2 py-0.5 bg-destructive/10 rounded-full text-xs text-destructive font-medium",children:["- ",m]},m))})]}),y.length>0&&e.jsxs("div",{children:[e.jsx("span",{className:"text-[10px] font-bold uppercase text-primary",children:"Gaps Closed"}),e.jsx("div",{className:"flex flex-wrap gap-1.5 mt-1",children:y.map(m=>e.jsxs("span",{className:"px-2 py-0.5 bg-primary/10 rounded-full text-xs text-primary font-medium",children:["✅ ",m]},m))})]}),w.length>0&&e.jsxs("div",{children:[e.jsx("span",{className:"text-[10px] font-bold uppercase text-warning",children:"New Gaps Detected"}),e.jsx("div",{className:"flex flex-wrap gap-1.5 mt-1",children:w.map(m=>e.jsxs("span",{className:"px-2 py-0.5 bg-warning/10 rounded-full text-xs text-warning font-medium",children:["⚠️ ",m]},m))})]})]})}function Pe(s,t){var u,c,N,m,i,f;const o=[],d=(s==null?void 0:s.brand_score)||0,x=(t==null?void 0:t.brand_score)||0;d!==x&&o.push({dimension:"Brand Clarity Score",direction:x>d?"up":"down",delta:x-d,detail:`Overall score moved from ${d} to ${x}`});const p=((u=s==null?void 0:s.expertise_map)==null?void 0:u.core_domains)||[],b=((c=t==null?void 0:t.expertise_map)==null?void 0:c.core_domains)||[];for(const A of b){const R=p.find(ae=>ae.name.toLowerCase()===A.name.toLowerCase());R&&R.depth!==A.depth&&o.push({dimension:`Expertise: ${A.name}`,direction:A.depth>R.depth?"up":"down",delta:A.depth-R.depth,detail:`Depth shifted from ${R.depth}/10 to ${A.depth}/10`})}const j=((N=s==null?void 0:s.positioning)==null?void 0:N.current_position)||"",y=((m=t==null?void 0:t.positioning)==null?void 0:m.current_position)||"";j&&y&&j!==y&&o.push({dimension:"Positioning",direction:"stable",delta:0,detail:`Changed from "${j.slice(0,60)}…" to "${y.slice(0,60)}…"`});const w=((i=s==null?void 0:s.audience_perception)==null?void 0:i.perception_gap)||"",S=((f=t==null?void 0:t.audience_perception)==null?void 0:f.perception_gap)||"";return w&&S&&w!==S&&o.push({dimension:"Perception Gap",direction:"stable",delta:0,detail:S.slice(0,120)}),o}async function Ms(s,t,o,d,x){const p=x?Pe(x,o):null;await re.from("brand_snapshots").insert([{device_id:s,profile_id:t,brand_score:d,analysis:o,shifts:p}])}function $s(s){switch(s.severity){case"critical":return{bg:"bg-destructive/5",border:"border-destructive/20",iconColor:"text-destructive",icon:e.jsx(_s,{className:"w-4 h-4"}),badge:"bg-destructive/10 text-destructive"};case"warning":return{bg:"bg-warning/5",border:"border-warning/20",iconColor:"text-warning",icon:e.jsx(ds,{className:"w-4 h-4"}),badge:"bg-warning/10 text-warning"};case"positive":return{bg:"bg-success/5",border:"border-success/20",iconColor:"text-success",icon:e.jsx(os,{className:"w-4 h-4"}),badge:"bg-success/10 text-success"};default:return{bg:"bg-primary/5",border:"border-primary/20",iconColor:"text-primary",icon:e.jsx(ws,{className:"w-4 h-4"}),badge:"bg-primary/10 text-primary"}}}function J({title:s,summary:t,defaultOpen:o=!1,children:d,onOpen:x}){const[p,b]=l.useState(o),j=()=>{const y=!p;b(y),y&&(x==null||x())};return e.jsxs("section",{className:"rounded-2xl border border-border/60 bg-card overflow-hidden",children:[e.jsxs("button",{type:"button",onClick:j,"aria-expanded":p,className:"w-full flex items-start gap-3 px-5 py-4 text-left hover:bg-muted/30 transition-colors",children:[e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsx("h2",{className:"text-sm font-semibold text-foreground",children:s}),t&&e.jsx("p",{className:"text-xs text-muted-foreground mt-1 line-clamp-2",children:t})]}),e.jsx(me,{className:Le("w-4 h-4 text-muted-foreground shrink-0 mt-0.5 transition-transform",p&&"rotate-180")}
6)]}),p&&e.jsx("div",{className:"px-5 pb-5 pt-1 space-y-4 border-t border-border/40",children:d})]})}function C({label:s,value:t}){return t?e.jsxs("div",{children:[e.jsx("span",{className:"text-[11px] font-medium uppercase tracking-wider text-muted-foreground",children:s}),e.jsx("p",{className:"text-sm text-foreground/90 leading-relaxed mt-1",children:t})]}):null}function z({label:s,items:t,tone:o="muted"}){return!t||t.length===0?null:e.jsxs("div",{children:[e.jsx("span",{className:"text-[11px] font-medium uppercase tracking-wider text-muted-foreground",children:s}),e.jsx("div",{className:"flex flex-wrap gap-1.5 mt-1.5",children:t.map((d,x)=>e.jsx("span",{className:Le("px-2.5 py-1 rounded-full text-xs",o==="primary"?"bg-primary/10 text-primary border border-primary/20":"bg-secondary/60 text-foreground"),children:d},`${d}-${x}`))})]})}function Y({label:s,items:t}){return!t||t.length===0?null:e.jsxs("div",{children:[e.jsx("span",{className:"text-[11px] font-medium uppercase tracking-wider text-muted-foreground",children:s}),e.jsx("ul",{className:"mt-1.5 space-y-1",children:t.map((o,d)=>e.jsxs("li",{className:"text-sm text-foreground/85 leading-relaxed flex gap-2",children:[e.jsx("span",{className:"text-muted-foreground/60 shrink-0",children:"·"}),e.jsx("span",{children:o})]},`${o}-${d}`))})]})}const g=s=>typeof s=="string"?s.trim():"",T=s=>Array.isArray(s)?s.map(t=>g(t)).filter(Boolean):[];function P(s){return s.some(t=>Array.isArray(t)?t.length>0:g(t).length>0)}function Oe(s){if(!s||typeof s!="object")return null;const t=s,o=t.positioning||{},d=t.audience_perception||{},x=t.proof_points||{},p=t.voice_fingerprint||{},b=t.expertise_map||{},j=t.positioning_statements||{},y=Object.entries(j).map(([c,N])=>({audience:g(c),statement:g(N)})).filter(c=>c.audience&&c.statement),w=(Array.isArray(b.core_domains)?b.core_domains:[]).map(c=>({name:g(c==null?void 0:c.name),depth:typeof(c==null?void 0:c.depth)=="number"&&isFinite(c.depth)?c.depth:0,evidence:g(c==null?void 0:c.evidence)})).filter(c=>c.name),S=typeof t.brand_score=="number"&&isFinite(t.brand_score)?t.brand_score:null,u={criticalInsight:g(t.critical_insight),nextMoves:T(t.next_moves),brandScore:S,positioning:{current:g(o.current_position),ideal:g(o.ideal_position),gap:g(o.gap_analysis),mechanism:g(o.unique_mechanism),moat:g(o.competitive_moat)},audience:{statements:y},voice:{primaryTone:g(p.primary_tone),secondaryTones:T(p.secondary_tones),sentencePatterns:T(p.sentence_patterns),signaturePhrases:T(p.signature_phrases),vocabularyLevel:g(p.vocabulary_level),emotionalRegister:g(p.emotional_register),expertise:w,blindSpots:T(b.blind_spots)},proof:{strongest:T(x.strongest),underused:T(x.underused),missing:T(x.missing)},perception:{readsAs:g(d.what_people_think),intended:g(d.what_they_want_known),gap:g(d.perception_gap),associations:T(d.association_words),missingAssociations:T(d.missing_associations)}};return Ds(u)?u:null}function Ds(s){return s?P([s.criticalInsight,s.positioning.current,s.positioning.ideal,s.perception.readsAs,s.voice.primaryTone,s.proof.strongest,s.audience.statements]):!1}function Es(s,t){const o=g(t==null?void 0:t.name)||"this brand",d=(s==null?void 0:s.positioning.current)||(s==null?void 0:s.perception.readsAs);return d?`Your public signal currently reads ${o} as ${Ls(d)}`:null}function Ls(s){return!s||/^[A-Z][A-Z]/.test(s)?s:s[0].toLowerCase()+s.slice(1)}const zs=[/\bwrite\b/i,/\bpost(s|ing)?\b/i,/\bpublish\b/i,/\bcontent\b/i,/\bcaption\b/i,/\bheadline\b/i,/\bnewsletter\b/i];function Ps(s){const t=g(s);return t?zs.some(o=>o.test(t)):!1}function Os(s){const t=(s==null?void 0:s.criticalInsight)||"";return Ps(t)?{label:"Draft this in Composer",path:"/composer"}:{label:"Run deeper diagnosis",path:"/strategy?source=brand"}}
6function Ws(s){if(!s)return null;const t=new Date(s);return isNaN(t.getTime())?null:t.toLocaleString(void 0,{month:"short",day:"numeric",hour:"numeric",minute:"2-digit"})}function Fs(s){const t=Array.isArray(s)?s[0]:void 0,o=t==null?void 0:t.analysis;return o&&Oe(o)?{kind:"loaded",analysis:o,createdAt:g(t==null?void 0:t.created_at)||null}:{kind:"empty"}}function pt(){var ve,we,_e,Ce,Se,ke;const s=cs(),{toast:t}=ls(),{profiles:o,activeProfile:d,loading:x,switchProfile:p,updateProfile:b,saving:j}=hs(),{recordUsage:y}=xs("twin"),[w,S]=l.useState(null),[u,c]=l.useState(""),[N,m]=l.useState(null),[i,f]=l.useState(null),[A,R]=l.useState(!1),[ae,We]=l.useState(0),[Fe,ne]=l.useState(!1),[pe,G]=l.useState(!1),[He,ie]=l.useState(!1),[Je,K]=l.useState(null),[Ye,oe]=l.useState(!1),[ue,U]=l.useState(!1),[he,Ge]=l.useState(!1),[V,Ke]=l.useState(!1),[D,O]=l.useState([]),[W,de]=l.useState(""),[Z,ge]=l.useState(!1),fe=l.useRef(null),Q=Ie(),n=o.find(a=>a.id===w)||d,X=(n==null?void 0:n.id)??null;l.useEffect(()=>{M($.BRAND_OPENED)},[]),l.useEffect(()=>{d&&!w&&S(d.id)},[d,w]),l.useEffect(()=>{var a;V&&((a=fe.current)==null||a.scrollIntoView({behavior:"smooth"}))},[D,V]);const Ue=a=>{S(a),p(a),G(!1)},be=l.useRef(0),q=l.useCallback(async(a,{resetContextChanged:h=!1}={})=>{const _=++be.current;if(h&&G(!1),!a){m(null),K(null),U(!1),ie(!1);return}ie(!0),U(!1);const{data:I,error:v}=await re.rpc("get_brand_snapshots_by_device",{p_device_id:Q,p_profile_id:a,p_limit:1});if(_!==be.current)return;if(ie(!1),v){U(!0);return}const B=Fs(I);B.kind==="loaded"?(m(B.analysis),K(B.createdAt),oe(!1),O([])):(m(null),K(null))},[Q]);l.useEffect(()=>{q(X,{resetContextChanged:!0})},[X,q]);const ee=async()=>{if(!n){t({variant:"destructive",title:"Select a brand first"});return}const a=await y(us.brandDeepAnalysis);if(!a.ok){const _=gs(a.reason,"This analysis");t({variant:"destructive",title:_.title,description:_.description});return}M($.BRAND_ANALYSIS_STARTED,{profile_id:n.id}),R(!0);const h=N;m(null),O([]);try{const{data:_,error:I}=await re.functions.invoke("brand-twin",{body:{action:"deep_analysis",profile:n,content:u,run_token:a.runToken,deviceId:Ie()}});if(I)throw I;let v;try{const B=_.result.replace(/```json\n?/g,"").replace(/```\n?/g,"").trim();v=JSON.parse(B)}catch{throw new Error("Failed to parse analysis results")}f(h),m(v),oe(!0),K(null),U(!1),G(!1),n!=null&&n.id&&(Ms(Q,n.id,v,v.brand_score,h||void 0),We(B=>B+1)),M($.BRAND_ANALYSIS_COMPLETED,{profile_id:n.id}),O([{role:"assistant",content:`I've refreshed the read on ${n.name}.
7
8**Most important thing:** ${v.critical_insight}
9
10Ask me anything — "How should I pitch to investors?", "What content gaps do I have?", "Write my LinkedIn headline".`}])}catch(_){m(h),t({variant:"destructive",title:"Analysis failed",description:_.message})}finally{R(!1)}},Ve=async a=>{if(!n)return!1;const h=await b(n.id,a);return h&&(M($.BRAND_ACTION_CLICKED,{action:"brand_context_saved"}),N&&(G(!0),oe(!1)),ne(!1)),h},je=l.useCallback(async()=>{var I,v,B;if(!W.trim()||Z)return;const a={role:"user",content:W.trim()},h=[...D,a];O(h),de(""),ge(!0);let _="";try{const E=await fetch("https://rifgfsrcpiwsyyisovec.supabase.co/functions/v1/brand-twin",{method:"POST",headers:{"Content-Type":"application/json",Authorization:"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InJpZmdmc3JjcGl3c3l5aXNvdmVjIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NzE4NTc2MzQsImV4cCI6MjA4NzQzMzYzNH0.uiwSQreYfNUdaDWVNYCLAWpxJEOQiSyqAnHDe29DkeQ"},body:JSON.stringify({action:"chat",profile:n,analysis:N,chatHistory:h})});if(!E.ok||!E.body)throw E.status===429?new Error("Rate limit exceeded. Try again shortly."):E.status===402?new Error("AI credits exhausted."):new Error("Chat failed");const Xe=E.body.getReader(),qe=new TextDecoder;let F="";for(;;){const{done:es,value:ss}=await Xe.read();if(es)break;F+=qe.decode(ss,{stream:!0});let ce;for(;(ce=F.indexOf(`
11`))!==-1;){let H=F.slice(0,ce);if(F=F.slice(ce+1),H.endsWith("\r")&&(H=H.slice(0,-1)),!H.startsWith("data: "))continue;const Ae=H.slice(6).trim();if(Ae==="[DONE]")break;try{const Be=(B=(v=(I=JSON.parse(Ae).choices)==null?void 0:I[0])==null?void 0:v.delta)==null?void 0:B.content;Be&&(_+=Be,O(L=>{const le=L[L.length-1];return(le==null?void 0:le.role)==="assistant"&&L.length>h.length?L.map((Re,ts)=>ts===L.length-1?{...Re,content:_}:Re):[...L,{role:"assistant",content:_}]}))}catch{}}}}catch(E){t({variant:"destructive",title:"Chat error",description:E.message})}finally{ge(!1)}},[W,D,Z,n,N,t]),r=l.useMemo(()=>Oe(N),[N]),Ne=l.useMemo(()=>Es(r,n),[r,n]),ye=l.useMemo(()=>Os(r),[r]),Ze=Ws(Je),se=(a,h)=>{M($.BRAND_ACTION_CLICKED,{action:h,path:a}),s(a)},Qe=x||He;return e.jsxs("div",{className:"min-h-screen bg-background flex flex-col",children:[e.jsx("div",{className:"sticky top-0 z-30 bg-background/95 backdrop-blur-sm border-b border-border",children:e.jsxs("div",{className:"max-w-3xl mx-auto px-4 py-3 flex items-center gap-3",children:[e.jsx(k,{variant:"ghost",size:"icon",onClick:()=>
11s("/dashboard"),className:"shrink-0","aria-label":"Back",children:e.jsx(ks,{className:"w-4 h-4"})}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("h1",{className:"text-lg font-semibold text-foreground",children:"Brand"}),e.jsx("p",{className:"text-xs text-muted-foreground truncate",children:n?`${n.emoji} ${n.name}`:"What we understand about your brand"})]})]})}),e.jsxs("div",{className:"flex-1 max-w-3xl mx-auto w-full px-4 py-6 space-y-5",children:[o.length>1&&e.jsx("div",{className:"flex flex-wrap gap-2",children:o.map(a=>e.jsxs("button",{onClick:()=>Ue(a.id),className:`flex items-center gap-1.5 px-3 py-1.5 rounded-full border text-xs transition-colors ${(n==null?void 0:n.id)===a.id?"bg-primary/10 border-primary/30 text-foreground":"bg-card border-border text-muted-foreground hover:text-foreground"}`,children:[e.jsx("span",{children:a.emoji})," ",a.name]},a.id))}),n&&e.jsxs("section",{className:"rounded-2xl border border-border/60 bg-card p-4 sm:p-5 space-y-3",children:[e.jsxs("div",{className:"flex items-start justify-between gap-3",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-sm font-semibold text-foreground",children:"What SocialBrilliance remembers"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Saved brand context you entered. Composer and Brand both use this."})]}),e.jsxs(k,{variant:"outline",size:"sm",className:"gap-1.5 text-xs shrink-0 min-h-[40px]",onClick:()=>{ne(!0),M($.BRAND_ACTION_CLICKED,{action:"edit_brand_context"})},children:[e.jsx(As,{className:"w-3.5 h-3.5"})," Edit brand context"]})]}),e.jsxs("dl",{className:"grid gap-x-6 gap-y-2 sm:grid-cols-2 text-xs",children:[e.jsxs("div",{children:[e.jsx("dt",{className:"text-muted-foreground",children:"Brand"}),e.jsxs("dd",{className:"text-foreground",children:[n.emoji," ",n.name]})]}),e.jsxs("div",{children:[e.jsx("dt",{className:"text-muted-foreground",children:"Audience"}),e.jsx("dd",{className:"text-foreground",children:(()=>{const a=n.audience_preset?Ss(n.audience_preset):null;return a?`${a.emoji} ${a.label}`:n.audience_preset||"Not set"})()})]}),e.jsxs("div",{className:"sm:col-span-2",children:[e.jsx("dt",{className:"text-muted-foreground",children:"Goal"}),e.jsx("dd",{className:"text-foreground",children:n.goal||"Not set"})]}),e.jsxs("div",{className:"sm:col-span-2",children:[e.jsx("dt",{className:"text-muted-foreground",children:"CTA style"}),e.jsx("dd",{className:"text-foreground truncate",children:n.cta_style||"Not set"})]})]}),e.jsx("div",{className:"flex flex-wrap gap-1.5",children:[{label:"voice samples",count:((ve=n.voice_samples)==null?void 0:ve.length)??0},{label:"recurring themes",count:((we=n.recurring_themes)==null?void 0:we.length)??0},{label:"banned phrases",count:((_e=n.banned_phrases)==null?void 0:_e.length)??0}].map(a=>e.jsxs("span",{className:"text-[11px] px-2 py-0.5 rounded-full bg-secondary/60 text-muted-foreground border border-border",children:[a.count," ",a.label]},a.label))})]}),Qe?e.jsxs("div",{className:"space-y-3",children:[e.jsx("div",{className:"h-24 rounded-2xl border border-border/50 bg-card animate-pulse"}),e.jsx("div",{className:"h-16 rounded-2xl border border-border/50 bg-card animate-pulse"})]}):A?e.jsxs("div",{className:"flex flex-col items-center justify-center py-20 gap-3 text-center",children:[e.jsx(Te,{className:"w-6 h-6 animate-spin text-primary"}),e.jsx("p",{className:"text-sm font-medium text-foreground",children:"Reading your brand signal…"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Considering positioning, audience, voice, proof, and perception gaps."})]}):n?ue&&!r?e.jsxs("section",{className:"rounded-2xl border border-border/60 bg-card p-6 sm:p-8 space-y-3",children:[e.jsx("h2",{className:"text-base font-semibold text-foreground",children:"We couldn't load your last Brand read"}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:["This is a connection problem on our side, not a sign that ",n.name," has never been analyzed. Retry the load before running a new analysis."]}),e.jsxs("div",{className:"flex flex-wrap gap-2",children:[e.jsxs(k,{variant:"outline",size:"sm",className:"gap-1.5",onClick:()=>q(X),children:[e.jsx(xe,{className:"w-3.5 h-3.5"})," Retry"]}),e.jsx(k,{variant:"ghost",size:"sm",onClick:ee,children:"Run a new Brand analysis instead"})]})]}):r?e.jsxs(e.Fragment,{children:[e.jsxs("section",{className:"space-y-2",children:[Ne&&e.jsx("p",{className:"text-lg sm:text-xl text-foreground leading-snug",children:Ne}),e.jsx("p",{className:"text-xs text-muted-foreground",children:Ye?"Read just now. This is SocialBrilliance's interpretation of your public signal, not measured audience research.":`Last read ${Ze??"previously"} — not a live reading. This is SocialBrilliance's interpretation of your public signal, not measured audience research.`}),ue&&e.jsxs("p",{className:"text-xs text-muted-foreground flex flex-wrap items-center gap-2",children:["We couldn't load your last Brand read, so this may be out of date.",e.jsx("button",{onClick:()=>q(X),className:"underline underline-offset-2 hover:text-foreground transition-colors",children:"Retry"})]}),pe&&e.jsxs("div",{className:"rounded-xl border border-border/60 bg-secondary/40 p-3 space-y-2",children:[e.jsx("p",{className:"text-xs text-foreground",children:"Brand context changed since this read."}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Refresh Brand analysis when you want the interpretation to incorporate the updated context."}),e.jsxs(k,{variant:"outline",size:"sm",className:"gap-1.5 text-xs min-h-[40px]",onClick:ee,children:[e.jsx(xe,{className:"w-3.5 h-3.5"})," Refresh analysis"]})]})]}),r.criticalInsight&&e.jsxs("section",{className:"rounded-2xl border border-primary/25 bg-primary/5 p-5 space-y-3",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(ms,{className:"w-4 h-4 text-primary"}),e.jsx("h2",{className:"text-xs font-semibold uppercase tracking-wider text-primary",children:"Most important thing"})]}),e.jsx("p",{className:"text-base text-foreground leading-relaxed",children:r.criticalInsight}),e.jsxs(k,{size:"sm",className:"gap-1.5",onClick:()=>se(ye.path,"critical_insight"),children:[ye.label," ",e.jsx(ps,{className:"w-3.5 h-3.5"})]}),r.nextMoves.length>0&&e.jsxs("details",{className:"pt-1",children:[e.jsxs("summary",{className:"text-xs text-muted-foreground cursor-pointer hover:text-foreground transition-colors",children:["Next moves (",r.nextMoves.length,")"]}),e.jsx("ol",{className:"mt-2 space-y-1.5",children:r.nextMoves.map((a,h)=>
11e.jsxs("li",{className:"text-sm text-foreground/85 flex gap-2",children:[e.jsxs("span",{className:"text-primary text-xs font-medium mt-0.5",children:[h+1,"."]}),e.jsx("span",{children:a})]},h))})]})]}),e.jsxs("div",{className:"space-y-3",children:[P([r.positioning.current,r.positioning.ideal,r.positioning.gap,r.positioning.mechanism,r.positioning.moat])&&e.jsxs(J,{title:"Positioning",summary:r.positioning.gap||r.positioning.current,defaultOpen:!0,children:[e.jsx(C,{label:"Reads as today",value:r.positioning.current}),e.jsx(C,{label:"Where it should land",value:r.positioning.ideal}),e.jsx(C,{label:"The gap",value:r.positioning.gap}),e.jsx(C,{label:"Your mechanism",value:r.positioning.mechanism}),e.jsx(C,{label:"Your moat",value:r.positioning.moat})]}),(r.audience.statements.length>0||P([n.goal,n.audience_preset]))&&e.jsxs(J,{title:"Audience",summary:n.audience_preset?`Saved target: ${n.audience_preset}`:"Who your signal appears to speak to",children:[e.jsx(C,{label:"Saved target audience",value:n.audience_preset}),e.jsx(C,{label:"Saved goal",value:n.goal}),r.audience.statements.length>0&&e.jsxs("div",{className:"space-y-3 pt-1",children:[e.jsx("span",{className:"text-[11px] font-medium uppercase tracking-wider text-muted-foreground",children:"How your signal reads to each audience (inferred)"}),r.audience.statements.map(a=>e.jsxs("div",{children:[e.jsx("span",{className:"text-[11px] font-medium uppercase tracking-wider text-primary",children:a.audience}),e.jsx("p",{className:"text-sm text-foreground/85 leading-relaxed mt-0.5",children:a.statement})]},a.audience))]})]}),(P([r.voice.primaryTone,r.voice.secondaryTones,r.voice.signaturePhrases,r.voice.sentencePatterns,r.voice.vocabularyLevel,r.voice.emotionalRegister,r.voice.blindSpots])||r.voice.expertise.length>0||(((Ce=n.voice_samples)==null?void 0:Ce.length)??0)>0)&&e.jsxs(J,{title:"Voice",summary:r.voice.primaryTone||"Your saved voice preferences",children:[e.jsx(C,{label:"Primary tone (inferred)",value:r.voice.primaryTone}),e.jsx(z,{label:"Secondary tones",items:r.voice.secondaryTones}),e.jsx(z,{label:"Sentence patterns",items:r.voice.sentencePatterns}),e.jsx(Y,{label:"Signature phrases",items:r.voice.signaturePhrases}),e.jsx(C,{label:"Vocabulary",value:r.voice.vocabularyLevel}),e.jsx(C,{label:"Emotional register",value:r.voice.emotionalRegister}),r.voice.expertise.length>0&&e.jsxs("div",{className:"space-y-2 pt-1",children:[e.jsx("span",{className:"text-[11px] font-medium uppercase tracking-wider text-muted-foreground",children:"Expertise your signal demonstrates"}),r.voice.expertise.map((a,h)=>e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-medium text-foreground",children:a.name}),a.evidence&&e.jsx("p",{className:"text-xs text-muted-foreground",children:a.evidence})]},`${a.name}-${h}`))]}),e.jsx(Y,{label:"Thin areas",items:r.voice.blindSpots}),e.jsxs("div",{className:"pt-1 space-y-2",children:[e.jsx(z,{label:"Saved recurring themes",items:n.recurring_themes||[]}),e.jsx(z,{label:"Phrases you've banned",items:n.banned_phrases||[]}),e.jsx(C,{label:"Saved CTA style",value:n.cta_style}),(((Se=n.voice_samples)==null?void 0:Se.length)??0)>0&&e.jsxs("p",{className:"text-xs text-muted-foreground",children:["Based partly on ",n.voice_samples.length," saved voice sample",n.voice_samples.length===1?"":"s","."]})]})]}),P([r.proof.strongest,r.proof.underused,r.proof.missing])&&e.jsxs(J,{title:"Proof",summary:r.proof.missing.length>0?`${r.proof.missing.length} credibility gap${r.proof.missing.length===1?"":"s"} identified`:"Credibility your signal carries",children:[e.jsx(Y,{label:"Strongest proof",items:r.proof.strongest}),e.jsx(Y,{label:"Underused proof",items:r.proof.underused}),e.jsx(Y,{label:"Credibility gaps",items:r.proof.missing})]}),P([r.perception.readsAs,r.perception.intended,r.perception.gap,r.perception.associations,r.perception.missingAssociations])&&e.jsxs(J,{title:"Perception gaps",summary:r.perception.gap||r.perception.readsAs,children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:"Inferred from your public signal by SocialBrilliance — not measured audience research."}),e.jsx(C,{label:"Your signal currently reads as",value:r.perception.readsAs}),e.jsx(C,{label:"What you intend to be known for",value:r.perception.intended}),e.jsx(C,{label:"The gap",value:r.perception.gap}),e.jsx(z,{label:"Associations your signal carries (inferred)",items:r.perception.associations}),e.jsx(z,{label:"Associations missing (worth building)",items:r.perception.missingAssociations,tone:"primary"})]})]}),e.jsxs("div",{className:"flex flex-wrap gap-2 pt-1",children:[e.jsxs(k,{variant:"outline",size:"sm",className:"gap-1.5 text-xs",onClick:ee,children:[e.jsx(xe,{className:"w-3.5 h-3.5"})," Refresh Brand analysis"]}),e.jsxs(k,{variant:"outline",size:"sm",className:"gap-1.5 text-xs",onClick:()=>se("/strategy?source=brand","deeper_diagnosis"),children:[e.jsx(Me,{className:"w-3.5 h-3.5"})," Run deeper diagnosis"]}),e.jsxs(k,{variant:"ghost",size:"sm",className:"gap-1.5 text-xs",onClick:()=>{Ge(a=>!a),M($.BRAND_ACTION_CLICKED,{action:"history_alerts"})},children:[e.jsx(ze,{className:"w-3.5 h-3.5"})," History & alerts",e.jsx(me,{className:`w-3 h-3 transition-transform ${he?"rotate-180":""}`})]}),e.jsxs(k,{variant:"ghost",size:"sm",className:"gap-1.5 text-xs",onClick:()=>{Ke(a=>!a),M($.BRAND_ACTION_CLICKED,{action:"ask_brand"})},children:[e.jsx(Bs,{className:"w-3.5 h-3.5"})," Ask Brand",e.jsx(me,{className:`w-3 h-3 transition-transform ${V?"rotate-180":""}`})]})]}),he&&e.jsx(Is,{deviceId:Q,profileId:(n==null?void 0:n.id)||null,currentAnalysis:N,currentScore:r.brandScore??0},ae),V&&e.jsxs("div",{className:"bg-card border border-border rounded-2xl overflow-hidden flex flex-col",style:{height:"60vh"},children:[pe&&e.jsxs("div",{className:"flex items-start gap-2 border-b border-border bg-secondary/40 px-4 py-2.5",children:[e.jsx(Ee,{className:"w-3.5 h-3.5 text-muted-foreground mt-0.5 shrink-0"}),e.jsx("p",{className:"text-[11px] text-muted-foreground",children:"Your saved brand profile is newer than the Brand read shown above. Refreshing the analysis gives a consistent interpretation."})]}),e.jsxs("div",{className:"flex-1 overflow-y-auto p-4 space-y-3",children:[D.length===0&&e.jsx("p",{className:"text-xs text-muted-foreground",children:"Ask about your positioning, audience, proof, or what to publish next. Answers use the Brand read shown above."}),D.map((a,h)=>e.jsx("div",{className:`flex ${a.role==="user"?"justify-end":"justify-start"}`,children:e.jsx("div",{className:`max-w-[85%] rounded-2xl px-4 py-2.5 text-sm leading-relaxed ${a.role==="user"?"bg-primary text-primary-foreground":"bg-secondary/50 text-foreground"}`,children:a.content.split(`
12`).map((_,I)=>e.jsx("p",{className:I>0?"mt-1.5":"",children:_.split(/(\*\*.*?\*\*)/).map((v,B)=>v.startsWith("**")&&v.endsWith("**")?e.jsx("strong",{children:v.slice(2,-2)},B):v)},I))})},h)),Z&&((ke=D[D.length-1])==null?void 0:ke.role)==="user"&&e.jsx("div",{className:"flex justify-start",children:e.jsx("div",{className:"bg-secondary/50 rounded-2xl px-4 py-2.5",children:e.jsx(Te,{className:"w-4 h-4 animate-spin text-muted-foreground"})})}),e.jsx("div",{ref:fe})]}),e.jsxs("div",{className:"border-t border-border p-3",children:[e.jsxs("div",{className:"flex gap-2",children:[e.jsx($e,{placeholder:"Ask Brand anything…",value:W,onChange:a=>de(a.target.value),onKeyDown:a=>
12{a.key==="Enter"&&!a.shiftKey&&(a.preventDefault(),je())},rows:1,className:"text-sm resize-none flex-1 min-h-[40px] max-h-[120px]"}),e.jsx(k,{size:"icon",onClick:je,disabled:!W.trim()||Z,className:"shrink-0 self-end","aria-label":"Send message",children:e.jsx(Rs,{className:"w-4 h-4"})})]}),e.jsx("div",{className:"flex flex-wrap gap-1.5 mt-2",children:["How should I pitch to investors?","What content should I post this week?","Write my LinkedIn headline"].map(a=>e.jsx("button",{onClick:()=>de(a),className:"text-[10px] px-2 py-1 rounded-full bg-secondary/50 text-muted-foreground hover:text-foreground transition-colors",children:a},a))})]})]})]}):e.jsxs("section",{className:"rounded-2xl border border-border/60 bg-card p-6 sm:p-8 space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsxs("h2",{className:"text-base font-semibold text-foreground",children:["No Brand analysis yet for ",n.name]}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Nothing has been read for this brand, so there is nothing to report yet. Run an analysis and Brand will interpret your positioning, audience, voice, proof, and perception gaps from your public signal."})]}),e.jsx($e,{placeholder:"Optional: paste recent posts, your bio, or interview excerpts to sharpen the read…",value:u,onChange:a=>c(a.target.value),rows:3,className:"text-sm resize-none"}),e.jsxs(k,{onClick:ee,className:"w-full gap-2",children:[e.jsx(Me,{className:"w-4 h-4"})," Run Brand analysis"]}),e.jsx("button",{onClick:()=>se("/strategy?source=brand","deeper_diagnosis"),className:"w-full text-xs text-muted-foreground hover:text-foreground transition-colors",children:"Or run a deeper diagnosis on a URL"})]}):e.jsxs("section",{className:"rounded-2xl border border-border/60 bg-card p-8 text-center space-y-3",children:[e.jsx("h2",{className:"text-base font-semibold text-foreground",children:"No brand set up yet"}),e.jsx("p",{className:"text-sm text-muted-foreground max-w-md mx-auto",children:"Create a brand with your goal, audience, and a few voice samples. Brand then reads how your public signal comes across and where it drifts from what you intend."}),e.jsx(k,{variant:"outline",size:"sm",onClick:()=>se("/composer?new_profile=1","create_profile"),children:"Create a brand"})]})]}),Fe&&n&&e.jsx(Cs,{profile:n,onSave:Ve,onClose:()=>ne(!1),saving:j})]})}export{pt as default};

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.