react-semaphor 0.0.498 → 0.0.499
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.
- package/dist/chunks/{dashboard-plus-Dam5l-m3.js → dashboard-plus-CoNExipD.js} +2 -2
- package/dist/chunks/{dashboard-plus-DZais8J6.js → dashboard-plus-N66fUb47.js} +2 -2
- package/dist/chunks/{index-DMXY0DgW.js → index-B-1DOZy1.js} +35 -35
- package/dist/chunks/{index-BtYSXA37.js → index-Bu82AvMn.js} +1538 -1538
- package/dist/dashboard/index.cjs +1 -1
- package/dist/dashboard/index.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.js +2 -2
- package/dist/surfboard/index.cjs +1 -1
- package/dist/surfboard/index.js +1 -1
- package/dist/types/main.d.ts +5 -5
- package/package.json +1 -1
|
@@ -4,7 +4,7 @@ var Ga = (t) => {
|
|
|
4
4
|
var An = (t, a, n) => a.has(t) || Ga("Cannot " + n);
|
|
5
5
|
var ue = (t, a, n) => (An(t, a, "read from private field"), n ? n.call(t) : a.get(t)), Ae = (t, a, n) => a.has(t) ? Ga("Cannot add the same private member more than once") : a instanceof WeakSet ? a.add(t) : a.set(t, n), Ce = (t, a, n, r) => (An(t, a, "write to private field"), r ? r.call(t, n) : a.set(t, n), n), ze = (t, a, n) => (An(t, a, "access private method"), n);
|
|
6
6
|
import { jsxs as s, jsx as e, Fragment as ae } from "react/jsx-runtime";
|
|
7
|
-
import { z as Br, A as Gn, B as _r, Q as ds, C as us, E as Pr, F as Gr, G as $r, H as Jr, I as Hr, J as Ur, K as $a, L as Ja, M as Yr, N as Kr, O as j, P as ia, v as $, w as Tt, R as ot, T as mn, U as ms, V as P, W as Se, X as hn, Y as la, Z as oa, _ as ca, $ as H, a0 as we, a1 as hs, a2 as ps, a3 as fs, a4 as se, a5 as gs, a6 as ys, a7 as vs, a8 as Qr, a9 as Ha, aa as G, ab as da, ac as Wr, ad as le, ae as oe, af as ce, ag as de, ah as nt, ai as $e, aj as K, ak as bs, al as Ye, am as Ke, an as Ot, ao as Ze, ap as yt, aq as It, ar as Jt, as as Et, at, au as xs, av as Ht, aw as ua, ax as Ns, ay as ma, az as $n, aA as Cs, x as M, aB as ws, aC as St, aD as tn, aE as vt, aF as pn, aG as fn, aH as gn, aI as yn, aJ as vn, aK as bn, aL as xn, aM as Vt, t as _e, aN as Ss, aO as ks, aP as Ms, aQ as Ds, l as Ts, k as Os, f as qe, aR as Zr, e as Pe, aS as Xr, g as ft, h as Ge, aT as Is, j as ei, aU as ti, aV as ni, aW as it, aX as st, aY as Es, aZ as ai, y as X, d as As, a_ as jt, a$ as Xe, b0 as si, b1 as Jn, b2 as ha, b3 as pa, b4 as Nn, b5 as He, b6 as zs, r as nn, p as Ua, b7 as Ee, b8 as ri, b9 as lt, ba as ii, bb as li, bc as oi, bd as ci, be as Fs, bf as di, bg as Cn, bh as ui, bi as mi, bj as Hn, bk as be, bl as et, bm as Ne, bn as tt, bo as hi, bp as Ls, bq as Rs, br as Vs, bs as pi, bt as fi, bu as gi, bv as yi, bw as zn, bx as vi, by as Ya, bz as bi, bA as Ka, bB as xi, bC as Ni, bD as Ci, bE as wi, bF as js, bG as Si, bH as fa, bI as qs, bJ as wn, bK as Bs, bL as Nt, bM as _s, bN as ki, bO as Ps, bP as Sn, bQ as Mi, bR as Di, bS as Gs, bT as $s, bU as kn, bV as Js, bW as Ti, bX as Ut, bY as Yt, bZ as De, b_ as en, b$ as Oi, c0 as Ii, c1 as Ei, c2 as ga, c3 as Hs, c4 as Ai, c5 as an, c6 as Us, c7 as sn, c8 as Ys, c9 as zi, ca as Fi, cb as Li, cc as Ks, cd as Ri, ce as ct, cf as he, cg as pe, ch as fe, ci as Mn, cj as Vi, ck as ji, cl as qi, cm as Bi, q as _i, cn as Qa, co as Qs, cp as Pi, cq as Gi, cr as $i, cs as Ws, ct as ya, cu as At, cv as va, cw as Ue, cx as Ji, cy as zt, cz as Zs, cA as Hi, cB as Ui, cC as Yi, cD as Ki, cE as Wa, cF as Qi, cG as Wi, cH as Zi, cI as Xi, cJ as el, cK as tl, cL as nl, cM as al, cN as sl, cO as rl, cP as il, cQ as Xs, cR as Un, cS as Fn, cT as ll, cU as qt, cV as Je, cW as ba, cX as ge, cY as ol, cZ as cl, c_ as dl, c$ as kt, d0 as Be, d1 as ul, d2 as ml, d3 as Yn, d4 as Za, d5 as Kn, d6 as Qn, d7 as rn, d8 as Mt, d9 as Bt, da as hl, db as er, dc as pl, dd as fl, de as gl, df as yl, dg as vl, dh as bl } from "./index-
|
|
7
|
+
import { z as Br, A as Gn, B as _r, Q as ds, C as us, E as Pr, F as Gr, G as $r, H as Jr, I as Hr, J as Ur, K as $a, L as Ja, M as Yr, N as Kr, O as j, P as ia, v as $, w as Tt, R as ot, T as mn, U as ms, V as P, W as Se, X as hn, Y as la, Z as oa, _ as ca, $ as H, a0 as we, a1 as hs, a2 as ps, a3 as fs, a4 as se, a5 as gs, a6 as ys, a7 as vs, a8 as Qr, a9 as Ha, aa as G, ab as da, ac as Wr, ad as le, ae as oe, af as ce, ag as de, ah as nt, ai as $e, aj as K, ak as bs, al as Ye, am as Ke, an as Ot, ao as Ze, ap as yt, aq as It, ar as Jt, as as Et, at, au as xs, av as Ht, aw as ua, ax as Ns, ay as ma, az as $n, aA as Cs, x as M, aB as ws, aC as St, aD as tn, aE as vt, aF as pn, aG as fn, aH as gn, aI as yn, aJ as vn, aK as bn, aL as xn, aM as Vt, t as _e, aN as Ss, aO as ks, aP as Ms, aQ as Ds, l as Ts, k as Os, f as qe, aR as Zr, e as Pe, aS as Xr, g as ft, h as Ge, aT as Is, j as ei, aU as ti, aV as ni, aW as it, aX as st, aY as Es, aZ as ai, y as X, d as As, a_ as jt, a$ as Xe, b0 as si, b1 as Jn, b2 as ha, b3 as pa, b4 as Nn, b5 as He, b6 as zs, r as nn, p as Ua, b7 as Ee, b8 as ri, b9 as lt, ba as ii, bb as li, bc as oi, bd as ci, be as Fs, bf as di, bg as Cn, bh as ui, bi as mi, bj as Hn, bk as be, bl as et, bm as Ne, bn as tt, bo as hi, bp as Ls, bq as Rs, br as Vs, bs as pi, bt as fi, bu as gi, bv as yi, bw as zn, bx as vi, by as Ya, bz as bi, bA as Ka, bB as xi, bC as Ni, bD as Ci, bE as wi, bF as js, bG as Si, bH as fa, bI as qs, bJ as wn, bK as Bs, bL as Nt, bM as _s, bN as ki, bO as Ps, bP as Sn, bQ as Mi, bR as Di, bS as Gs, bT as $s, bU as kn, bV as Js, bW as Ti, bX as Ut, bY as Yt, bZ as De, b_ as en, b$ as Oi, c0 as Ii, c1 as Ei, c2 as ga, c3 as Hs, c4 as Ai, c5 as an, c6 as Us, c7 as sn, c8 as Ys, c9 as zi, ca as Fi, cb as Li, cc as Ks, cd as Ri, ce as ct, cf as he, cg as pe, ch as fe, ci as Mn, cj as Vi, ck as ji, cl as qi, cm as Bi, q as _i, cn as Qa, co as Qs, cp as Pi, cq as Gi, cr as $i, cs as Ws, ct as ya, cu as At, cv as va, cw as Ue, cx as Ji, cy as zt, cz as Zs, cA as Hi, cB as Ui, cC as Yi, cD as Ki, cE as Wa, cF as Qi, cG as Wi, cH as Zi, cI as Xi, cJ as el, cK as tl, cL as nl, cM as al, cN as sl, cO as rl, cP as il, cQ as Xs, cR as Un, cS as Fn, cT as ll, cU as qt, cV as Je, cW as ba, cX as ge, cY as ol, cZ as cl, c_ as dl, c$ as kt, d0 as Be, d1 as ul, d2 as ml, d3 as Yn, d4 as Za, d5 as Kn, d6 as Qn, d7 as rn, d8 as Mt, d9 as Bt, da as hl, db as er, dc as pl, dd as fl, de as gl, df as yl, dg as vl, dh as bl } from "./index-Bu82AvMn.js";
|
|
8
8
|
import * as Ie from "react";
|
|
9
9
|
import Le, { useState as V, useMemo as rt, useRef as gt, useCallback as ke, useEffect as ve, Suspense as xl } from "react";
|
|
10
10
|
import "react-dom";
|
|
@@ -2725,7 +2725,7 @@ function Vo({
|
|
|
2725
2725
|
function O(w) {
|
|
2726
2726
|
w === !0 && (d || x) && o ? (f({
|
|
2727
2727
|
connectionId: o,
|
|
2728
|
-
connectionType: b === "S3" || b === "API" ? "DuckDb" : b,
|
|
2728
|
+
connectionType: b === "S3" || b === "API" ? "DuckDb" : b || "",
|
|
2729
2729
|
databaseName: c || "",
|
|
2730
2730
|
schemaName: h || "",
|
|
2731
2731
|
tableName: d || "",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";var Ot=s=>{throw TypeError(s)};var Je=(s,a,n)=>a.has(s)||Ot("Cannot "+n);var W=(s,a,n)=>(Je(s,a,"read from private field"),n?n.call(s):a.get(s)),oe=(s,a,n)=>a.has(s)?Ot("Cannot add the same private member more than once"):a instanceof WeakSet?a.add(s):a.set(s,n),te=(s,a,n,r)=>(Je(s,a,"write to private field"),r?r.call(s,n):a.set(s,n),n),ie=(s,a,n)=>(Je(s,a,"access private method"),n);const e=require("react/jsx-runtime"),t=require("./index-
|
|
1
|
+
"use strict";var Ot=s=>{throw TypeError(s)};var Je=(s,a,n)=>a.has(s)||Ot("Cannot "+n);var W=(s,a,n)=>(Je(s,a,"read from private field"),n?n.call(s):a.get(s)),oe=(s,a,n)=>a.has(s)?Ot("Cannot add the same private member more than once"):a instanceof WeakSet?a.add(s):a.set(s,n),te=(s,a,n,r)=>(Je(s,a,"write to private field"),r?r.call(s,n):a.set(s,n),n),ie=(s,a,n)=>(Je(s,a,"access private method"),n);const e=require("react/jsx-runtime"),t=require("./index-B-1DOZy1.js"),L=require("react");require("react-dom");function Ls(s){const a=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(s){for(const n in s)if(n!=="default"){const r=Object.getOwnPropertyDescriptor(s,n);Object.defineProperty(a,n,r.get?r:{enumerable:!0,get:()=>s[n]})}}return a.default=s,Object.freeze(a)}const re=Ls(L);function Ft(s,a){return s.filter(n=>!a.includes(n))}function Os(s,a,n){const r=s.slice(0);return r[a]=n,r}var pe,ne,Ce,Ne,ae,he,Ie,De,Te,ee,tt,st,nt,at,rt,Ht,Fs=(Ht=class extends t.Subscribable{constructor(a,n,r){super();oe(this,ee);oe(this,pe);oe(this,ne);oe(this,Ce);oe(this,Ne);oe(this,ae);oe(this,he);oe(this,Ie);oe(this,De);oe(this,Te,[]);te(this,pe,a),te(this,Ne,r),te(this,Ce,[]),te(this,ae,[]),te(this,ne,[]),this.setQueries(n)}onSubscribe(){this.listeners.size===1&&W(this,ae).forEach(a=>{a.subscribe(n=>{ie(this,ee,at).call(this,a,n)})})}onUnsubscribe(){this.listeners.size||this.destroy()}destroy(){this.listeners=new Set,W(this,ae).forEach(a=>{a.destroy()})}setQueries(a,n,r){if(te(this,Ce,a),te(this,Ne,n),process.env.NODE_ENV!=="production"){const o=a.map(i=>W(this,pe).defaultQueryOptions(i).queryHash);new Set(o).size!==o.length&&console.warn("[QueriesObserver]: Duplicate Queries found. This might result in unexpected behavior.")}t.notifyManager.batch(()=>{const o=W(this,ae),i=ie(this,ee,nt).call(this,W(this,Ce));te(this,Te,i),i.forEach(h=>h.observer.setOptions(h.defaultedQueryOptions,r));const l=i.map(h=>h.observer),u=l.map(h=>h.getCurrentResult()),c=l.some((h,d)=>h!==o[d]);o.length===l.length&&!c||(te(this,ae,l),te(this,ne,u),this.hasListeners()&&(Ft(o,l).forEach(h=>{h.destroy()}),Ft(l,o).forEach(h=>{h.subscribe(d=>{ie(this,ee,at).call(this,h,d)})}),ie(this,ee,rt).call(this)))})}getCurrentResult(){return W(this,ne)}getQueries(){return W(this,ae).map(a=>a.getCurrentQuery())}getObservers(){return W(this,ae)}getOptimisticResult(a,n){const r=ie(this,ee,nt).call(this,a),o=r.map(i=>i.observer.getOptimisticResult(i.defaultedQueryOptions));return[o,i=>ie(this,ee,st).call(this,i??o,n),()=>ie(this,ee,tt).call(this,o,r)]}},pe=new WeakMap,ne=new WeakMap,Ce=new WeakMap,Ne=new WeakMap,ae=new WeakMap,he=new WeakMap,Ie=new WeakMap,De=new WeakMap,Te=new WeakMap,ee=new WeakSet,tt=function(a,n){return n.map((r,o)=>{const i=a[o];return r.defaultedQueryOptions.notifyOnChangeProps?i:r.observer.trackResult(i,l=>{n.forEach(u=>{u.observer.trackProp(l)})})})},st=function(a,n){return n?((!W(this,he)||W(this,ne)!==W(this,De)||n!==W(this,Ie))&&(te(this,Ie,n),te(this,De,W(this,ne)),te(this,he,t.replaceEqualDeep(W(this,he),n(a)))),W(this,he)):a},nt=function(a){const n=new Map(W(this,ae).map(o=>[o.options.queryHash,o])),r=[];return a.forEach(o=>{const i=W(this,pe).defaultQueryOptions(o),l=n.get(i.queryHash);l?r.push({defaultedQueryOptions:i,observer:l}):r.push({defaultedQueryOptions:i,observer:new t.QueryObserver(W(this,pe),i)})}),r},at=function(a,n){const r=W(this,ae).indexOf(a);r!==-1&&(te(this,ne,Os(W(this,ne),r,n)),ie(this,ee,rt).call(this))},rt=function(){var a;if(this.hasListeners()){const n=W(this,he),r=ie(this,ee,tt).call(this,W(this,ne),W(this,Te)),o=ie(this,ee,st).call(this,r,(a=W(this,Ne))==null?void 0:a.combine);n!==o&&t.notifyManager.batch(()=>{this.listeners.forEach(i=>{i(W(this,ne))})})}},Ht);function zs({queries:s,...a},n){const r=t.useQueryClient(),o=t.useIsRestoring(),i=t.useQueryErrorResetBoundary(),l=re.useMemo(()=>s.map(m=>{const v=r.defaultQueryOptions(m);return v._optimisticResults=o?"isRestoring":"optimistic",v}),[s,r,o]);l.forEach(m=>{t.ensureSuspenseTimers(m),t.ensurePreventErrorBoundaryRetry(m,i)}),t.useClearResetErrorBoundary(i);const[u]=re.useState(()=>new Fs(r,l,a)),[c,h,d]=u.getOptimisticResult(l,a.combine),x=!o&&a.subscribed!==!1;re.useSyncExternalStore(re.useCallback(m=>x?u.subscribe(t.notifyManager.batchCalls(m)):t.noop,[u,x]),()=>u.getCurrentResult(),()=>u.getCurrentResult()),re.useEffect(()=>{u.setQueries(l,a,{listeners:!1})},[l,a,u]);const p=c.some((m,v)=>t.shouldSuspend(l[v],m))?c.flatMap((m,v)=>{const f=l[v];if(f){const k=new t.QueryObserver(r,f);if(t.shouldSuspend(f,m))return t.fetchOptimistic(f,k,i);t.willFetch(m,o)&&t.fetchOptimistic(f,k,i)}return[]}):[];if(p.length>0)throw Promise.all(p);const j=c.find((m,v)=>{const f=l[v];return f&&t.getHasError({result:m,errorResetBoundary:i,throwOnError:f.throwOnError,query:r.getQueryCache().get(f.queryHash),suspense:f.suspense})});if(j!=null&&j.error)throw j.error;return h(d())}/**
|
|
2
2
|
* @license lucide-react v0.453.0 - ISC
|
|
3
3
|
*
|
|
4
4
|
* This source code is licensed under the ISC license.
|
|
@@ -388,7 +388,7 @@
|
|
|
388
388
|
*
|
|
389
389
|
* This source code is licensed under the ISC license.
|
|
390
390
|
* See the LICENSE file in the root directory of this source tree.
|
|
391
|
-
*/const Zt=t.createLucideIcon("Type",[["polyline",{points:"4 7 4 4 20 4 20 7",key:"1nosan"}],["line",{x1:"9",x2:"15",y1:"20",y2:"20",key:"swin9y"}],["line",{x1:"12",x2:"12",y1:"4",y2:"20",key:"1tx1rr"}]]);function Ct(s){var a;return{...s,sheets:(a=s==null?void 0:s.sheets)==null?void 0:a.map(n=>{var r;return{...n,frames:(r=n.frames)==null?void 0:r.map(o=>{var i;return{...o,cards:(i=o.cards)==null?void 0:i.map(l=>es(l))}})}})}}function es(s){return{...s,customCfg:On(s.customCfg),data:[]}}function On(s){var a,n;return s?{...s,data:{...s==null?void 0:s.data,labels:[],datasets:(n=(a=s==null?void 0:s.data)==null?void 0:a.datasets)==null?void 0:n.map(r=>({...r,data:[]}))}}:null}async function Fn(s,a,n,r){if(!r)return null;const o=JSON.stringify(a);return await t.postRequest("https://semaphor.cloud/api/v1/lens",r,{name:s,template:o,filterValues:n,operation:"create"})}async function ot(s,a){if(!a)return null;const{id:n,template:r,filterValues:o,isDefault:i,shared:l}=s;return await t.postRequest("https://semaphor.cloud/api/v1/lens",a,{lensId:n,template:r,filterValues:o,isDefault:i,shared:l,operation:"update"})}async function zn(s,a){return a?await t.postRequest("https://semaphor.cloud/api/v1/lens",a,{operation:"delete",lensId:s}):null}function Bn(){const[s,a]=L.useState(""),[n,r]=L.useState(!1),o=t.useDashboardStore(m=>m.lenses),{setLenses:i,setSelectedLensId:l}=t.useDashboardActions(),[u,c]=L.useState(!1),[h,d]=L.useState(!1),x=t.useDashboardStore(m=>m.dashboard),g=t.useDashboardStore(m=>m.filterValues),{authToken:p}=t.useSemaphorContext();async function j(){c(!0);const m=Ct(x),v=await Fn(s,m,g,p==null?void 0:p.accessToken);v&&(i([...o||[],v]),l(v.id),d(!1)),c(!1)}return e.jsxs(t.Dialog,{open:h,onOpenChange:d,children:[e.jsx(t.DialogTrigger,{asChild:!0,children:e.jsxs(t.Button,{variant:"secondary",className:"h-7 p-2",size:"sm",children:[e.jsx(t.Plus,{className:"mr-2 h-4 w-4"}),"Lens"]})}),e.jsxs(t.DialogContent,{className:"sm:max-w-[425px]",children:[e.jsxs(t.DialogHeader,{children:[e.jsx(t.DialogTitle,{children:"Create Lens"}),e.jsx(t.DialogDescription,{children:"Lenses are private by default and are only visible to you."})]}),e.jsx("div",{className:"grid gap-4 py-4",children:e.jsxs("div",{className:"grid grid-cols-8 items-center gap-4",children:[e.jsx(t.Label,{className:"text-right",children:"Name"}),e.jsx(t.Input,{value:s,onChange:m=>{a(m.target.value)},id:"name",className:"col-span-7 focus-visible:ring-0 focus-visible:ring-ring focus-visible:ring-offset-0"})]})}),n&&e.jsx("p",{className:"text-sm",children:"The lens name already exists. Use a differnet name."}),e.jsx(t.DialogFooter,{children:e.jsx(t.Button,{size:"sm",variant:"outline",type:"submit",onClick:j,disabled:u,children:u?"Creating...":"Add"})})]})]})}function Rn(){const[s,a]=L.useState(!1),n=t.useDashboardStore(d=>d.lenses),r=t.useDashboardStore(d=>d.selectedLensId),{setLenses:o,setSelectedLensId:i}=t.useDashboardActions(),{authToken:l}=t.useSemaphorContext();async function u(d){i(d)}async function c(d){if(await zn(d,l==null?void 0:l.accessToken)){const g=n==null?void 0:n.filter(p=>p.id!==d);o(g||[]),i("original")}}async function h(d){const x=n==null?void 0:n.map(j=>j.id===d.id?{...j,isDefault:!d.isDefault}:{...j,isDefault:!1});o(x||[]);const g={...d,isDefault:!d.isDefault};await ot(g,l==null?void 0:l.accessToken);const p=n==null?void 0:n.find(j=>j.isDefault);if(p&&p.id!==d.id){const j={...p,isDefault:!1};await ot(j,l==null?void 0:l.accessToken)}}return e.jsxs(t.DropdownMenu,{open:s,onOpenChange:a,children:[e.jsx(t.DropdownMenuTrigger,{asChild:!0,children:e.jsx(t.IconButton,{tooltip:"Lenses",className:"h-7 p-2",children:e.jsx(hn,{className:"h-4 w-4"})})}),e.jsxs(t.DropdownMenuContent,{align:"end",className:"w-60",children:[e.jsx(t.DropdownMenuLabel,{className:"px-3",children:"Lenses"}),e.jsx(t.DropdownMenuSeparator,{}),e.jsxs(t.DropdownMenuRadioGroup,{value:r,onValueChange:u,children:[e.jsx(t.DropdownMenuRadioItem,{value:"original",children:"Original"}),n==null?void 0:n.map(d=>e.jsxs(t.DropdownMenuRadioItem,{className:"group flex justify-between gap-2",value:d.id,children:[e.jsx("p",{children:d.name}),e.jsxs("span",{className:"flex items-center gap-3",children:[e.jsx(fn,{onClick:x=>{x.preventDefault(),h(d)},className:t.cn("size-3.5 text-muted-foreground hover:cursor-pointer hover:text-foreground group-hover:visible",{invisible:!d.isDefault,"text-green-500":d.isDefault}),strokeWidth:1.5}),e.jsx(t.Cross2Icon,{onClick:x=>{x.preventDefault(),c(d.id)},className:"invisible size-3.5 text-muted-foreground hover:cursor-pointer hover:text-foreground group-hover:visible",strokeWidth:1.5})]})]},d.id))]})]})]})}function ts(){var i;const s=t.useDashboardStore.getState().authToken,a=t.useDashboardStore.getState().theme,n=t.useDashboardStore.getState().filterValues,r=(i=t.useDashboardStore.getState().dashboard)==null?void 0:i.title,o=t.useDashboardStore.getState().selectedSheetId;return{authToken:s,theme:a||"light",filterValues:n,dashboardTitle:r||"",selectedSheetId:o||""}}async function ss(){const s=t.useDashboardStore.getState().isDownloadingPdf,{authToken:a,theme:n,filterValues:r,dashboardTitle:o,selectedSheetId:i}=ts();if(s)return;const l=new URLSearchParams;l.set("token",(a==null?void 0:a.accessToken)||""),l.set("theme",n),l.set("filterValues",JSON.stringify(r)),l.set("selectedSheetId",i||"");const u=`https://semaphor.cloud/api/v1/generate-pdf?params=${encodeURIComponent(l.toString())}`;t.useDashboardStore.setState({isDownloadingPdf:!0});const d=(await(await fetch(u,{method:"GET",headers:{Authorization:`Bearer ${a==null?void 0:a.accessToken}`}})).json()).url,x=document.createElement("a");x.href=d;const g=`${o}-${new Date().toISOString()}.pdf`;x.download=g,x.click(),t.useDashboardStore.setState({isDownloadingPdf:!1})}function Pn({iconClassName:s}){const{authToken:a}=t.useSemaphorContext();t.useDashboardStore(o=>{var i;return(i=o.dashboard)==null?void 0:i.title}),t.useDashboardStore(o=>o.theme),t.useDashboardStore(o=>o.filterValues);const n=t.useDashboardStore(o=>o.isDownloadingPdf);async function r(){await ss()}return e.jsx(t.IconButton,{tooltip:"Download PDF",className:"h-7 p-2",onClick:r,children:n?e.jsx(ce,{className:t.cn("size-3.5 animate-spin",s)}):e.jsx(t.Download,{className:t.cn("size-4",s)})})}function le({defaultValue:s,label:a,options:n,value:r,onValueChange:o,className:i,icon:l,showPlaceholderLabel:u=!0}){return e.jsxs(t.Select,{defaultValue:s,value:r,onValueChange:o,children:[e.jsx(t.SelectTrigger,{className:t.cn("w-full focus:ring-0 focus:ring-offset-0",i),children:e.jsxs("div",{className:"flex items-center gap-2",children:[l&&e.jsx("span",{children:l}),u&&e.jsx(t.SelectValue,{placeholder:a})]})}),e.jsx(t.SelectContent,{children:e.jsxs(t.SelectGroup,{children:[e.jsx(t.SelectLabel,{children:a}),n.map(c=>e.jsx(t.SelectItem,{value:c.value,children:c.label},c.value))]})})]})}const Rt=[{value:"daily",label:"Daily"},{value:"weekly",label:"Weekly"},{value:"biweekly",label:"Bi-Weekly"},{value:"monthly",label:"Monthly"}];function ns({triggerButtonClassName:s}){const{data:a,handleScheduleSubscription:n,handleDeleteSubscription:r,status:o,error:i,isDeleting:l}=Vn(),[u,c]=L.useState(""),[h,d]=L.useState(Rt[0].value),[x,g]=L.useState("06:00"),[p,j]=L.useState("");return e.jsx(t.TooltipProvider,{delayDuration:0,children:e.jsxs(t.Popover,{children:[e.jsx(t.PopoverTrigger,{asChild:!0,children:e.jsx(t.IconButton,{tooltip:"Schedule Dashboard",className:t.cn("h-7 p-2",s),children:e.jsx(tn,{className:"size-4"})})}),e.jsx(t.PopoverContent,{align:"end",className:"w-80",children:e.jsxs("div",{className:"grid gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("h4",{className:"font-medium leading-none",children:"Schedule Subscription"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Subscribe to receive dashboard via email"})]}),e.jsxs("div",{className:"grid gap-2",children:[e.jsxs("div",{className:"grid grid-cols-3 items-center gap-4",children:[e.jsx(t.Label,{htmlFor:"email",children:"Email"}),e.jsx(t.Input,{id:"email",value:u,onChange:m=>c(m.target.value),className:`col-span-2 h-8 ${t.removeRing}`})]}),e.jsxs("div",{className:"grid grid-cols-3 items-center gap-4",children:[e.jsx(t.Label,{htmlFor:"subject",children:"Subject"}),e.jsx(t.Input,{id:"subject",placeholder:"Monthly Sales Report",value:p,onChange:m=>{j(m.target.value.replace(/[^a-zA-Z0-9\s-_]/g,""))},className:`col-span-2 h-8 ${t.removeRing}`})]}),e.jsxs("div",{className:"grid grid-cols-3 items-center gap-4",children:[e.jsx(t.Label,{htmlFor:"frequency",children:"Frequency"}),e.jsx(le,{className:"col-span-2 h-8",label:"Select Frequency",options:Rt,value:h,onValueChange:m=>d(m)})]}),e.jsxs("div",{className:"grid grid-cols-3 items-center gap-4",children:[e.jsx(t.Label,{htmlFor:"time",children:"Time"}),e.jsx(t.Input,{id:"time",type:"time",value:x,onChange:m=>g(m.target.value),className:`col-span-2 h-8 ${t.removeRing}`})]}),e.jsx("div",{className:"mt-2 flex justify-end",children:e.jsxs(t.Button,{onClick:async()=>{await n({email:u,frequency:h,time:x,subject:p}),g("00:00"),c(""),j("")},className:"h-8 p-2",disabled:o==="loading"||!u,children:[o==="loading"&&e.jsx(ce,{className:"mr-2 size-4 animate-spin"}),o==="success"&&e.jsx(t.Check,{className:"mr-2 size-4"}),o==="error"&&e.jsxs(t.Tooltip,{children:[e.jsx(t.TooltipTrigger,{children:e.jsx(Ut,{className:"mr-2 size-4 text-destructive"})}),e.jsx(t.TooltipContent,{children:e.jsx("p",{children:i})})]}),"Schedule"]})}),a&&a.length>0&&e.jsxs("div",{className:"space-y-4",children:[e.jsx(t.Separator,{className:"my-2"}),e.jsx("div",{className:"space-y-2",children:e.jsxs("div",{className:"flex items-start justify-between",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("h4",{className:"font-medium leading-none",children:"Subscriptions"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Active email subscriptions"})]}),t.isDevEnv]})}),e.jsx("div",{className:"grid gap-2",children:a==null?void 0:a.map(m=>e.jsxs("div",{className:"group grid grid-cols-5 gap-3",children:[e.jsx("div",{className:"col-span-3 text-sm",children:e.jsxs("div",{className:"flex items-center justify-between gap-2",children:[e.jsxs("div",{className:"boder-border rounded-md border bg-muted px-2 py-1",children:[t.titleCase(m.frequency)," "]}),e.jsx(t.Label,{children:m.time})]})}),e.jsxs("div",{className:"col-span-2 flex items-center justify-end gap-4",children:[e.jsx(Pt,{tooltip:`Next Run: ${new Date(m.nextRun).toLocaleString()} ${m.timezone}`,children:e.jsx(en,{className:"size-3.5"})}),e.jsx(Pt,{tooltip:`${m.endUserEmail}`,children:e.jsx(Cn,{className:"size-3.5"})}),l===m.id?e.jsx(ce,{className:"size-3.5 animate-spin"}):e.jsx(t.Trash2,{onClick:()=>r(m.id),className:"size-3.5 cursor-pointer"})]})]},m.id))})]})]})]})})]})})}const Pt=({children:s,tooltip:a})=>e.jsx(t.TooltipProvider,{delayDuration:0,children:e.jsxs(t.Tooltip,{children:[e.jsx(t.TooltipTrigger,{asChild:!0,children:s}),e.jsx(t.TooltipContent,{children:a})]})});function Vn(){const[s,a]=L.useState("idle"),[n,r]=L.useState(""),[o,i]=L.useState(null),l=t.useDashboardStore(T=>T.authToken),u=t.useDashboardStore(T=>T.dashboard.id),c=t.useDashboardStore(T=>T.dashboard.title),{endUserId:h}=t.getTenantAndEndUserId((l==null?void 0:l.accessToken)||""),d="https://semaphor.cloud/api/v1/subscriptions/get",x={dashboardId:u,endUserId:h},{data:g,isLoading:p,isError:j,isFetching:m,refetch:v}=t.useQuery({queryKey:["subscriptions",u,h],queryFn:()=>t.getRequest(d,(l==null?void 0:l.accessToken)||"",x),enabled:!!(l!=null&&l.accessToken)&&!!u&&!!h});async function f({email:T,subject:E,frequency:y,time:C}){const{authToken:b,...w}=ts(),I=Intl.DateTimeFormat().resolvedOptions().timeZone,D={email:T,frequency:y,time:C,timezone:I,endUserId:h,endUserEmail:T,subject:E||c||"Scheduled Dashboard",dashboardId:u,dashboardParams:w};a("loading");const S="https://semaphor.cloud/api/v1/subscriptions/create";try{const F=await t.postRequestRegular(S,(b==null?void 0:b.accessToken)||"",{...D});a("success"),r(""),v(),setTimeout(()=>{a("idle")},2e3)}catch(F){console.error(F),a("error"),r(F.message)}}async function k(T){const E="https://semaphor.cloud/api/v1/subscriptions/delete";i(T);try{await t.postRequestRegular(E,(l==null?void 0:l.accessToken)||"",{id:T}),v(),setTimeout(()=>{i(null)},2e3)}catch(y){console.error(y),i(null),a("error"),r(y.message)}}return{data:g,isLoading:p,isFetching:m,isError:j,isDeleting:o,refetch:v,handleScheduleSubscription:f,handleDeleteSubscription:k,status:s,error:n}}function qn(){const{authToken:s}=t.useSemaphorContext();t.useDashboardStore(D=>D.theme);const a=t.useDashboardStore(D=>D.lenses),n=t.useDashboardStore(D=>D.selectedLensId),r=a==null?void 0:a.find(D=>D.id===n),o=t.useDashboardStore(D=>D.dashboard);t.useDashboardStore(D=>D.selectedSheetId);const i=t.useDashboardStore(D=>D.isDashboardEditing),l=t.useEditorStore(D=>D.selectedConnectionId),[u,c]=L.useState(!1),h=t.useDashboardStore(D=>D.filterValues),d=t.useDashboardStore(D=>D.isVisualEditing),x=t.useDashboardStore(D=>D.showFilters);t.useDashboardStore(D=>D.showDashboardJSON);const g=t.useDashboardStore(D=>D.onSaveFunction),p=t.useDashboardStore(D=>D.actions.setShowFilters),{addFrame:j,setSelectedFrameId:m}=t.useDashboardStore(D=>D.actions),{getSelectedSheet:v,setIsDashboardEditing:f,setShowDashboardJSON:k}=t.useDashboardStore(D=>D.actions),T=v(),{tenantId:E,endUserId:y}=L.useMemo(()=>t.getTenantAndEndUserId(s==null?void 0:s.accessToken),[s==null?void 0:s.accessToken]),C=L.useMemo(()=>E||y,[E,y]),b=()=>{if(!i||!T)return;const D={connectionId:l,id:t.v4(),title:"Title",preferences:{onClickFilter:[{columnIndex:0,expression:""}]},type:"bar",sql:"",data:[]},S={id:t.v4(),cards:[D],activeCardId:D.id};j(T.id,S),m(S.id)},w=()=>{f(!0)};async function I(){if(c(!0),!n||!r)return;const D=Ct(o),S=JSON.stringify(D),F={...r,template:S,filterValues:h};await ot(F,s==null?void 0:s.accessToken),t.ue.success("Lens saved"),g==null||g(D),c(!1)}return d?null:e.jsxs("div",{className:"flex items-center justify-between gap-2 border-b py-1 pl-3 pr-2",role:"dashboard-controls",children:[!d&&e.jsx("div",{className:"flex items-center gap-3",children:e.jsx(t.Label,{children:r==null?void 0:r.name})}),e.jsxs("div",{className:"flex gap-2",children:[!1,!d&&e.jsx(Pn,{}),!d&&e.jsx(ns,{}),i&&!d&&C&&e.jsx(Bn,{}),!d&&C&&e.jsx(Rn,{}),i&&!d&&e.jsxs(t.Button,{variant:"secondary",className:"h-7 p-2",size:"sm",disabled:!i,onClick:b,children:[e.jsx(t.Plus,{className:"mr-2 h-4 w-4"}),"Frame"]}),!i&&e.jsx(t.Button,{variant:"ghost",className:"h-7 p-2",onClick:w,size:"sm",children:e.jsx(t.Pencil,{className:"h-4 w-4"})}),i&&!d&&e.jsx(t.IconButton,{className:"h-7 p-2",onClick:()=>{f(!1),k(!1)},children:e.jsx(t.Cross2Icon,{className:"h-4 w-4"})}),i&&!d&&C&&e.jsx(t.Button,{className:"h-7 p-2",variant:"ghost",disabled:u,onClick:I,size:"sm",children:e.jsx(Tn,{className:"h-4 w-4"})}),!x&&!d&&i&&e.jsx(t.Button,{className:"h-7 p-2",variant:"ghost",onClick:()=>p(!0),size:"sm",children:e.jsx(ft,{className:"h-4 w-4"})}),!d&&i&&!1,!d&&!1]})]})}function _n({name:s,items:a,value:n,onChange:r,isLoading:o,isError:i,refetch:l,className:u,icon:c,showNone:h=!1}){const d=re.useRef(null),[x,g]=re.useState(!1),[p,j]=re.useState("100%"),m=h?[...a||[],{id:"none",name:"None"}]:a;re.useLayoutEffect(()=>{d.current&&j(d.current.offsetWidth)},[]);function v(){var k;return o?"Loading...":i?"Error":n?(k=a==null?void 0:a.find(T=>T.id===n))==null?void 0:k.name:`Select ${s}`}function f(k,T){r(T),g(!1)}return e.jsxs(t.Popover,{open:x,onOpenChange:g,children:[e.jsx(t.PopoverTrigger,{asChild:!0,children:e.jsxs(t.Button,{ref:d,variant:"outline",role:"combobox","aria-expanded":x,className:t.cn("flex h-9 w-full justify-between",u),children:[e.jsxs("div",{className:"flex items-center gap-2",children:[c&&e.jsx("div",{className:"mr-2",children:c}),e.jsx("span",{className:"max-w-[150px] overflow-hidden truncate text-ellipsis whitespace-nowrap",children:v()})]}),e.jsx(t.ChevronDown,{className:"size-4 opacity-50"})]})}),e.jsx(t.PopoverContent,{className:"w-full p-0",style:{minWidth:p},children:e.jsxs(t.Command,{children:[e.jsxs("div",{className:"flex h-9 items-center justify-between border-b pr-1",children:[e.jsx(t.CommandInput,{className:"h-9",placeholder:"Search..."}),e.jsx(t.IconButton,{className:"h-7",onClick:l,children:e.jsx(Kt,{className:t.cn("size-3.5 opacity-50",o&&"animate-spin")})})]}),e.jsxs(t.CommandList,{children:[e.jsxs(t.CommandEmpty,{children:["No ",s==null?void 0:s.toLowerCase()," found."]}),e.jsx(t.CommandGroup,{children:m==null?void 0:m.map(k=>e.jsx(t.CommandItem,{className:"overflow-x-auto",value:k.name,onSelect:T=>f(T,k.id),children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(t.Check,{className:t.cn("size-4",n===k.id?"opacity-100":"opacity-0")}),e.jsx("div",{title:k.name,children:k.name})]})},k.id))})]})]})})]})}function ve({title:s,items:a,value:n,isLoading:r,isError:o,refetch:i,onChange:l,showNone:u=!1,className:c,disabled:h=!1,type:d="combobox"}){const x=r?"Loading...":o?"Error":n;function g(){if(s==="Connection")return e.jsx(In,{className:"size-4"});if(s==="Database")return e.jsx(t.Database,{className:"size-4"});if(s==="Schema")return e.jsx(kn,{className:"size-4"});if(s==="Table")return e.jsx(He,{className:"size-4"});if(s==="Model")return e.jsx(Ks,{className:"size-4"})}function p(){return e.jsxs(t.Select,{disabled:h,value:x,onValueChange:l,children:[e.jsx(t.SelectTrigger,{className:t.cn("h-9 w-full border-muted focus:ring-1 focus:ring-offset-0",c),children:e.jsx(t.SelectValue,{placeholder:r?"Loading...":o?"Error":`Select ${s}`})}),e.jsx(t.SelectContent,{children:e.jsxs(t.SelectGroup,{children:[r?e.jsx(t.SelectLabel,{children:"Loading..."}):e.jsxs(t.SelectLabel,{className:"flex items-center justify-between gap-2",children:[s,e.jsx(Kt,{className:t.cn("mr-1 size-3 cursor-pointer text-muted-foreground hover:text-foreground",r&&"animate-spin"),onClick:i})]}),e.jsxs(e.Fragment,{children:[u&&e.jsx(t.SelectItem,{value:"none",children:"None"},"none"),a&&a.map((m,v)=>e.jsx(t.SelectItem,{value:m.id,children:m.name},v))]}),(r||o)&&e.jsx(t.SelectItem,{value:x,children:x})]})})]})}function j(){return e.jsx(_n,{icon:g(),items:a,value:x,onChange:l||(()=>{}),isLoading:r,isError:o,refetch:i,name:s,showNone:u})}return e.jsx(t.TooltipProvider$1,{delayDuration:0,children:e.jsx("div",{className:"flex items-center gap-2",children:d==="select"?p():j()})})}function Gn({column:s}){const[a,n]=L.useState(!1),r=t.useEditorStore(m=>m.selectedTableName),o=t.useEditorStore(m=>m.selectedSchemaName),i=t.useEditorStore(m=>m.selectedDatabaseName),l=t.useEditorStore(m=>m.selectedDatamodelId),u=t.useEditorStore(m=>m.sqlGen),{modelItems:c,connectionType:h}=t.useEditorAside(),d=t.useEditorStore(m=>m.actions.setSqlGen),x=t.useEditorStore(m=>m.actions.setCardSql),g=t.useEditorStore(m=>m.actions.setRunSql);function p(m,v){var E;const f=t.createSqlGenConfig(m,u,v),k=(E=c==null?void 0:c.find(y=>y.id===l))==null?void 0:E.name;d(f);const T=t.getSql(f,t.fmt(o||""),t.fmt(r),k||"",h,i||"");T&&(x(T),g(!0)),n(!1)}function j(){return e.jsxs("ul",{className:"text-xs",children:[e.jsx("li",{onClick:()=>p(s,"COUNT"),className:"cursor-pointer p-1 px-3 transition-colors hover:bg-muted",children:"Count"}),e.jsx("li",{onClick:()=>p(s,"AVG"),className:"cursor-pointer p-1 px-3 transition-colors hover:bg-muted",children:"Avg"}),e.jsx("li",{onClick:()=>p(s,"SUM"),className:"cursor-pointer p-1 px-3 transition-colors hover:bg-muted",children:"Sum"}),e.jsx("li",{onClick:()=>p(s,"MAX"),className:"cursor-pointer p-1 px-3 transition-colors hover:bg-muted",children:"Max"}),e.jsx("li",{onClick:()=>p(s,"MIN"),className:"cursor-pointer p-1 px-3 transition-colors hover:bg-muted",children:"Min"})]})}return e.jsxs(t.HoverCard,{openDelay:0,open:a,onOpenChange:n,children:[e.jsx(t.HoverCardTrigger,{asChild:!0,children:e.jsx(t.EllipsisVertical,{className:"invisible size-3.5 cursor-pointer text-foreground/40 group-hover:visible"})}),e.jsx(t.HoverCardContent,{align:"center",className:"w-20 rounded-md p-0 shadow-none",children:j()})]})}function Nt({column:s}){return e.jsxs(t.Tooltip,{children:[e.jsx(t.TooltipTrigger$1,{className:"shrink-0",asChild:!0,children:t.isTextDataType(s.data_type)?e.jsx(t.TextIcon,{className:"size-3 text-muted-foreground"}):t.isNumberDataType(s.data_type)?e.jsx(gt,{className:"size-3 text-muted-foreground"}):t.isDateDataType(s.data_type)?e.jsx(t.Calendar,{className:"size-3 text-muted-foreground"}):t.isBooleanDataType(s.data_type)?e.jsx(Xt,{className:"size-3 text-muted-foreground"}):s.data_type.startsWith("json")?e.jsx(Qs,{className:"size-3 text-muted-foreground"}):e.jsx(t.QuestionMarkIcon,{className:"size-3 text-muted-foreground"})}),e.jsx(t.TooltipContent,{children:e.jsx("p",{className:"text-xs",children:s.data_type})})]})}function Ue({column:s,className:a}){return e.jsxs("div",{className:t.cn("flex min-h-8 grow items-center space-x-2 px-2 text-sm",a),children:[e.jsx(Nt,{column:s}),e.jsx("span",{children:s.column_name})]})}function $n({id:s,children:a,type:n="column"}){const{attributes:r,listeners:o,setNodeRef:i,transform:l}=t.useDraggable({id:s,data:{type:n}}),u=l?{translate:t.CSS.Translate.toString(l),transform:t.CSS.Transform.toString(l)}:void 0;return e.jsx("div",{ref:i,style:u,...o,...r,children:a})}function Hn({onClick:s,column:a}){const n=t.useDndStore(d=>d.activeId),r=t.useEditorStore(d=>{var x;return(x=d.card.queryConfig)==null?void 0:x.groupByColumns}),o=t.useEditorStore(d=>{var x;return(x=d.card.queryConfig)==null?void 0:x.metricColumns}),i=t.useEditorStore(d=>{var x;return(x=d.card.queryConfig)==null?void 0:x.filterColumns});function l(d){return u(d)||c(d)||h(d)}function u(d){return r==null?void 0:r.some(x=>x.name===d.column_name)}function c(d){return o==null?void 0:o.some(x=>x.name===d.column_name)}function h(d){return i==null?void 0:i.some(x=>x.name===d.column_name)}return e.jsxs("div",{onClick:()=>s(a),className:t.cn("relative grow overflow-x-hidden overflow-y-hidden rounded-md active:border active:border-dashed active:border-muted-foreground",{"bg-muted-foreground/10":l(a)}),children:[n===a.column_name&&e.jsx(t.DragOverlay,{children:e.jsx(Ue,{column:a,className:"rounded-md border-2 border-dashed border-muted-foreground bg-muted shadow-md"})}),n===a.column_name&&e.jsx("div",{className:"absolute inset-0",children:n===a.column_name&&e.jsx(Ue,{column:a})}),e.jsx($n,{id:a.column_name,type:"column",children:e.jsx(Ue,{column:a})})]})}const Ye={id:"__placeholder__",role:"groupby",type:"string"},Ke={id:"__placeholder__",role:"groupby",type:"string"},Qe={id:"__placeholder__",role:"metric",type:"number"},We={name:"Filter",id:"__placeholder__",role:"filter",type:"string"},as={id:"__placeholder__"},fe="droppable-group-by",ge="droppable-metrics",je="droppable-pivot-by",ze="sortable-group-by",it="sortable-metrics",Be="sortable-pivot-by",lt="droppable-filters",ct="sortable-filters",St="sortable-order-by";function Jn(){const s=t.useDndStore(A=>A.insertIndex),a=t.useEditorStore(A=>{var N;return((N=A.card.queryConfig)==null?void 0:N.groupByColumns)||[]}),n=t.useEditorStore(A=>{var N;return((N=A.card.queryConfig)==null?void 0:N.metricColumns)||[]}),r=t.useEditorStore(A=>{var N;return((N=A.card.queryConfig)==null?void 0:N.filterColumns)||[]}),o=t.useEditorStore(A=>{var N;return((N=A.card.queryConfig)==null?void 0:N.orderBy)||[]}),i=t.useEditorStore(A=>{var N;return((N=A.card.queryConfig)==null?void 0:N.pivotColumns)||[]}),{setInsertIndex:l,setActiveId:u,setPreviewGroupByColumns:c,setPreviewMetricColumns:h,setPreviewFilterColumns:d,setPreviewPivotColumns:x}=t.useDndActions(),{setGroupByColumns:g,setMetricColumns:p,setFilterColumns:j,setOrderByColumns:m,setPivotByColumns:v}=t.useEditorActions(),{columns:f}=t.useDataColumns(),{formattedTableName:k}=t.useEditorAside(),T=t.useSensors(t.useSensor(t.MouseSensor,{activationConstraint:{distance:1}})),E=A=>{var z;const{active:N,over:M}=A;if(((z=N.data.current)==null?void 0:z.type)==="column"&&M){const R=M.id,q=f.find($=>$.column_name===N.id);if(!q)return;const B=t.isNumberDataType(q.data_type),K=t.isTextDataType(q.data_type),J=t.isDateDataType(q.data_type);if(K||J){if(a.some(Y=>Y.id===R)){const Y=a.findIndex(V=>V.id===R);l(Y);const _=a.filter(V=>V.id!==Ye.id);_.splice(Y,0,Ye),c(_)}else if(M.id===fe){l(a.length);const Y=[...a,Ye];c(Y),h(n),d(r)}}if(K||J){if(i.some(Y=>Y.id===R)){const Y=i.findIndex(V=>V.id===R);l(Y);const _=i.filter(V=>V.id!==Ke.id);_.splice(Y,0,Ke),x(_)}else if(M.id===je){l(i.length);const Y=[...i,Ke];x(Y),c(a),h(n),d(r)}}if(B||K){if(n.some(Y=>Y.id===R)){console.log("isOverMetricSortable",R);const Y=n.findIndex(V=>V.id===R);l(Y);const _=n.filter(V=>V.id!==Qe.id);_.splice(Y,0,Qe),h(_)}else if(M.id===ge){l(n.length);const Y=[...n,Qe];h(Y),d(r),c(a),x(i)}}if(C(q)){if(r.some(Y=>Y.id===R)){const Y=r.findIndex(V=>V.id===R);l(Y);const _=r.filter(V=>V.id!==We.id);_.splice(Y,0,We),d(_)}else if(M.id===lt){l(r.length);const Y=[...r,We];d(Y),h(n),c(a),x(i)}}}else c(a),h(n),d(r),x(i)};function y(A){u(A.active.id),l(null),c(a),h(n),d(r),x(i)}function C(A){return!r.some(N=>N.name===A.column_name)}function b(A,N="SUM"){const{column_name:M,data_type:z}=A,R=[...n],q=s!==null?s:n.length,B={...A,id:ue(M),name:M,label:Ee(M,N),type:z,role:"metric",aggregate:N};R.splice(q,0,B),p(R)}function w(A){const N=t.isTextDataType(A.data_type);t.isDateDataType(A.data_type);const M=[...i],z=s!==null?s:i.length;let R;N?R={...A,id:ue(A.column_name),name:A.column_name,type:A.data_type,role:"groupby"}:R={...A,id:ue(A.column_name),name:A.column_name,type:A.data_type,role:"groupby",label:Se(A.column_name,"month"),granularity:"month"},M.splice(z,0,R),v(M)}function I(A){const N=t.isTextDataType(A.data_type);t.isDateDataType(A.data_type);const M=[...a],z=s!==null?s:a.length;let R;N?R={...A,id:ue(A.column_name),name:A.column_name,type:A.data_type,role:"groupby"}:R={...A,id:ue(A.column_name),name:A.column_name,type:A.data_type,role:"groupby",label:Se(A.column_name,"month"),granularity:"month"},M.splice(z,0,R),g(M)}function D(A){const N=[...r],M=s!==null?s:r.length,z={...A,id:ue(A.column_name),name:A.column_name,type:A.data_type,sql:t.getDefaultFilterSql(A.data_type,k,t.fmt(A.column_name)),role:"filter"};N.splice(M,0,z),j(N)}function S(A){var R,q,B,K,J,$,Y,_;const{active:N,over:M}=A,z=N.data.current;if((z==null?void 0:z.type)==="column"&&M){const V=f.find(Z=>Z.column_name===N.id);if(!V)return;const P=t.isNumberDataType(V.data_type),H=t.isTextDataType(V.data_type),U=t.isDateDataType(V.data_type);(H||U)&&(M.id===fe||((R=M.data.current)==null?void 0:R.type)===ze)&&I(V),(H||U)&&(M.id===je||((q=M.data.current)==null?void 0:q.type)===Be)?w(V):(P||H)&&(M.id===ge||((B=M.data.current)==null?void 0:B.type)===it)?(P&&b(V),H&&b(V,"COUNT")):C(V)&&(M.id===lt||((K=M.data.current)==null?void 0:K.type)===ct)&&D(V)}if(M&&((J=M.data.current)==null?void 0:J.type)===ze&&a.some(V=>V.id===N.id)){const V=a.findIndex(H=>H.id===N.id),P=a.findIndex(H=>H.id===M.id);if(V!==-1&&P!==-1&&V!==P){const H=[...a],[U]=H.splice(V,1);H.splice(P,0,U),g(H)}}if(M&&(($=M.data.current)==null?void 0:$.type)===Be&&i.some(V=>V.id===N.id)){const V=i.findIndex(H=>H.id===N.id),P=i.findIndex(H=>H.id===M.id);if(V!==-1&&P!==-1&&V!==P){const H=[...i],[U]=H.splice(V,1);H.splice(P,0,U),v(H)}}if(M&&((Y=M.data.current)==null?void 0:Y.type)===it&&n.some(V=>V.id===N.id)){const V=n.findIndex(H=>H.id===N.id),P=n.findIndex(H=>H.id===M.id);if(V!==-1&&P!==-1&&V!==P){const H=[...n],[U]=H.splice(V,1);H.splice(P,0,U),p(H)}}if(M&&((_=M.data.current)==null?void 0:_.type)===ct&&r.some(V=>V.id===N.id)){const V=r.findIndex(H=>H.id===N.id),P=r.findIndex(H=>H.id===M.id);if(V!==-1&&P!==-1&&V!==P){const H=[...r],[U]=H.splice(V,1);H.splice(P,0,U),j(H)}}F(A),u(null),l(null),c(a),h(n),d(r),x(i)}function F(A){const{active:N,over:M}=A,z=N.data.current;if((z==null?void 0:z.type)===St&&M){const R=o.findIndex(B=>ke(B.columnId)===N.id),q=o.findIndex(B=>ke(B.columnId)===M.id);if(R!==-1&&q!==-1&&R!==q){const B=[...o],[K]=B.splice(R,1);B.splice(q,0,K),m(B)}}}function O(){u(null),l(null),c(a),h(n),d(r)}return{handleDragOver:E,handleDragEnd:S,handleDragStart:y,handleDragCancel:O,sensors:T}}function ue(s){return`${s}-${t.v4()}`}function Ee(s,a){const n=t.toTitleCase(s.replace(/_/g," "));switch(a){case"SUM":return`Sum of ${n}`;case"COUNT":return`Count of ${n}`;case"DISTINCT":return`Unique ${n}`;case"AVG":return`Average of ${n}`;case"MIN":return`Min of ${n}`;case"MAX":return`Max of ${n}`;default:return n}}function ke(s){return`${St}-${s}`}function Se(s,a){const n=t.toTitleCase(s.replace(/_/g," "));switch(a){case"day":return`Date of ${n}`;case"week":return`Week of ${n}`;case"month":return`Month of ${n}`;case"year":return`Year of ${n}`;default:return n}}function Un({schemaName:s,tableName:a,columns:n,status:r}){const o=t.useEditorStore(N=>N.sqlGen),i=t.useEditorStore(N=>N.isDevMode),l=t.useEditorStore(N=>N.selectedConnectionId),u=t.useEditorStore(N=>{var M,z;return(z=(M=N.card)==null?void 0:M.queryConfig)==null?void 0:z.metricColumns});t.useEditorStore(N=>{var M,z;return(z=(M=N.card)==null?void 0:M.queryConfig)==null?void 0:z.groupByColumns});const c=t.useEditorStore(N=>N.selectedDatabaseName),h=t.useEditorStore(N=>N.selectedSchemaName),d=t.useEditorStore(N=>N.selectedTableName),x=t.useEditorStore(N=>N.selectedDatamodelId),g=t.useDashboardStore(N=>N.dashboard.aiScopeTables),{addToAIScope:p,removeFromAIScope:j}=t.useDashboardActions(),{addColumnToMetricColumns:m,addColumnToGroupBy:v,setCardSql:f,setRunSql:k,setSqlGen:T,setIsShowingVisual:E}=t.useEditorActions(),{connectionType:y,modelItems:C,selectedDatamodelName:b}=t.useEditorAside();function w(N){var M;if(i){const z=t.createSqlGenConfig(N,o);T(z);const R=(M=C==null?void 0:C.find(B=>B.id===x))==null?void 0:M.name,q=t.getSql(z,t.fmt(s),t.fmt(a),R||"",y,c||"");q&&(f(q),k(!0))}if(!i){if(t.isTextDataType(N.data_type))if(u&&u.length>0){const z={...N,role:"groupby",id:ue(N.column_name),name:N.column_name,type:N.data_type};v(z)}else{const z={...N,role:"metric",id:ue(N.column_name),name:N.column_name,label:Ee(N.column_name,"COUNT"),type:N.data_type,aggregate:"COUNT"};m(z)}if(t.isDateDataType(N.data_type)){const z={...N,role:"groupby",id:ue(N.column_name),name:N.column_name,type:N.data_type,label:Se(N.column_name,"month"),granularity:"month"};v(z)}if(t.isNumberDataType(N.data_type)){const z={...N,role:"metric",id:ue(N.column_name),name:N.column_name,label:Ee(N.column_name,"SUM"),type:N.data_type,aggregate:"SUM"};m(z)}}}function I(N){N===!0&&(d||b)&&l?(p({connectionId:l,connectionType:y==="S3"||y==="API"?"DuckDb":y,databaseName:c||"",schemaName:h||"",tableName:d||"",datamodelName:t.resolveDatamodelName(b),datamodelId:x||""}),t.ue.success("Added to Assistant Scope",{position:"top-right",duration:2500,richColors:!0})):(j({connectionId:l||"",connectionType:y||"",tableName:d||"",datamodelName:t.resolveDatamodelName(b),datamodelId:x||"",databaseName:c||"",schemaName:h||""}),t.ue("Removed from Assistant Scope",{position:"top-right",duration:2e3,richColors:!0}))}function D(){const N=t.getFormattedTableNameForQuery({schemaName:s,tableName:a,modelName:b,connectionType:y,databaseName:c||""});E(!1);const M=y==="MSSQL"?`SELECT top 10 * FROM ${N}`:`SELECT * FROM ${N} LIMIT 10`;f(M),k(!0)}function S(){const N=t.getFormattedTableNameForQuery({schemaName:s,tableName:a,modelName:b,connectionType:y,databaseName:c||""});f(`SELECT COUNT(*) as count FROM ${N}`),k(!0)}function F(){return(g==null?void 0:g.find(N=>N.connectionId===l&&(N.tableName===d||N.datamodelName===b)))!==void 0}function O(N){return i?e.jsxs("div",{className:"flex grow items-center justify-between space-x-2 rounded-md px-2 py-1",onClick:()=>w(N),children:[e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(Nt,{column:N}),e.jsx("span",{children:N.column_name})]}),e.jsx("div",{onClick:M=>M.stopPropagation(),className:"flex items-center justify-end",children:t.isNumberDataType(N.data_type)&&e.jsx(Gn,{column:N})})]}):e.jsx(Hn,{onClick:w,column:N})}function A(){return e.jsxs(e.Fragment,{children:[e.jsxs(t.Toggle,{title:`${F()?"Remove from":"Add to"} Assistant Scope`,pressed:F(),onPressedChange:I,variant:"outline",className:t.cn("h-9 px-2",{}),children:[e.jsx(t.Plus,{className:t.cn("mr-1 size-4",{"text-green-400":F(),"text-muted-foreground":!F()})}),"AI"]}),e.jsxs(t.Button,{disabled:r!=="Success",onClick:D,size:"sm",variant:"secondary",className:"flex",children:[r!=="Success"?e.jsx(ce,{className:"mr-2 h-4 w-4 animate-spin text-foreground/50"}):e.jsx(vn,{className:"mr-2 size-4"}),"Top 10"]}),e.jsxs(t.Button,{disabled:r!=="Success",onClick:S,size:"sm",variant:"secondary",className:"w-full",children:[r!=="Success"?e.jsx(ce,{className:"mr-2 h-4 w-4 animate-spin text-foreground/50"}):e.jsx(Qt,{className:"mr-2 size-4"}),"Count"]})]})}return r!=="Success"?e.jsx("div",{className:"flex grow flex-col justify-start gap-2 px-6",children:Array.from({length:3}).map((N,M)=>e.jsx(t.Skeleton,{className:"h-7 w-full"},M))}):e.jsxs("div",{className:"flex grow flex-col space-y-4 px-4",children:[i&&e.jsx("div",{className:"mx-2 flex items-center justify-center gap-2",children:A()}),e.jsxs(t.CustomScrollArea,{className:"flex-1 basis-0 overflow-y-auto px-2 py-0",children:[e.jsx("div",{className:"flex flex-col gap-1",children:n==null?void 0:n.map(N=>e.jsx("div",{className:t.cn("mr-2 cursor-pointer rounded-md text-sm transition-colors hover:bg-muted"),children:O(N)},N.column_name))}),e.jsx(t.ScrollBar,{orientation:"vertical"}),e.jsx(t.ScrollBar,{orientation:"horizontal"})]})]})}function rs({ColumnsComponent:s,type:a}){var Lt;const n=t.useEditorStore(X=>X.card),r=t.useEditorStore(X=>X.selectedConnectionId),o=t.useDashboardStore(X=>X.showFilters),i=t.useEditorStore(X=>X.selectedDatamodelId),l=t.useEditorStore(X=>X.selectedDatabaseName),u=t.useEditorStore(X=>X.selectedSchemaName),c=t.useEditorStore(X=>X.selectedTableName),h=t.useEditorStore(X=>X.isDevMode),{setSelectedTableName:d,setIsDevMode:x,setCardSql:g}=t.useEditorActions(),{connectionItems:p,modelItems:j,schemaItems:m,databaseItems:v,tableItems:f,columns:k,isConnectionsLoading:T,isModelsLoading:E,isConnectionsError:y,isModelsError:C,isDatabasesLoadingOrFetching:b,isDatabaseError:w,isSchemaLoadingOrFetching:I,isSchemaError:D,isColumnsError:S,isColumnsLoadingOrFetching:F,isTableLoadingOrFetching:O,isTableError:A,connectionType:N,isNonDbConnection:M,refetchDatabases:z,refetchModels:R,refetchSchemas:q,refetchTables:B,refetchColumns:K,refetchConnections:J,handleConnectionChange:$,handleDatabaseChange:Y,handleSchemaChange:_,handleDatamodelChange:V,handleTableChange:P,tableName:H,schemaName:U}=t.useEditorAside(),{data:Z,refetch:de}=t.useDashboardCardQuery(n);function G(){return s?s(U,H,k):e.jsx(Un,{schemaName:U,tableName:H,columns:k,status:F?"Loading...":S?"Error":"Success"})}function Q(){window.location.href="mailto:support@semaphor.cloud?subject=Help&body=I need help with..."}function se(X){X===!0?Z&&(g(Z.userSql),setTimeout(()=>{de()},100)):g(""),x(X)}function me(){if(!r||N==="S3"||N==="API")return null;if(!i||i==="none")return e.jsx(ve,{refetch:z,showNone:!0,onChange:Y,isError:w,isLoading:b,items:v,value:l||"",title:"Database",type:"combobox"})}function Ds(){if(r&&!M&&(!l||l==="none")&&(!i||i==="none"))return e.jsxs("div",{className:"grid grid-cols-3",children:[e.jsx("div",{className:"col-span-1 grid place-items-center",children:e.jsx(t.Separator,{})}),e.jsx("div",{className:"col-span-1 grid place-items-center",children:e.jsx(t.Label,{children:"OR"})}),e.jsx("div",{className:"col-span-1 grid place-items-center",children:e.jsx(t.Separator,{})})]})}function Ts(){if(!r||N==="S3"||N==="API")return null;if(!l||l==="none")return e.jsx(ve,{refetch:R,showNone:!0,onChange:V,isError:C,isLoading:E,items:j,value:i||"",title:"Model",type:"combobox"})}function Ms(){return!r||N==="S3"||N==="API"||!l||l==="none"?null:e.jsx(ve,{refetch:q,onChange:_,isError:D,isLoading:I,items:m,value:u||"",title:"Schema",type:"combobox"})}function As(){function X(){let xe=!1;return N==="API"?(xe=!1,xe):((N==="S3"||r&&l&&l!=="none"&&u)&&(xe=!0),xe)}return X()?e.jsx(ve,{refetch:B,onChange:P,isError:A,items:f,isLoading:O,value:c||"",title:"Table",type:"combobox"}):null}return e.jsxs("aside",{className:t.cn("flex h-full min-w-[310px] max-w-[310px] flex-col justify-between rounded-none",{"border-r-[1px]":!o}),children:[e.jsxs("section",{className:"space-y-3 px-6 py-4",children:[e.jsx(ve,{refetch:J,items:p,title:"Connection",value:r||"",onChange:$,isLoading:T,isError:y,type:"combobox",connectionTooltip:((Lt=p==null?void 0:p.find(X=>X.id===r))==null?void 0:Lt.type)||""}),me(),Ds(),Ts(),Ms(),As()]}),G(),e.jsx("div",{className:"flex items-center justify-between gap-2 px-6 py-4",children:a==="main"&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(t.Switch,{className:"data-[state=checked]:bg-muted-foreground/80",checked:h,onCheckedChange:se,id:"dev-mode"}),e.jsx(t.Label,{htmlFor:"dev-mode",children:"Advanced Mode"})]}),e.jsxs(t.Button,{onClick:Q,size:"sm",variant:"secondary",className:"group flex items-center gap-2 rounded-full",children:[e.jsx(Sn,{className:"size-4"}),e.jsx("span",{className:"hidden group-hover:block",children:"Help"})]})]})})]})}function os({sql:s,error:a,onClick:n}){const r=t.useEditorStore(c=>c.isDevMode),{setIsDevMode:o,setCardSql:i,setRunSql:l}=t.useEditorActions();function u(){o(!r),i(s),setTimeout(()=>{l(!0)},100)}return e.jsx("div",{className:"flex justify-between space-x-4",children:e.jsxs("div",{className:"flex grow flex-col space-y-2",children:[e.jsxs(t.ScrollArea,{className:"max-h-[250px] min-h-0 overflow-scroll text-xs leading-relaxed text-foreground/80",children:[a&&a,s&&e.jsx("div",{className:"flex flex-col space-y-1.5",children:e.jsx("div",{className:"rounded-md",children:e.jsx(t.CodeHighlighter,{code:s,language:"sql"})})})]}),!r&&e.jsxs(t.Button,{onClick:u,variant:"secondary",size:"sm",children:[e.jsx(cn,{className:"mr-2 h-4 w-4"}),"View in SQL Editor"]}),a&&n&&e.jsxs(t.Button,{onClick:n,size:"sm",variant:"secondary",children:[e.jsx(pn,{className:"mr-2 h-4 w-4"}),"Debug with Assistant"]})]})})}function is({sql:s,error:a,onClick:n}){return e.jsxs(t.HoverCard,{openDelay:a?0:500,children:[e.jsx(t.HoverCardTrigger,{asChild:!0,children:e.jsx(t.Button,{variant:"link",className:"h-5 p-0",children:a?e.jsx(Ut,{className:"h-4 w-4 text-red-500"}):e.jsx(t.Info,{className:"h-4 w-4 text-muted-foreground"})})}),e.jsx(t.HoverCardContent,{align:"start",className:"w-full min-w-fit max-w-[700px]",children:e.jsx(os,{sql:s,error:a,onClick:n})})]})}var ls={color:void 0,size:void 0,className:void 0,style:void 0,attr:void 0},Vt=L.createContext&&L.createContext(ls),Yn=["attr","size","title"];function Kn(s,a){if(s==null)return{};var n=Qn(s,a),r,o;if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(s);for(o=0;o<i.length;o++)r=i[o],!(a.indexOf(r)>=0)&&Object.prototype.propertyIsEnumerable.call(s,r)&&(n[r]=s[r])}return n}function Qn(s,a){if(s==null)return{};var n={};for(var r in s)if(Object.prototype.hasOwnProperty.call(s,r)){if(a.indexOf(r)>=0)continue;n[r]=s[r]}return n}function Re(){return Re=Object.assign?Object.assign.bind():function(s){for(var a=1;a<arguments.length;a++){var n=arguments[a];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(s[r]=n[r])}return s},Re.apply(this,arguments)}function qt(s,a){var n=Object.keys(s);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(s);a&&(r=r.filter(function(o){return Object.getOwnPropertyDescriptor(s,o).enumerable})),n.push.apply(n,r)}return n}function Pe(s){for(var a=1;a<arguments.length;a++){var n=arguments[a]!=null?arguments[a]:{};a%2?qt(Object(n),!0).forEach(function(r){Wn(s,r,n[r])}):Object.getOwnPropertyDescriptors?Object.defineProperties(s,Object.getOwnPropertyDescriptors(n)):qt(Object(n)).forEach(function(r){Object.defineProperty(s,r,Object.getOwnPropertyDescriptor(n,r))})}return s}function Wn(s,a,n){return a=Xn(a),a in s?Object.defineProperty(s,a,{value:n,enumerable:!0,configurable:!0,writable:!0}):s[a]=n,s}function Xn(s){var a=Zn(s,"string");return typeof a=="symbol"?a:a+""}function Zn(s,a){if(typeof s!="object"||!s)return s;var n=s[Symbol.toPrimitive];if(n!==void 0){var r=n.call(s,a);if(typeof r!="object")return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return(a==="string"?String:Number)(s)}function cs(s){return s&&s.map((a,n)=>L.createElement(a.tag,Pe({key:n},a.attr),cs(a.child)))}function ye(s){return a=>L.createElement(ea,Re({attr:Pe({},s.attr)},a),cs(s.child))}function ea(s){var a=n=>{var{attr:r,size:o,title:i}=s,l=Kn(s,Yn),u=o||n.size||"1em",c;return n.className&&(c=n.className),s.className&&(c=(c?c+" ":"")+s.className),L.createElement("svg",Re({stroke:"currentColor",fill:"currentColor",strokeWidth:"0"},n.attr,r,l,{className:c,style:Pe(Pe({color:s.color||n.color},n.style),s.style),height:u,width:u,xmlns:"http://www.w3.org/2000/svg"}),i&&L.createElement("title",null,i),s.children)};return Vt!==void 0?L.createElement(Vt.Consumer,null,n=>a(n)):a(ls)}function ta(s){return ye({attr:{viewBox:"0 0 448 512"},child:[{tag:"path",attr:{d:"M439.8 200.5c-7.7-30.9-22.3-54.2-53.4-54.2h-40.1v47.4c0 36.8-31.2 67.8-66.8 67.8H172.7c-29.2 0-53.4 25-53.4 54.3v101.8c0 29 25.2 46 53.4 54.3 33.8 9.9 66.3 11.7 106.8 0 26.9-7.8 53.4-23.5 53.4-54.3v-40.7H226.2v-13.6h160.2c31.1 0 42.6-21.7 53.4-54.2 11.2-33.5 10.7-65.7 0-108.6zM286.2 404c11.1 0 20.1 9.1 20.1 20.3 0 11.3-9 20.4-20.1 20.4-11 0-20.1-9.2-20.1-20.4.1-11.3 9.1-20.3 20.1-20.3zM167.8 248.1h106.8c29.7 0 53.4-24.5 53.4-54.3V91.9c0-29-24.4-50.7-53.4-55.6-35.8-5.9-74.7-5.6-106.8.1-45.2 8-53.4 24.7-53.4 55.6v40.7h106.9v13.6h-147c-31.1 0-58.3 18.7-66.8 54.2-9.8 40.7-10.2 66.1 0 108.6 7.6 31.6 25.7 54.2 56.8 54.2H101v-48.8c0-35.3 30.5-66.4 66.8-66.4zm-6.7-142.6c-11.1 0-20.1-9.1-20.1-20.3.1-11.3 9-20.4 20.1-20.4 11 0 20.1 9.2 20.1 20.4s-9 20.3-20.1 20.3z"},child:[]}]})(s)}function ds(s){const{theme:a}=t.useTheme(),n=t.useDashboardStore(m=>m.theme),r=t.useEditorStore(m=>m.frame),o=t.useEditorStore(m=>m.card),i=t.useEditorStore(m=>m.activeTabCardId),l=t.Le(),{refetch:u}=t.useDashboardCardQuery(o),{updateCardTab:c,setRunSql:h}=t.useEditorActions(),d=L.useRef(),x=L.useRef({}),g=r.cards.find(m=>m.id===i),p=L.useCallback(()=>{let m="";return s==="python"&&(m=(g==null?void 0:g.python)||t.PYTHON_DEFAULT_CODE),s==="sql"&&(m=(g==null?void 0:g.sql)||""),m},[s,g]);L.useEffect(()=>{if(i&&o.id&&o.id!==i){if(d.current&&l){const m=d.current.getModel();m&&(x.current[o.id]=m);let v=x.current[i];if(!v){const f=p();v=l.editor.createModel(f,s),x.current[i]=v}d.current.setModel(v)}c(i,o),h(!0)}},[i,o,c,l,h,s,p]),L.useEffect(()=>{if(!l)return;const m=window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light",v=n==="system"?m:n;l.editor.defineTheme("default",{base:v==="light"?"vs":"vs-dark",inherit:!0,rules:[],colors:{"editor.background":v==="light"?"#ffffff":t.hslStringToHex("240 10% 10%")}}),l.editor.setTheme("default")},[a,n,l]),L.useEffect(()=>{const m=d.current;m&&l&&o&&m.addCommand(l.KeyMod.Shift|l.KeyCode.Enter,function(){return u(),!1})},[l,u,o]);function j(m,v){if(d.current=m,!x.current[o.id]){const f=m.getModel(),k=p();x.current[o.id]=f||(v==null?void 0:v.editor.createModel(k,s))||m.getModel()}m.setModel(x.current[o.id])}return{handleEditorDidMount:j}}function sa(){const s=t.useEditorStore(l=>l.card.sql),a=t.useEditorStore(l=>l.sqlGen),{setCardSql:n,setSqlGen:r}=t.useEditorActions(),{handleEditorDidMount:o}=ds("sql");function i(l){n(l),t.isObjectEmpty(a)||r({})}return e.jsxs("div",{className:"relative h-full w-full",children:[!s&&e.jsx("div",{className:"pointer-events-none absolute left-[65px] top-3 z-10 font-mono text-sm text-muted-foreground",children:"Write your SQL here"}),e.jsx(t.Ft,{theme:"default",className:"pt-3",onChange:i,value:s||"",options:{minimap:{enabled:!1},wordWrap:"on",scrollbar:{verticalScrollbarSize:0,horizontalScrollbarSize:0},fontSize:13,renderLineHighlight:"none"},defaultLanguage:"sql",onMount:o})]})}function na(){const s=t.useEditorStore(o=>o.card.python),{setCardPython:a}=t.useEditorActions(),{handleEditorDidMount:n}=ds("python");function r(o){a(o)}return e.jsx(t.Ft,{className:"pt-3",theme:"default",onChange:r,value:s||t.PYTHON_DEFAULT_CODE,options:{minimap:{enabled:!1},wordWrap:"on",scrollbar:{verticalScrollbarSize:0,horizontalScrollbarSize:0},fontSize:13,renderLineHighlight:"none"},defaultLanguage:"python",onMount:n})}function aa({jsonString:s}){return e.jsx(t.Ft,{theme:"default",defaultLanguage:"json",className:"mt-1 min-h-[220px]",value:s,options:{readOnly:!0,minimap:{enabled:!1},lineNumbers:"off",glyphMargin:!1,wordWrap:"on",scrollbar:{verticalScrollbarSize:0,horizontalScrollbarSize:0},fontSize:12,renderLineHighlight:"none"}})}function ra(){const s=t.useQueryClient(),[a,n]=L.useState(!1),[r,o]=L.useState(!1);t.useEditorStore(B=>B.frame);const i=t.useEditorStore(B=>B.card);t.useEditorStore(B=>B.isShowingVisual),t.useEditorStore(B=>B.selectedConnectionId),t.useEditorStore(B=>B.tempQueryData),t.useEditorStore(B=>B.selectedDatabaseName),t.useEditorStore(B=>B.selectedDatamodelId),t.useEditorStore(B=>B.selectedSchemaName);const l=t.useEditorStore(B=>B.showAIDialog);t.useEditorStore(B=>B.selectedTableName);const u=t.useEditorStore(B=>{var K;return(K=B==null?void 0:B.card)==null?void 0:K.sql}),c=t.useEditorStore(B=>{var K;return(K=B==null?void 0:B.card)==null?void 0:K.python}),{sheetFilterValues:h}=t.useSheetFilterValues(),{setCardCustomCfg:d,setQueryResultColumns:x,setCardSql:g,setTempQueryData:p,setPythonStdOut:j,setCardPreferences:m,setCardType:v,setCardPython:f,runAssistant:k,setShowAIDialog:T}=t.useEditorActions();t.useEditorStore(B=>B.onSave),t.useEditorStore(B=>B.onClose),t.useEditorStore(B=>B.actions.setSqlGen);const E=t.useEditorStore(B=>B.runSql),y=t.useEditorStore(B=>B.actions.setRunSql),C=t.useEditorStore(B=>B.actions.setIsSqlRunning),{data:b,isLoading:w,isFetching:I,isSuccess:D,isError:S,error:F,queryKey:O,refetch:A,errorMessage:N}=t.useDashboardCardQuery(i);L.useEffect(()=>{if(b&&(b.rowLimitExceeded&&t.ue.error("Row limit (10,000) exceeded. Refine your query.",{position:"top-center",richColors:!0}),j((b==null?void 0:b.output)||""),b.records.length>0)){const{dimensionKeys:B}=t.getKeys(b.records);x(B)}},[b,x,j]),L.useEffect(()=>{(w||I)&&C(!0),(S||D)&&C(!1),S&&(x([]),j(""))},[w,I,b,j,S,D,C,x,F]),L.useEffect(()=>{E&&(s.getQueryData(O)||A(),y(!1))},[E,A,y,O,s]);async function M(){const B=`Can you help me debug this query in the current table scope?
|
|
391
|
+
*/const Zt=t.createLucideIcon("Type",[["polyline",{points:"4 7 4 4 20 4 20 7",key:"1nosan"}],["line",{x1:"9",x2:"15",y1:"20",y2:"20",key:"swin9y"}],["line",{x1:"12",x2:"12",y1:"4",y2:"20",key:"1tx1rr"}]]);function Ct(s){var a;return{...s,sheets:(a=s==null?void 0:s.sheets)==null?void 0:a.map(n=>{var r;return{...n,frames:(r=n.frames)==null?void 0:r.map(o=>{var i;return{...o,cards:(i=o.cards)==null?void 0:i.map(l=>es(l))}})}})}}function es(s){return{...s,customCfg:On(s.customCfg),data:[]}}function On(s){var a,n;return s?{...s,data:{...s==null?void 0:s.data,labels:[],datasets:(n=(a=s==null?void 0:s.data)==null?void 0:a.datasets)==null?void 0:n.map(r=>({...r,data:[]}))}}:null}async function Fn(s,a,n,r){if(!r)return null;const o=JSON.stringify(a);return await t.postRequest("https://semaphor.cloud/api/v1/lens",r,{name:s,template:o,filterValues:n,operation:"create"})}async function ot(s,a){if(!a)return null;const{id:n,template:r,filterValues:o,isDefault:i,shared:l}=s;return await t.postRequest("https://semaphor.cloud/api/v1/lens",a,{lensId:n,template:r,filterValues:o,isDefault:i,shared:l,operation:"update"})}async function zn(s,a){return a?await t.postRequest("https://semaphor.cloud/api/v1/lens",a,{operation:"delete",lensId:s}):null}function Bn(){const[s,a]=L.useState(""),[n,r]=L.useState(!1),o=t.useDashboardStore(m=>m.lenses),{setLenses:i,setSelectedLensId:l}=t.useDashboardActions(),[u,c]=L.useState(!1),[h,d]=L.useState(!1),x=t.useDashboardStore(m=>m.dashboard),g=t.useDashboardStore(m=>m.filterValues),{authToken:p}=t.useSemaphorContext();async function j(){c(!0);const m=Ct(x),v=await Fn(s,m,g,p==null?void 0:p.accessToken);v&&(i([...o||[],v]),l(v.id),d(!1)),c(!1)}return e.jsxs(t.Dialog,{open:h,onOpenChange:d,children:[e.jsx(t.DialogTrigger,{asChild:!0,children:e.jsxs(t.Button,{variant:"secondary",className:"h-7 p-2",size:"sm",children:[e.jsx(t.Plus,{className:"mr-2 h-4 w-4"}),"Lens"]})}),e.jsxs(t.DialogContent,{className:"sm:max-w-[425px]",children:[e.jsxs(t.DialogHeader,{children:[e.jsx(t.DialogTitle,{children:"Create Lens"}),e.jsx(t.DialogDescription,{children:"Lenses are private by default and are only visible to you."})]}),e.jsx("div",{className:"grid gap-4 py-4",children:e.jsxs("div",{className:"grid grid-cols-8 items-center gap-4",children:[e.jsx(t.Label,{className:"text-right",children:"Name"}),e.jsx(t.Input,{value:s,onChange:m=>{a(m.target.value)},id:"name",className:"col-span-7 focus-visible:ring-0 focus-visible:ring-ring focus-visible:ring-offset-0"})]})}),n&&e.jsx("p",{className:"text-sm",children:"The lens name already exists. Use a differnet name."}),e.jsx(t.DialogFooter,{children:e.jsx(t.Button,{size:"sm",variant:"outline",type:"submit",onClick:j,disabled:u,children:u?"Creating...":"Add"})})]})]})}function Rn(){const[s,a]=L.useState(!1),n=t.useDashboardStore(d=>d.lenses),r=t.useDashboardStore(d=>d.selectedLensId),{setLenses:o,setSelectedLensId:i}=t.useDashboardActions(),{authToken:l}=t.useSemaphorContext();async function u(d){i(d)}async function c(d){if(await zn(d,l==null?void 0:l.accessToken)){const g=n==null?void 0:n.filter(p=>p.id!==d);o(g||[]),i("original")}}async function h(d){const x=n==null?void 0:n.map(j=>j.id===d.id?{...j,isDefault:!d.isDefault}:{...j,isDefault:!1});o(x||[]);const g={...d,isDefault:!d.isDefault};await ot(g,l==null?void 0:l.accessToken);const p=n==null?void 0:n.find(j=>j.isDefault);if(p&&p.id!==d.id){const j={...p,isDefault:!1};await ot(j,l==null?void 0:l.accessToken)}}return e.jsxs(t.DropdownMenu,{open:s,onOpenChange:a,children:[e.jsx(t.DropdownMenuTrigger,{asChild:!0,children:e.jsx(t.IconButton,{tooltip:"Lenses",className:"h-7 p-2",children:e.jsx(hn,{className:"h-4 w-4"})})}),e.jsxs(t.DropdownMenuContent,{align:"end",className:"w-60",children:[e.jsx(t.DropdownMenuLabel,{className:"px-3",children:"Lenses"}),e.jsx(t.DropdownMenuSeparator,{}),e.jsxs(t.DropdownMenuRadioGroup,{value:r,onValueChange:u,children:[e.jsx(t.DropdownMenuRadioItem,{value:"original",children:"Original"}),n==null?void 0:n.map(d=>e.jsxs(t.DropdownMenuRadioItem,{className:"group flex justify-between gap-2",value:d.id,children:[e.jsx("p",{children:d.name}),e.jsxs("span",{className:"flex items-center gap-3",children:[e.jsx(fn,{onClick:x=>{x.preventDefault(),h(d)},className:t.cn("size-3.5 text-muted-foreground hover:cursor-pointer hover:text-foreground group-hover:visible",{invisible:!d.isDefault,"text-green-500":d.isDefault}),strokeWidth:1.5}),e.jsx(t.Cross2Icon,{onClick:x=>{x.preventDefault(),c(d.id)},className:"invisible size-3.5 text-muted-foreground hover:cursor-pointer hover:text-foreground group-hover:visible",strokeWidth:1.5})]})]},d.id))]})]})]})}function ts(){var i;const s=t.useDashboardStore.getState().authToken,a=t.useDashboardStore.getState().theme,n=t.useDashboardStore.getState().filterValues,r=(i=t.useDashboardStore.getState().dashboard)==null?void 0:i.title,o=t.useDashboardStore.getState().selectedSheetId;return{authToken:s,theme:a||"light",filterValues:n,dashboardTitle:r||"",selectedSheetId:o||""}}async function ss(){const s=t.useDashboardStore.getState().isDownloadingPdf,{authToken:a,theme:n,filterValues:r,dashboardTitle:o,selectedSheetId:i}=ts();if(s)return;const l=new URLSearchParams;l.set("token",(a==null?void 0:a.accessToken)||""),l.set("theme",n),l.set("filterValues",JSON.stringify(r)),l.set("selectedSheetId",i||"");const u=`https://semaphor.cloud/api/v1/generate-pdf?params=${encodeURIComponent(l.toString())}`;t.useDashboardStore.setState({isDownloadingPdf:!0});const d=(await(await fetch(u,{method:"GET",headers:{Authorization:`Bearer ${a==null?void 0:a.accessToken}`}})).json()).url,x=document.createElement("a");x.href=d;const g=`${o}-${new Date().toISOString()}.pdf`;x.download=g,x.click(),t.useDashboardStore.setState({isDownloadingPdf:!1})}function Pn({iconClassName:s}){const{authToken:a}=t.useSemaphorContext();t.useDashboardStore(o=>{var i;return(i=o.dashboard)==null?void 0:i.title}),t.useDashboardStore(o=>o.theme),t.useDashboardStore(o=>o.filterValues);const n=t.useDashboardStore(o=>o.isDownloadingPdf);async function r(){await ss()}return e.jsx(t.IconButton,{tooltip:"Download PDF",className:"h-7 p-2",onClick:r,children:n?e.jsx(ce,{className:t.cn("size-3.5 animate-spin",s)}):e.jsx(t.Download,{className:t.cn("size-4",s)})})}function le({defaultValue:s,label:a,options:n,value:r,onValueChange:o,className:i,icon:l,showPlaceholderLabel:u=!0}){return e.jsxs(t.Select,{defaultValue:s,value:r,onValueChange:o,children:[e.jsx(t.SelectTrigger,{className:t.cn("w-full focus:ring-0 focus:ring-offset-0",i),children:e.jsxs("div",{className:"flex items-center gap-2",children:[l&&e.jsx("span",{children:l}),u&&e.jsx(t.SelectValue,{placeholder:a})]})}),e.jsx(t.SelectContent,{children:e.jsxs(t.SelectGroup,{children:[e.jsx(t.SelectLabel,{children:a}),n.map(c=>e.jsx(t.SelectItem,{value:c.value,children:c.label},c.value))]})})]})}const Rt=[{value:"daily",label:"Daily"},{value:"weekly",label:"Weekly"},{value:"biweekly",label:"Bi-Weekly"},{value:"monthly",label:"Monthly"}];function ns({triggerButtonClassName:s}){const{data:a,handleScheduleSubscription:n,handleDeleteSubscription:r,status:o,error:i,isDeleting:l}=Vn(),[u,c]=L.useState(""),[h,d]=L.useState(Rt[0].value),[x,g]=L.useState("06:00"),[p,j]=L.useState("");return e.jsx(t.TooltipProvider,{delayDuration:0,children:e.jsxs(t.Popover,{children:[e.jsx(t.PopoverTrigger,{asChild:!0,children:e.jsx(t.IconButton,{tooltip:"Schedule Dashboard",className:t.cn("h-7 p-2",s),children:e.jsx(tn,{className:"size-4"})})}),e.jsx(t.PopoverContent,{align:"end",className:"w-80",children:e.jsxs("div",{className:"grid gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("h4",{className:"font-medium leading-none",children:"Schedule Subscription"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Subscribe to receive dashboard via email"})]}),e.jsxs("div",{className:"grid gap-2",children:[e.jsxs("div",{className:"grid grid-cols-3 items-center gap-4",children:[e.jsx(t.Label,{htmlFor:"email",children:"Email"}),e.jsx(t.Input,{id:"email",value:u,onChange:m=>c(m.target.value),className:`col-span-2 h-8 ${t.removeRing}`})]}),e.jsxs("div",{className:"grid grid-cols-3 items-center gap-4",children:[e.jsx(t.Label,{htmlFor:"subject",children:"Subject"}),e.jsx(t.Input,{id:"subject",placeholder:"Monthly Sales Report",value:p,onChange:m=>{j(m.target.value.replace(/[^a-zA-Z0-9\s-_]/g,""))},className:`col-span-2 h-8 ${t.removeRing}`})]}),e.jsxs("div",{className:"grid grid-cols-3 items-center gap-4",children:[e.jsx(t.Label,{htmlFor:"frequency",children:"Frequency"}),e.jsx(le,{className:"col-span-2 h-8",label:"Select Frequency",options:Rt,value:h,onValueChange:m=>d(m)})]}),e.jsxs("div",{className:"grid grid-cols-3 items-center gap-4",children:[e.jsx(t.Label,{htmlFor:"time",children:"Time"}),e.jsx(t.Input,{id:"time",type:"time",value:x,onChange:m=>g(m.target.value),className:`col-span-2 h-8 ${t.removeRing}`})]}),e.jsx("div",{className:"mt-2 flex justify-end",children:e.jsxs(t.Button,{onClick:async()=>{await n({email:u,frequency:h,time:x,subject:p}),g("00:00"),c(""),j("")},className:"h-8 p-2",disabled:o==="loading"||!u,children:[o==="loading"&&e.jsx(ce,{className:"mr-2 size-4 animate-spin"}),o==="success"&&e.jsx(t.Check,{className:"mr-2 size-4"}),o==="error"&&e.jsxs(t.Tooltip,{children:[e.jsx(t.TooltipTrigger,{children:e.jsx(Ut,{className:"mr-2 size-4 text-destructive"})}),e.jsx(t.TooltipContent,{children:e.jsx("p",{children:i})})]}),"Schedule"]})}),a&&a.length>0&&e.jsxs("div",{className:"space-y-4",children:[e.jsx(t.Separator,{className:"my-2"}),e.jsx("div",{className:"space-y-2",children:e.jsxs("div",{className:"flex items-start justify-between",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("h4",{className:"font-medium leading-none",children:"Subscriptions"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Active email subscriptions"})]}),t.isDevEnv]})}),e.jsx("div",{className:"grid gap-2",children:a==null?void 0:a.map(m=>e.jsxs("div",{className:"group grid grid-cols-5 gap-3",children:[e.jsx("div",{className:"col-span-3 text-sm",children:e.jsxs("div",{className:"flex items-center justify-between gap-2",children:[e.jsxs("div",{className:"boder-border rounded-md border bg-muted px-2 py-1",children:[t.titleCase(m.frequency)," "]}),e.jsx(t.Label,{children:m.time})]})}),e.jsxs("div",{className:"col-span-2 flex items-center justify-end gap-4",children:[e.jsx(Pt,{tooltip:`Next Run: ${new Date(m.nextRun).toLocaleString()} ${m.timezone}`,children:e.jsx(en,{className:"size-3.5"})}),e.jsx(Pt,{tooltip:`${m.endUserEmail}`,children:e.jsx(Cn,{className:"size-3.5"})}),l===m.id?e.jsx(ce,{className:"size-3.5 animate-spin"}):e.jsx(t.Trash2,{onClick:()=>r(m.id),className:"size-3.5 cursor-pointer"})]})]},m.id))})]})]})]})})]})})}const Pt=({children:s,tooltip:a})=>e.jsx(t.TooltipProvider,{delayDuration:0,children:e.jsxs(t.Tooltip,{children:[e.jsx(t.TooltipTrigger,{asChild:!0,children:s}),e.jsx(t.TooltipContent,{children:a})]})});function Vn(){const[s,a]=L.useState("idle"),[n,r]=L.useState(""),[o,i]=L.useState(null),l=t.useDashboardStore(T=>T.authToken),u=t.useDashboardStore(T=>T.dashboard.id),c=t.useDashboardStore(T=>T.dashboard.title),{endUserId:h}=t.getTenantAndEndUserId((l==null?void 0:l.accessToken)||""),d="https://semaphor.cloud/api/v1/subscriptions/get",x={dashboardId:u,endUserId:h},{data:g,isLoading:p,isError:j,isFetching:m,refetch:v}=t.useQuery({queryKey:["subscriptions",u,h],queryFn:()=>t.getRequest(d,(l==null?void 0:l.accessToken)||"",x),enabled:!!(l!=null&&l.accessToken)&&!!u&&!!h});async function f({email:T,subject:E,frequency:y,time:C}){const{authToken:b,...w}=ts(),I=Intl.DateTimeFormat().resolvedOptions().timeZone,D={email:T,frequency:y,time:C,timezone:I,endUserId:h,endUserEmail:T,subject:E||c||"Scheduled Dashboard",dashboardId:u,dashboardParams:w};a("loading");const S="https://semaphor.cloud/api/v1/subscriptions/create";try{const F=await t.postRequestRegular(S,(b==null?void 0:b.accessToken)||"",{...D});a("success"),r(""),v(),setTimeout(()=>{a("idle")},2e3)}catch(F){console.error(F),a("error"),r(F.message)}}async function k(T){const E="https://semaphor.cloud/api/v1/subscriptions/delete";i(T);try{await t.postRequestRegular(E,(l==null?void 0:l.accessToken)||"",{id:T}),v(),setTimeout(()=>{i(null)},2e3)}catch(y){console.error(y),i(null),a("error"),r(y.message)}}return{data:g,isLoading:p,isFetching:m,isError:j,isDeleting:o,refetch:v,handleScheduleSubscription:f,handleDeleteSubscription:k,status:s,error:n}}function qn(){const{authToken:s}=t.useSemaphorContext();t.useDashboardStore(D=>D.theme);const a=t.useDashboardStore(D=>D.lenses),n=t.useDashboardStore(D=>D.selectedLensId),r=a==null?void 0:a.find(D=>D.id===n),o=t.useDashboardStore(D=>D.dashboard);t.useDashboardStore(D=>D.selectedSheetId);const i=t.useDashboardStore(D=>D.isDashboardEditing),l=t.useEditorStore(D=>D.selectedConnectionId),[u,c]=L.useState(!1),h=t.useDashboardStore(D=>D.filterValues),d=t.useDashboardStore(D=>D.isVisualEditing),x=t.useDashboardStore(D=>D.showFilters);t.useDashboardStore(D=>D.showDashboardJSON);const g=t.useDashboardStore(D=>D.onSaveFunction),p=t.useDashboardStore(D=>D.actions.setShowFilters),{addFrame:j,setSelectedFrameId:m}=t.useDashboardStore(D=>D.actions),{getSelectedSheet:v,setIsDashboardEditing:f,setShowDashboardJSON:k}=t.useDashboardStore(D=>D.actions),T=v(),{tenantId:E,endUserId:y}=L.useMemo(()=>t.getTenantAndEndUserId(s==null?void 0:s.accessToken),[s==null?void 0:s.accessToken]),C=L.useMemo(()=>E||y,[E,y]),b=()=>{if(!i||!T)return;const D={connectionId:l,id:t.v4(),title:"Title",preferences:{onClickFilter:[{columnIndex:0,expression:""}]},type:"bar",sql:"",data:[]},S={id:t.v4(),cards:[D],activeCardId:D.id};j(T.id,S),m(S.id)},w=()=>{f(!0)};async function I(){if(c(!0),!n||!r)return;const D=Ct(o),S=JSON.stringify(D),F={...r,template:S,filterValues:h};await ot(F,s==null?void 0:s.accessToken),t.ue.success("Lens saved"),g==null||g(D),c(!1)}return d?null:e.jsxs("div",{className:"flex items-center justify-between gap-2 border-b py-1 pl-3 pr-2",role:"dashboard-controls",children:[!d&&e.jsx("div",{className:"flex items-center gap-3",children:e.jsx(t.Label,{children:r==null?void 0:r.name})}),e.jsxs("div",{className:"flex gap-2",children:[!1,!d&&e.jsx(Pn,{}),!d&&e.jsx(ns,{}),i&&!d&&C&&e.jsx(Bn,{}),!d&&C&&e.jsx(Rn,{}),i&&!d&&e.jsxs(t.Button,{variant:"secondary",className:"h-7 p-2",size:"sm",disabled:!i,onClick:b,children:[e.jsx(t.Plus,{className:"mr-2 h-4 w-4"}),"Frame"]}),!i&&e.jsx(t.Button,{variant:"ghost",className:"h-7 p-2",onClick:w,size:"sm",children:e.jsx(t.Pencil,{className:"h-4 w-4"})}),i&&!d&&e.jsx(t.IconButton,{className:"h-7 p-2",onClick:()=>{f(!1),k(!1)},children:e.jsx(t.Cross2Icon,{className:"h-4 w-4"})}),i&&!d&&C&&e.jsx(t.Button,{className:"h-7 p-2",variant:"ghost",disabled:u,onClick:I,size:"sm",children:e.jsx(Tn,{className:"h-4 w-4"})}),!x&&!d&&i&&e.jsx(t.Button,{className:"h-7 p-2",variant:"ghost",onClick:()=>p(!0),size:"sm",children:e.jsx(ft,{className:"h-4 w-4"})}),!d&&i&&!1,!d&&!1]})]})}function _n({name:s,items:a,value:n,onChange:r,isLoading:o,isError:i,refetch:l,className:u,icon:c,showNone:h=!1}){const d=re.useRef(null),[x,g]=re.useState(!1),[p,j]=re.useState("100%"),m=h?[...a||[],{id:"none",name:"None"}]:a;re.useLayoutEffect(()=>{d.current&&j(d.current.offsetWidth)},[]);function v(){var k;return o?"Loading...":i?"Error":n?(k=a==null?void 0:a.find(T=>T.id===n))==null?void 0:k.name:`Select ${s}`}function f(k,T){r(T),g(!1)}return e.jsxs(t.Popover,{open:x,onOpenChange:g,children:[e.jsx(t.PopoverTrigger,{asChild:!0,children:e.jsxs(t.Button,{ref:d,variant:"outline",role:"combobox","aria-expanded":x,className:t.cn("flex h-9 w-full justify-between",u),children:[e.jsxs("div",{className:"flex items-center gap-2",children:[c&&e.jsx("div",{className:"mr-2",children:c}),e.jsx("span",{className:"max-w-[150px] overflow-hidden truncate text-ellipsis whitespace-nowrap",children:v()})]}),e.jsx(t.ChevronDown,{className:"size-4 opacity-50"})]})}),e.jsx(t.PopoverContent,{className:"w-full p-0",style:{minWidth:p},children:e.jsxs(t.Command,{children:[e.jsxs("div",{className:"flex h-9 items-center justify-between border-b pr-1",children:[e.jsx(t.CommandInput,{className:"h-9",placeholder:"Search..."}),e.jsx(t.IconButton,{className:"h-7",onClick:l,children:e.jsx(Kt,{className:t.cn("size-3.5 opacity-50",o&&"animate-spin")})})]}),e.jsxs(t.CommandList,{children:[e.jsxs(t.CommandEmpty,{children:["No ",s==null?void 0:s.toLowerCase()," found."]}),e.jsx(t.CommandGroup,{children:m==null?void 0:m.map(k=>e.jsx(t.CommandItem,{className:"overflow-x-auto",value:k.name,onSelect:T=>f(T,k.id),children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(t.Check,{className:t.cn("size-4",n===k.id?"opacity-100":"opacity-0")}),e.jsx("div",{title:k.name,children:k.name})]})},k.id))})]})]})})]})}function ve({title:s,items:a,value:n,isLoading:r,isError:o,refetch:i,onChange:l,showNone:u=!1,className:c,disabled:h=!1,type:d="combobox"}){const x=r?"Loading...":o?"Error":n;function g(){if(s==="Connection")return e.jsx(In,{className:"size-4"});if(s==="Database")return e.jsx(t.Database,{className:"size-4"});if(s==="Schema")return e.jsx(kn,{className:"size-4"});if(s==="Table")return e.jsx(He,{className:"size-4"});if(s==="Model")return e.jsx(Ks,{className:"size-4"})}function p(){return e.jsxs(t.Select,{disabled:h,value:x,onValueChange:l,children:[e.jsx(t.SelectTrigger,{className:t.cn("h-9 w-full border-muted focus:ring-1 focus:ring-offset-0",c),children:e.jsx(t.SelectValue,{placeholder:r?"Loading...":o?"Error":`Select ${s}`})}),e.jsx(t.SelectContent,{children:e.jsxs(t.SelectGroup,{children:[r?e.jsx(t.SelectLabel,{children:"Loading..."}):e.jsxs(t.SelectLabel,{className:"flex items-center justify-between gap-2",children:[s,e.jsx(Kt,{className:t.cn("mr-1 size-3 cursor-pointer text-muted-foreground hover:text-foreground",r&&"animate-spin"),onClick:i})]}),e.jsxs(e.Fragment,{children:[u&&e.jsx(t.SelectItem,{value:"none",children:"None"},"none"),a&&a.map((m,v)=>e.jsx(t.SelectItem,{value:m.id,children:m.name},v))]}),(r||o)&&e.jsx(t.SelectItem,{value:x,children:x})]})})]})}function j(){return e.jsx(_n,{icon:g(),items:a,value:x,onChange:l||(()=>{}),isLoading:r,isError:o,refetch:i,name:s,showNone:u})}return e.jsx(t.TooltipProvider$1,{delayDuration:0,children:e.jsx("div",{className:"flex items-center gap-2",children:d==="select"?p():j()})})}function Gn({column:s}){const[a,n]=L.useState(!1),r=t.useEditorStore(m=>m.selectedTableName),o=t.useEditorStore(m=>m.selectedSchemaName),i=t.useEditorStore(m=>m.selectedDatabaseName),l=t.useEditorStore(m=>m.selectedDatamodelId),u=t.useEditorStore(m=>m.sqlGen),{modelItems:c,connectionType:h}=t.useEditorAside(),d=t.useEditorStore(m=>m.actions.setSqlGen),x=t.useEditorStore(m=>m.actions.setCardSql),g=t.useEditorStore(m=>m.actions.setRunSql);function p(m,v){var E;const f=t.createSqlGenConfig(m,u,v),k=(E=c==null?void 0:c.find(y=>y.id===l))==null?void 0:E.name;d(f);const T=t.getSql(f,t.fmt(o||""),t.fmt(r),k||"",h,i||"");T&&(x(T),g(!0)),n(!1)}function j(){return e.jsxs("ul",{className:"text-xs",children:[e.jsx("li",{onClick:()=>p(s,"COUNT"),className:"cursor-pointer p-1 px-3 transition-colors hover:bg-muted",children:"Count"}),e.jsx("li",{onClick:()=>p(s,"AVG"),className:"cursor-pointer p-1 px-3 transition-colors hover:bg-muted",children:"Avg"}),e.jsx("li",{onClick:()=>p(s,"SUM"),className:"cursor-pointer p-1 px-3 transition-colors hover:bg-muted",children:"Sum"}),e.jsx("li",{onClick:()=>p(s,"MAX"),className:"cursor-pointer p-1 px-3 transition-colors hover:bg-muted",children:"Max"}),e.jsx("li",{onClick:()=>p(s,"MIN"),className:"cursor-pointer p-1 px-3 transition-colors hover:bg-muted",children:"Min"})]})}return e.jsxs(t.HoverCard,{openDelay:0,open:a,onOpenChange:n,children:[e.jsx(t.HoverCardTrigger,{asChild:!0,children:e.jsx(t.EllipsisVertical,{className:"invisible size-3.5 cursor-pointer text-foreground/40 group-hover:visible"})}),e.jsx(t.HoverCardContent,{align:"center",className:"w-20 rounded-md p-0 shadow-none",children:j()})]})}function Nt({column:s}){return e.jsxs(t.Tooltip,{children:[e.jsx(t.TooltipTrigger$1,{className:"shrink-0",asChild:!0,children:t.isTextDataType(s.data_type)?e.jsx(t.TextIcon,{className:"size-3 text-muted-foreground"}):t.isNumberDataType(s.data_type)?e.jsx(gt,{className:"size-3 text-muted-foreground"}):t.isDateDataType(s.data_type)?e.jsx(t.Calendar,{className:"size-3 text-muted-foreground"}):t.isBooleanDataType(s.data_type)?e.jsx(Xt,{className:"size-3 text-muted-foreground"}):s.data_type.startsWith("json")?e.jsx(Qs,{className:"size-3 text-muted-foreground"}):e.jsx(t.QuestionMarkIcon,{className:"size-3 text-muted-foreground"})}),e.jsx(t.TooltipContent,{children:e.jsx("p",{className:"text-xs",children:s.data_type})})]})}function Ue({column:s,className:a}){return e.jsxs("div",{className:t.cn("flex min-h-8 grow items-center space-x-2 px-2 text-sm",a),children:[e.jsx(Nt,{column:s}),e.jsx("span",{children:s.column_name})]})}function $n({id:s,children:a,type:n="column"}){const{attributes:r,listeners:o,setNodeRef:i,transform:l}=t.useDraggable({id:s,data:{type:n}}),u=l?{translate:t.CSS.Translate.toString(l),transform:t.CSS.Transform.toString(l)}:void 0;return e.jsx("div",{ref:i,style:u,...o,...r,children:a})}function Hn({onClick:s,column:a}){const n=t.useDndStore(d=>d.activeId),r=t.useEditorStore(d=>{var x;return(x=d.card.queryConfig)==null?void 0:x.groupByColumns}),o=t.useEditorStore(d=>{var x;return(x=d.card.queryConfig)==null?void 0:x.metricColumns}),i=t.useEditorStore(d=>{var x;return(x=d.card.queryConfig)==null?void 0:x.filterColumns});function l(d){return u(d)||c(d)||h(d)}function u(d){return r==null?void 0:r.some(x=>x.name===d.column_name)}function c(d){return o==null?void 0:o.some(x=>x.name===d.column_name)}function h(d){return i==null?void 0:i.some(x=>x.name===d.column_name)}return e.jsxs("div",{onClick:()=>s(a),className:t.cn("relative grow overflow-x-hidden overflow-y-hidden rounded-md active:border active:border-dashed active:border-muted-foreground",{"bg-muted-foreground/10":l(a)}),children:[n===a.column_name&&e.jsx(t.DragOverlay,{children:e.jsx(Ue,{column:a,className:"rounded-md border-2 border-dashed border-muted-foreground bg-muted shadow-md"})}),n===a.column_name&&e.jsx("div",{className:"absolute inset-0",children:n===a.column_name&&e.jsx(Ue,{column:a})}),e.jsx($n,{id:a.column_name,type:"column",children:e.jsx(Ue,{column:a})})]})}const Ye={id:"__placeholder__",role:"groupby",type:"string"},Ke={id:"__placeholder__",role:"groupby",type:"string"},Qe={id:"__placeholder__",role:"metric",type:"number"},We={name:"Filter",id:"__placeholder__",role:"filter",type:"string"},as={id:"__placeholder__"},fe="droppable-group-by",ge="droppable-metrics",je="droppable-pivot-by",ze="sortable-group-by",it="sortable-metrics",Be="sortable-pivot-by",lt="droppable-filters",ct="sortable-filters",St="sortable-order-by";function Jn(){const s=t.useDndStore(A=>A.insertIndex),a=t.useEditorStore(A=>{var N;return((N=A.card.queryConfig)==null?void 0:N.groupByColumns)||[]}),n=t.useEditorStore(A=>{var N;return((N=A.card.queryConfig)==null?void 0:N.metricColumns)||[]}),r=t.useEditorStore(A=>{var N;return((N=A.card.queryConfig)==null?void 0:N.filterColumns)||[]}),o=t.useEditorStore(A=>{var N;return((N=A.card.queryConfig)==null?void 0:N.orderBy)||[]}),i=t.useEditorStore(A=>{var N;return((N=A.card.queryConfig)==null?void 0:N.pivotColumns)||[]}),{setInsertIndex:l,setActiveId:u,setPreviewGroupByColumns:c,setPreviewMetricColumns:h,setPreviewFilterColumns:d,setPreviewPivotColumns:x}=t.useDndActions(),{setGroupByColumns:g,setMetricColumns:p,setFilterColumns:j,setOrderByColumns:m,setPivotByColumns:v}=t.useEditorActions(),{columns:f}=t.useDataColumns(),{formattedTableName:k}=t.useEditorAside(),T=t.useSensors(t.useSensor(t.MouseSensor,{activationConstraint:{distance:1}})),E=A=>{var z;const{active:N,over:M}=A;if(((z=N.data.current)==null?void 0:z.type)==="column"&&M){const R=M.id,q=f.find($=>$.column_name===N.id);if(!q)return;const B=t.isNumberDataType(q.data_type),K=t.isTextDataType(q.data_type),J=t.isDateDataType(q.data_type);if(K||J){if(a.some(Y=>Y.id===R)){const Y=a.findIndex(V=>V.id===R);l(Y);const _=a.filter(V=>V.id!==Ye.id);_.splice(Y,0,Ye),c(_)}else if(M.id===fe){l(a.length);const Y=[...a,Ye];c(Y),h(n),d(r)}}if(K||J){if(i.some(Y=>Y.id===R)){const Y=i.findIndex(V=>V.id===R);l(Y);const _=i.filter(V=>V.id!==Ke.id);_.splice(Y,0,Ke),x(_)}else if(M.id===je){l(i.length);const Y=[...i,Ke];x(Y),c(a),h(n),d(r)}}if(B||K){if(n.some(Y=>Y.id===R)){console.log("isOverMetricSortable",R);const Y=n.findIndex(V=>V.id===R);l(Y);const _=n.filter(V=>V.id!==Qe.id);_.splice(Y,0,Qe),h(_)}else if(M.id===ge){l(n.length);const Y=[...n,Qe];h(Y),d(r),c(a),x(i)}}if(C(q)){if(r.some(Y=>Y.id===R)){const Y=r.findIndex(V=>V.id===R);l(Y);const _=r.filter(V=>V.id!==We.id);_.splice(Y,0,We),d(_)}else if(M.id===lt){l(r.length);const Y=[...r,We];d(Y),h(n),c(a),x(i)}}}else c(a),h(n),d(r),x(i)};function y(A){u(A.active.id),l(null),c(a),h(n),d(r),x(i)}function C(A){return!r.some(N=>N.name===A.column_name)}function b(A,N="SUM"){const{column_name:M,data_type:z}=A,R=[...n],q=s!==null?s:n.length,B={...A,id:ue(M),name:M,label:Ee(M,N),type:z,role:"metric",aggregate:N};R.splice(q,0,B),p(R)}function w(A){const N=t.isTextDataType(A.data_type);t.isDateDataType(A.data_type);const M=[...i],z=s!==null?s:i.length;let R;N?R={...A,id:ue(A.column_name),name:A.column_name,type:A.data_type,role:"groupby"}:R={...A,id:ue(A.column_name),name:A.column_name,type:A.data_type,role:"groupby",label:Se(A.column_name,"month"),granularity:"month"},M.splice(z,0,R),v(M)}function I(A){const N=t.isTextDataType(A.data_type);t.isDateDataType(A.data_type);const M=[...a],z=s!==null?s:a.length;let R;N?R={...A,id:ue(A.column_name),name:A.column_name,type:A.data_type,role:"groupby"}:R={...A,id:ue(A.column_name),name:A.column_name,type:A.data_type,role:"groupby",label:Se(A.column_name,"month"),granularity:"month"},M.splice(z,0,R),g(M)}function D(A){const N=[...r],M=s!==null?s:r.length,z={...A,id:ue(A.column_name),name:A.column_name,type:A.data_type,sql:t.getDefaultFilterSql(A.data_type,k,t.fmt(A.column_name)),role:"filter"};N.splice(M,0,z),j(N)}function S(A){var R,q,B,K,J,$,Y,_;const{active:N,over:M}=A,z=N.data.current;if((z==null?void 0:z.type)==="column"&&M){const V=f.find(Z=>Z.column_name===N.id);if(!V)return;const P=t.isNumberDataType(V.data_type),H=t.isTextDataType(V.data_type),U=t.isDateDataType(V.data_type);(H||U)&&(M.id===fe||((R=M.data.current)==null?void 0:R.type)===ze)&&I(V),(H||U)&&(M.id===je||((q=M.data.current)==null?void 0:q.type)===Be)?w(V):(P||H)&&(M.id===ge||((B=M.data.current)==null?void 0:B.type)===it)?(P&&b(V),H&&b(V,"COUNT")):C(V)&&(M.id===lt||((K=M.data.current)==null?void 0:K.type)===ct)&&D(V)}if(M&&((J=M.data.current)==null?void 0:J.type)===ze&&a.some(V=>V.id===N.id)){const V=a.findIndex(H=>H.id===N.id),P=a.findIndex(H=>H.id===M.id);if(V!==-1&&P!==-1&&V!==P){const H=[...a],[U]=H.splice(V,1);H.splice(P,0,U),g(H)}}if(M&&(($=M.data.current)==null?void 0:$.type)===Be&&i.some(V=>V.id===N.id)){const V=i.findIndex(H=>H.id===N.id),P=i.findIndex(H=>H.id===M.id);if(V!==-1&&P!==-1&&V!==P){const H=[...i],[U]=H.splice(V,1);H.splice(P,0,U),v(H)}}if(M&&((Y=M.data.current)==null?void 0:Y.type)===it&&n.some(V=>V.id===N.id)){const V=n.findIndex(H=>H.id===N.id),P=n.findIndex(H=>H.id===M.id);if(V!==-1&&P!==-1&&V!==P){const H=[...n],[U]=H.splice(V,1);H.splice(P,0,U),p(H)}}if(M&&((_=M.data.current)==null?void 0:_.type)===ct&&r.some(V=>V.id===N.id)){const V=r.findIndex(H=>H.id===N.id),P=r.findIndex(H=>H.id===M.id);if(V!==-1&&P!==-1&&V!==P){const H=[...r],[U]=H.splice(V,1);H.splice(P,0,U),j(H)}}F(A),u(null),l(null),c(a),h(n),d(r),x(i)}function F(A){const{active:N,over:M}=A,z=N.data.current;if((z==null?void 0:z.type)===St&&M){const R=o.findIndex(B=>ke(B.columnId)===N.id),q=o.findIndex(B=>ke(B.columnId)===M.id);if(R!==-1&&q!==-1&&R!==q){const B=[...o],[K]=B.splice(R,1);B.splice(q,0,K),m(B)}}}function O(){u(null),l(null),c(a),h(n),d(r)}return{handleDragOver:E,handleDragEnd:S,handleDragStart:y,handleDragCancel:O,sensors:T}}function ue(s){return`${s}-${t.v4()}`}function Ee(s,a){const n=t.toTitleCase(s.replace(/_/g," "));switch(a){case"SUM":return`Sum of ${n}`;case"COUNT":return`Count of ${n}`;case"DISTINCT":return`Unique ${n}`;case"AVG":return`Average of ${n}`;case"MIN":return`Min of ${n}`;case"MAX":return`Max of ${n}`;default:return n}}function ke(s){return`${St}-${s}`}function Se(s,a){const n=t.toTitleCase(s.replace(/_/g," "));switch(a){case"day":return`Date of ${n}`;case"week":return`Week of ${n}`;case"month":return`Month of ${n}`;case"year":return`Year of ${n}`;default:return n}}function Un({schemaName:s,tableName:a,columns:n,status:r}){const o=t.useEditorStore(N=>N.sqlGen),i=t.useEditorStore(N=>N.isDevMode),l=t.useEditorStore(N=>N.selectedConnectionId),u=t.useEditorStore(N=>{var M,z;return(z=(M=N.card)==null?void 0:M.queryConfig)==null?void 0:z.metricColumns});t.useEditorStore(N=>{var M,z;return(z=(M=N.card)==null?void 0:M.queryConfig)==null?void 0:z.groupByColumns});const c=t.useEditorStore(N=>N.selectedDatabaseName),h=t.useEditorStore(N=>N.selectedSchemaName),d=t.useEditorStore(N=>N.selectedTableName),x=t.useEditorStore(N=>N.selectedDatamodelId),g=t.useDashboardStore(N=>N.dashboard.aiScopeTables),{addToAIScope:p,removeFromAIScope:j}=t.useDashboardActions(),{addColumnToMetricColumns:m,addColumnToGroupBy:v,setCardSql:f,setRunSql:k,setSqlGen:T,setIsShowingVisual:E}=t.useEditorActions(),{connectionType:y,modelItems:C,selectedDatamodelName:b}=t.useEditorAside();function w(N){var M;if(i){const z=t.createSqlGenConfig(N,o);T(z);const R=(M=C==null?void 0:C.find(B=>B.id===x))==null?void 0:M.name,q=t.getSql(z,t.fmt(s),t.fmt(a),R||"",y,c||"");q&&(f(q),k(!0))}if(!i){if(t.isTextDataType(N.data_type))if(u&&u.length>0){const z={...N,role:"groupby",id:ue(N.column_name),name:N.column_name,type:N.data_type};v(z)}else{const z={...N,role:"metric",id:ue(N.column_name),name:N.column_name,label:Ee(N.column_name,"COUNT"),type:N.data_type,aggregate:"COUNT"};m(z)}if(t.isDateDataType(N.data_type)){const z={...N,role:"groupby",id:ue(N.column_name),name:N.column_name,type:N.data_type,label:Se(N.column_name,"month"),granularity:"month"};v(z)}if(t.isNumberDataType(N.data_type)){const z={...N,role:"metric",id:ue(N.column_name),name:N.column_name,label:Ee(N.column_name,"SUM"),type:N.data_type,aggregate:"SUM"};m(z)}}}function I(N){N===!0&&(d||b)&&l?(p({connectionId:l,connectionType:y==="S3"||y==="API"?"DuckDb":y||"",databaseName:c||"",schemaName:h||"",tableName:d||"",datamodelName:t.resolveDatamodelName(b),datamodelId:x||""}),t.ue.success("Added to Assistant Scope",{position:"top-right",duration:2500,richColors:!0})):(j({connectionId:l||"",connectionType:y||"",tableName:d||"",datamodelName:t.resolveDatamodelName(b),datamodelId:x||"",databaseName:c||"",schemaName:h||""}),t.ue("Removed from Assistant Scope",{position:"top-right",duration:2e3,richColors:!0}))}function D(){const N=t.getFormattedTableNameForQuery({schemaName:s,tableName:a,modelName:b,connectionType:y,databaseName:c||""});E(!1);const M=y==="MSSQL"?`SELECT top 10 * FROM ${N}`:`SELECT * FROM ${N} LIMIT 10`;f(M),k(!0)}function S(){const N=t.getFormattedTableNameForQuery({schemaName:s,tableName:a,modelName:b,connectionType:y,databaseName:c||""});f(`SELECT COUNT(*) as count FROM ${N}`),k(!0)}function F(){return(g==null?void 0:g.find(N=>N.connectionId===l&&(N.tableName===d||N.datamodelName===b)))!==void 0}function O(N){return i?e.jsxs("div",{className:"flex grow items-center justify-between space-x-2 rounded-md px-2 py-1",onClick:()=>w(N),children:[e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(Nt,{column:N}),e.jsx("span",{children:N.column_name})]}),e.jsx("div",{onClick:M=>M.stopPropagation(),className:"flex items-center justify-end",children:t.isNumberDataType(N.data_type)&&e.jsx(Gn,{column:N})})]}):e.jsx(Hn,{onClick:w,column:N})}function A(){return e.jsxs(e.Fragment,{children:[e.jsxs(t.Toggle,{title:`${F()?"Remove from":"Add to"} Assistant Scope`,pressed:F(),onPressedChange:I,variant:"outline",className:t.cn("h-9 px-2",{}),children:[e.jsx(t.Plus,{className:t.cn("mr-1 size-4",{"text-green-400":F(),"text-muted-foreground":!F()})}),"AI"]}),e.jsxs(t.Button,{disabled:r!=="Success",onClick:D,size:"sm",variant:"secondary",className:"flex",children:[r!=="Success"?e.jsx(ce,{className:"mr-2 h-4 w-4 animate-spin text-foreground/50"}):e.jsx(vn,{className:"mr-2 size-4"}),"Top 10"]}),e.jsxs(t.Button,{disabled:r!=="Success",onClick:S,size:"sm",variant:"secondary",className:"w-full",children:[r!=="Success"?e.jsx(ce,{className:"mr-2 h-4 w-4 animate-spin text-foreground/50"}):e.jsx(Qt,{className:"mr-2 size-4"}),"Count"]})]})}return r!=="Success"?e.jsx("div",{className:"flex grow flex-col justify-start gap-2 px-6",children:Array.from({length:3}).map((N,M)=>e.jsx(t.Skeleton,{className:"h-7 w-full"},M))}):e.jsxs("div",{className:"flex grow flex-col space-y-4 px-4",children:[i&&e.jsx("div",{className:"mx-2 flex items-center justify-center gap-2",children:A()}),e.jsxs(t.CustomScrollArea,{className:"flex-1 basis-0 overflow-y-auto px-2 py-0",children:[e.jsx("div",{className:"flex flex-col gap-1",children:n==null?void 0:n.map(N=>e.jsx("div",{className:t.cn("mr-2 cursor-pointer rounded-md text-sm transition-colors hover:bg-muted"),children:O(N)},N.column_name))}),e.jsx(t.ScrollBar,{orientation:"vertical"}),e.jsx(t.ScrollBar,{orientation:"horizontal"})]})]})}function rs({ColumnsComponent:s,type:a}){var Lt;const n=t.useEditorStore(X=>X.card),r=t.useEditorStore(X=>X.selectedConnectionId),o=t.useDashboardStore(X=>X.showFilters),i=t.useEditorStore(X=>X.selectedDatamodelId),l=t.useEditorStore(X=>X.selectedDatabaseName),u=t.useEditorStore(X=>X.selectedSchemaName),c=t.useEditorStore(X=>X.selectedTableName),h=t.useEditorStore(X=>X.isDevMode),{setSelectedTableName:d,setIsDevMode:x,setCardSql:g}=t.useEditorActions(),{connectionItems:p,modelItems:j,schemaItems:m,databaseItems:v,tableItems:f,columns:k,isConnectionsLoading:T,isModelsLoading:E,isConnectionsError:y,isModelsError:C,isDatabasesLoadingOrFetching:b,isDatabaseError:w,isSchemaLoadingOrFetching:I,isSchemaError:D,isColumnsError:S,isColumnsLoadingOrFetching:F,isTableLoadingOrFetching:O,isTableError:A,connectionType:N,isNonDbConnection:M,refetchDatabases:z,refetchModels:R,refetchSchemas:q,refetchTables:B,refetchColumns:K,refetchConnections:J,handleConnectionChange:$,handleDatabaseChange:Y,handleSchemaChange:_,handleDatamodelChange:V,handleTableChange:P,tableName:H,schemaName:U}=t.useEditorAside(),{data:Z,refetch:de}=t.useDashboardCardQuery(n);function G(){return s?s(U,H,k):e.jsx(Un,{schemaName:U,tableName:H,columns:k,status:F?"Loading...":S?"Error":"Success"})}function Q(){window.location.href="mailto:support@semaphor.cloud?subject=Help&body=I need help with..."}function se(X){X===!0?Z&&(g(Z.userSql),setTimeout(()=>{de()},100)):g(""),x(X)}function me(){if(!r||N==="S3"||N==="API")return null;if(!i||i==="none")return e.jsx(ve,{refetch:z,showNone:!0,onChange:Y,isError:w,isLoading:b,items:v,value:l||"",title:"Database",type:"combobox"})}function Ds(){if(r&&!M&&(!l||l==="none")&&(!i||i==="none"))return e.jsxs("div",{className:"grid grid-cols-3",children:[e.jsx("div",{className:"col-span-1 grid place-items-center",children:e.jsx(t.Separator,{})}),e.jsx("div",{className:"col-span-1 grid place-items-center",children:e.jsx(t.Label,{children:"OR"})}),e.jsx("div",{className:"col-span-1 grid place-items-center",children:e.jsx(t.Separator,{})})]})}function Ts(){if(!r||N==="S3"||N==="API")return null;if(!l||l==="none")return e.jsx(ve,{refetch:R,showNone:!0,onChange:V,isError:C,isLoading:E,items:j,value:i||"",title:"Model",type:"combobox"})}function Ms(){return!r||N==="S3"||N==="API"||!l||l==="none"?null:e.jsx(ve,{refetch:q,onChange:_,isError:D,isLoading:I,items:m,value:u||"",title:"Schema",type:"combobox"})}function As(){function X(){let xe=!1;return N==="API"?(xe=!1,xe):((N==="S3"||r&&l&&l!=="none"&&u)&&(xe=!0),xe)}return X()?e.jsx(ve,{refetch:B,onChange:P,isError:A,items:f,isLoading:O,value:c||"",title:"Table",type:"combobox"}):null}return e.jsxs("aside",{className:t.cn("flex h-full min-w-[310px] max-w-[310px] flex-col justify-between rounded-none",{"border-r-[1px]":!o}),children:[e.jsxs("section",{className:"space-y-3 px-6 py-4",children:[e.jsx(ve,{refetch:J,items:p,title:"Connection",value:r||"",onChange:$,isLoading:T,isError:y,type:"combobox",connectionTooltip:((Lt=p==null?void 0:p.find(X=>X.id===r))==null?void 0:Lt.type)||""}),me(),Ds(),Ts(),Ms(),As()]}),G(),e.jsx("div",{className:"flex items-center justify-between gap-2 px-6 py-4",children:a==="main"&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(t.Switch,{className:"data-[state=checked]:bg-muted-foreground/80",checked:h,onCheckedChange:se,id:"dev-mode"}),e.jsx(t.Label,{htmlFor:"dev-mode",children:"Advanced Mode"})]}),e.jsxs(t.Button,{onClick:Q,size:"sm",variant:"secondary",className:"group flex items-center gap-2 rounded-full",children:[e.jsx(Sn,{className:"size-4"}),e.jsx("span",{className:"hidden group-hover:block",children:"Help"})]})]})})]})}function os({sql:s,error:a,onClick:n}){const r=t.useEditorStore(c=>c.isDevMode),{setIsDevMode:o,setCardSql:i,setRunSql:l}=t.useEditorActions();function u(){o(!r),i(s),setTimeout(()=>{l(!0)},100)}return e.jsx("div",{className:"flex justify-between space-x-4",children:e.jsxs("div",{className:"flex grow flex-col space-y-2",children:[e.jsxs(t.ScrollArea,{className:"max-h-[250px] min-h-0 overflow-scroll text-xs leading-relaxed text-foreground/80",children:[a&&a,s&&e.jsx("div",{className:"flex flex-col space-y-1.5",children:e.jsx("div",{className:"rounded-md",children:e.jsx(t.CodeHighlighter,{code:s,language:"sql"})})})]}),!r&&e.jsxs(t.Button,{onClick:u,variant:"secondary",size:"sm",children:[e.jsx(cn,{className:"mr-2 h-4 w-4"}),"View in SQL Editor"]}),a&&n&&e.jsxs(t.Button,{onClick:n,size:"sm",variant:"secondary",children:[e.jsx(pn,{className:"mr-2 h-4 w-4"}),"Debug with Assistant"]})]})})}function is({sql:s,error:a,onClick:n}){return e.jsxs(t.HoverCard,{openDelay:a?0:500,children:[e.jsx(t.HoverCardTrigger,{asChild:!0,children:e.jsx(t.Button,{variant:"link",className:"h-5 p-0",children:a?e.jsx(Ut,{className:"h-4 w-4 text-red-500"}):e.jsx(t.Info,{className:"h-4 w-4 text-muted-foreground"})})}),e.jsx(t.HoverCardContent,{align:"start",className:"w-full min-w-fit max-w-[700px]",children:e.jsx(os,{sql:s,error:a,onClick:n})})]})}var ls={color:void 0,size:void 0,className:void 0,style:void 0,attr:void 0},Vt=L.createContext&&L.createContext(ls),Yn=["attr","size","title"];function Kn(s,a){if(s==null)return{};var n=Qn(s,a),r,o;if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(s);for(o=0;o<i.length;o++)r=i[o],!(a.indexOf(r)>=0)&&Object.prototype.propertyIsEnumerable.call(s,r)&&(n[r]=s[r])}return n}function Qn(s,a){if(s==null)return{};var n={};for(var r in s)if(Object.prototype.hasOwnProperty.call(s,r)){if(a.indexOf(r)>=0)continue;n[r]=s[r]}return n}function Re(){return Re=Object.assign?Object.assign.bind():function(s){for(var a=1;a<arguments.length;a++){var n=arguments[a];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(s[r]=n[r])}return s},Re.apply(this,arguments)}function qt(s,a){var n=Object.keys(s);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(s);a&&(r=r.filter(function(o){return Object.getOwnPropertyDescriptor(s,o).enumerable})),n.push.apply(n,r)}return n}function Pe(s){for(var a=1;a<arguments.length;a++){var n=arguments[a]!=null?arguments[a]:{};a%2?qt(Object(n),!0).forEach(function(r){Wn(s,r,n[r])}):Object.getOwnPropertyDescriptors?Object.defineProperties(s,Object.getOwnPropertyDescriptors(n)):qt(Object(n)).forEach(function(r){Object.defineProperty(s,r,Object.getOwnPropertyDescriptor(n,r))})}return s}function Wn(s,a,n){return a=Xn(a),a in s?Object.defineProperty(s,a,{value:n,enumerable:!0,configurable:!0,writable:!0}):s[a]=n,s}function Xn(s){var a=Zn(s,"string");return typeof a=="symbol"?a:a+""}function Zn(s,a){if(typeof s!="object"||!s)return s;var n=s[Symbol.toPrimitive];if(n!==void 0){var r=n.call(s,a);if(typeof r!="object")return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return(a==="string"?String:Number)(s)}function cs(s){return s&&s.map((a,n)=>L.createElement(a.tag,Pe({key:n},a.attr),cs(a.child)))}function ye(s){return a=>L.createElement(ea,Re({attr:Pe({},s.attr)},a),cs(s.child))}function ea(s){var a=n=>{var{attr:r,size:o,title:i}=s,l=Kn(s,Yn),u=o||n.size||"1em",c;return n.className&&(c=n.className),s.className&&(c=(c?c+" ":"")+s.className),L.createElement("svg",Re({stroke:"currentColor",fill:"currentColor",strokeWidth:"0"},n.attr,r,l,{className:c,style:Pe(Pe({color:s.color||n.color},n.style),s.style),height:u,width:u,xmlns:"http://www.w3.org/2000/svg"}),i&&L.createElement("title",null,i),s.children)};return Vt!==void 0?L.createElement(Vt.Consumer,null,n=>a(n)):a(ls)}function ta(s){return ye({attr:{viewBox:"0 0 448 512"},child:[{tag:"path",attr:{d:"M439.8 200.5c-7.7-30.9-22.3-54.2-53.4-54.2h-40.1v47.4c0 36.8-31.2 67.8-66.8 67.8H172.7c-29.2 0-53.4 25-53.4 54.3v101.8c0 29 25.2 46 53.4 54.3 33.8 9.9 66.3 11.7 106.8 0 26.9-7.8 53.4-23.5 53.4-54.3v-40.7H226.2v-13.6h160.2c31.1 0 42.6-21.7 53.4-54.2 11.2-33.5 10.7-65.7 0-108.6zM286.2 404c11.1 0 20.1 9.1 20.1 20.3 0 11.3-9 20.4-20.1 20.4-11 0-20.1-9.2-20.1-20.4.1-11.3 9.1-20.3 20.1-20.3zM167.8 248.1h106.8c29.7 0 53.4-24.5 53.4-54.3V91.9c0-29-24.4-50.7-53.4-55.6-35.8-5.9-74.7-5.6-106.8.1-45.2 8-53.4 24.7-53.4 55.6v40.7h106.9v13.6h-147c-31.1 0-58.3 18.7-66.8 54.2-9.8 40.7-10.2 66.1 0 108.6 7.6 31.6 25.7 54.2 56.8 54.2H101v-48.8c0-35.3 30.5-66.4 66.8-66.4zm-6.7-142.6c-11.1 0-20.1-9.1-20.1-20.3.1-11.3 9-20.4 20.1-20.4 11 0 20.1 9.2 20.1 20.4s-9 20.3-20.1 20.3z"},child:[]}]})(s)}function ds(s){const{theme:a}=t.useTheme(),n=t.useDashboardStore(m=>m.theme),r=t.useEditorStore(m=>m.frame),o=t.useEditorStore(m=>m.card),i=t.useEditorStore(m=>m.activeTabCardId),l=t.Le(),{refetch:u}=t.useDashboardCardQuery(o),{updateCardTab:c,setRunSql:h}=t.useEditorActions(),d=L.useRef(),x=L.useRef({}),g=r.cards.find(m=>m.id===i),p=L.useCallback(()=>{let m="";return s==="python"&&(m=(g==null?void 0:g.python)||t.PYTHON_DEFAULT_CODE),s==="sql"&&(m=(g==null?void 0:g.sql)||""),m},[s,g]);L.useEffect(()=>{if(i&&o.id&&o.id!==i){if(d.current&&l){const m=d.current.getModel();m&&(x.current[o.id]=m);let v=x.current[i];if(!v){const f=p();v=l.editor.createModel(f,s),x.current[i]=v}d.current.setModel(v)}c(i,o),h(!0)}},[i,o,c,l,h,s,p]),L.useEffect(()=>{if(!l)return;const m=window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light",v=n==="system"?m:n;l.editor.defineTheme("default",{base:v==="light"?"vs":"vs-dark",inherit:!0,rules:[],colors:{"editor.background":v==="light"?"#ffffff":t.hslStringToHex("240 10% 10%")}}),l.editor.setTheme("default")},[a,n,l]),L.useEffect(()=>{const m=d.current;m&&l&&o&&m.addCommand(l.KeyMod.Shift|l.KeyCode.Enter,function(){return u(),!1})},[l,u,o]);function j(m,v){if(d.current=m,!x.current[o.id]){const f=m.getModel(),k=p();x.current[o.id]=f||(v==null?void 0:v.editor.createModel(k,s))||m.getModel()}m.setModel(x.current[o.id])}return{handleEditorDidMount:j}}function sa(){const s=t.useEditorStore(l=>l.card.sql),a=t.useEditorStore(l=>l.sqlGen),{setCardSql:n,setSqlGen:r}=t.useEditorActions(),{handleEditorDidMount:o}=ds("sql");function i(l){n(l),t.isObjectEmpty(a)||r({})}return e.jsxs("div",{className:"relative h-full w-full",children:[!s&&e.jsx("div",{className:"pointer-events-none absolute left-[65px] top-3 z-10 font-mono text-sm text-muted-foreground",children:"Write your SQL here"}),e.jsx(t.Ft,{theme:"default",className:"pt-3",onChange:i,value:s||"",options:{minimap:{enabled:!1},wordWrap:"on",scrollbar:{verticalScrollbarSize:0,horizontalScrollbarSize:0},fontSize:13,renderLineHighlight:"none"},defaultLanguage:"sql",onMount:o})]})}function na(){const s=t.useEditorStore(o=>o.card.python),{setCardPython:a}=t.useEditorActions(),{handleEditorDidMount:n}=ds("python");function r(o){a(o)}return e.jsx(t.Ft,{className:"pt-3",theme:"default",onChange:r,value:s||t.PYTHON_DEFAULT_CODE,options:{minimap:{enabled:!1},wordWrap:"on",scrollbar:{verticalScrollbarSize:0,horizontalScrollbarSize:0},fontSize:13,renderLineHighlight:"none"},defaultLanguage:"python",onMount:n})}function aa({jsonString:s}){return e.jsx(t.Ft,{theme:"default",defaultLanguage:"json",className:"mt-1 min-h-[220px]",value:s,options:{readOnly:!0,minimap:{enabled:!1},lineNumbers:"off",glyphMargin:!1,wordWrap:"on",scrollbar:{verticalScrollbarSize:0,horizontalScrollbarSize:0},fontSize:12,renderLineHighlight:"none"}})}function ra(){const s=t.useQueryClient(),[a,n]=L.useState(!1),[r,o]=L.useState(!1);t.useEditorStore(B=>B.frame);const i=t.useEditorStore(B=>B.card);t.useEditorStore(B=>B.isShowingVisual),t.useEditorStore(B=>B.selectedConnectionId),t.useEditorStore(B=>B.tempQueryData),t.useEditorStore(B=>B.selectedDatabaseName),t.useEditorStore(B=>B.selectedDatamodelId),t.useEditorStore(B=>B.selectedSchemaName);const l=t.useEditorStore(B=>B.showAIDialog);t.useEditorStore(B=>B.selectedTableName);const u=t.useEditorStore(B=>{var K;return(K=B==null?void 0:B.card)==null?void 0:K.sql}),c=t.useEditorStore(B=>{var K;return(K=B==null?void 0:B.card)==null?void 0:K.python}),{sheetFilterValues:h}=t.useSheetFilterValues(),{setCardCustomCfg:d,setQueryResultColumns:x,setCardSql:g,setTempQueryData:p,setPythonStdOut:j,setCardPreferences:m,setCardType:v,setCardPython:f,runAssistant:k,setShowAIDialog:T}=t.useEditorActions();t.useEditorStore(B=>B.onSave),t.useEditorStore(B=>B.onClose),t.useEditorStore(B=>B.actions.setSqlGen);const E=t.useEditorStore(B=>B.runSql),y=t.useEditorStore(B=>B.actions.setRunSql),C=t.useEditorStore(B=>B.actions.setIsSqlRunning),{data:b,isLoading:w,isFetching:I,isSuccess:D,isError:S,error:F,queryKey:O,refetch:A,errorMessage:N}=t.useDashboardCardQuery(i);L.useEffect(()=>{if(b&&(b.rowLimitExceeded&&t.ue.error("Row limit (10,000) exceeded. Refine your query.",{position:"top-center",richColors:!0}),j((b==null?void 0:b.output)||""),b.records.length>0)){const{dimensionKeys:B}=t.getKeys(b.records);x(B)}},[b,x,j]),L.useEffect(()=>{(w||I)&&C(!0),(S||D)&&C(!1),S&&(x([]),j(""))},[w,I,b,j,S,D,C,x,F]),L.useEffect(()=>{E&&(s.getQueryData(O)||A(),y(!1))},[E,A,y,O,s]);async function M(){const B=`Can you help me debug this query in the current table scope?
|
|
392
392
|
|
|
393
393
|
Error:
|
|
394
394
|
|