PageSourceSearch

https://scenevista.ai/assets/CompanyManagement-DD-f8BG9.js

js scenevista.ai collected 2026-10-05 12:39:36 UTC 18,050 bytes, 1 lines download raw bytes

1import{b as ye,K as _e,O as c,i as T,j as e,ah as De,ad as o,B as r,Q as Me,$ as Se,n as O,aI as W,at as Ae,au as ke,av as Te,by as Oe,ax as X,aw as M,P as Fe,ae as Ee,a1 as Ie,M as qe,v as F,am as E,an as I,ao as q,ap as z,aA as R,ac as m,aO as B,R as P}from"./index-BpbZi-vK.js";import{u as ze}from"./useMutation-C7CZk7Z3.js";import{T as Y,a as Z,b as g,c as h,d as ee,e as l}from"./table-_XGBGBex.js";import{A as Re,b as Be,c as Pe,d as $e,e as Le,f as Ue,g as Ke,h as Qe}from"./alert-dialog-C973xiDE.js";import{S as se,a as ae,b as ie,c as le,d as b}from"./select-DPGXVhwY.js";import{S as ne}from"./scroll-area-Ba0g78jD.js";import{C as Ve,a as He,b as Je}from"./collapsible-DY4vkzea.js";import{a as Ge}from"./chevron-up-Cip4inou.js";import{E as We}from"./ellipsis-giETTDFX.js";import{T as Xe}from"./trash-2-CR0XjFBq.js";import{P as Ye}from"./phone-Owz0OU3D.js";import{G as Ze,U as es}from"./user-minus-D0ygSh6Q.js";import{M as ss}from"./minus-CsA_lIO7.js";import{f as te}from"./format-BtWS_xeJ.js";import"./index-BdQq_4o_.js";import"./en-US-DaTnBiBt.js";const bs=()=>{const{session:S,getAccessToken:w}=ye(),$=_e(),[A,re]=c.useState(""),[x,L]=c.useState(null),[de,f]=c.useState(!1),[ce,y]=c.useState(!1),[oe,k]=c.useState(!1),[U,_]=c.useState(!1),[n,j]=c.useState(null),[p,v]=c.useState(0),[D,K]=c.useState(!1),[Q,V]=c.useState("member"),[i,d]=c.useState({name:"",billing_email:"",tax_id:"",billing_address:"",billing_city:"",billing_postal_code:"",billing_country:"",phone:"",website:""}),{data:me=[],isLoading:he}=T({queryKey:["admin-companies"],queryFn:async()=>{const s=await w();if(!s)throw new Error("Not authenticated");const a=await fetch("https://awwzwtmancqefscmiohr.supabase.co/functions/v1/admin-companies",{method:"POST",headers:{Authorization:`Bearer ${s}`,"Content-Type":"application/json"},body:JSON.stringify({action:"list"})});if(!a.ok){const N=await a.json();throw new Error(N.error||"Failed to fetch companies")}return(await a.json()).companies},enabled:!!S?.access_token}),{data:C}=T({queryKey:["admin-company-details",x],queryFn:async()=>{if(!x)return null;const s=await w();if(!s)throw new Error("Not authenticated");const a=await fetch("https://awwzwtmancqefscmiohr.supabase.co/functions/v1/admin-companies",{method:"POST",headers:{Authorization:`Bearer ${s}`,"Content-Type":"application/json"},body:JSON.stringify({action:"get",companyId:x})});if(!a.ok){const t=await a.json();throw new Error(t.error||"Failed to fetch company details")}return a.json()},enabled:!!S?.access_token&&!!x}),{data:xe=[]}=T({queryKey:["admin-users-for-companies"],queryFn:async()=>{const s=await w();if(!s)throw new Error("Not authenticated");const a=await fetch("https://awwzwtmancqefscmiohr.supabase.co/functions/v1/admin-users",{headers:{Authorization:`Bearer ${s}`,"Content-Type":"application/json"}});if(!a.ok){const N=await a.json();throw new Error(N.error||"Failed to fetch users")}return(await a.json()).users||[]},enabled:!!S?.access_token&&U}),u=ze({mutationFn:async s=>{const a=await w();if(!a)throw new Error("Not authenticated");const t=await fetch("https://awwzwtmancqefscmiohr.supabase.co/functions/v1/admin-companies",{method:"POST",headers:{Authorization:`Bearer ${a}`,"Content-Type":"application/json"},body:JSON.stringify(s)});if(!t.ok){const N=await t.json();throw new Error(N.error||"Operation failed")}return t.json()},onSuccess:(s,a)=>{$.invalidateQueries({queryKey:["admin-companies"]}),$.invalidateQueries({queryKey:["admin-company-details"]});const t={create:"Company created successfully",update:"Company updated successfully",delete:"Company deleted successfully",adjust_credits:"Credits adjusted successfully",add_member:"Member added successfully",remove_member:"Member removed successfully",update_member_role:"Member role updated successfully"};P.success(t[a.action]||"Operation completed successfully")},onError:s=>{P.error(s.message)}}),ue=()=>{K(!1),j(null),d({name:"",billing_email:"",tax_id:"",billing_address:"",billing_city:"",billing_postal_code:"",billing_country:"",phone:"",website:""}),f(!0)},je=s=>{K(!0),j(s),d({name:s.name,billing_email:s.billing_email||"",tax_id:s.tax_id||"",billing_address:s.billing_address||"",billing_city:s.billing_city||"",billing_postal_code:s.billing_postal_code||"",billing_country:s.billing_country||"",phone:s.phone||"",website:s.website||""}),f(!0)}
1,pe=s=>{j(s),v(0),y(!0)},ge=s=>{j(s),k(!0)},H=s=>{j(s),V("member"),_(!0)},be=()=>{if(!i.name.trim()){P.error("Company name is required");return}D&&n?u.mutate({action:"update",companyId:n.id,...i}):u.mutate({action:"create",...i}),f(!1)},fe=()=>{n&&(u.mutate({action:"delete",companyId:n.id}),k(!1),j(null),L(null))},ve=()=>{!n||p===0||(u.mutate({action:"adjust_credits",companyId:n.id,amount:p}),y(!1),v(0),j(null))},Ce=s=>{n&&(u.mutate({action:"add_member",companyId:n.id,userId:s,role:Q}),_(!1))},Ne=s=>{x&&u.mutate({action:"remove_member",companyId:x,userId:s})},we=(s,a)=>{x&&u.mutate({action:"update_member_role",companyId:x,userId:s,role:a})},J=me.filter(s=>s.name.toLowerCase().includes(A.toLowerCase())||s.tax_id?.toLowerCase().includes(A.toLowerCase())),G=xe.filter(s=>!(C?.members||[]).some(t=>t.user_id===s.id));return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-3xl font-bold",children:"Company Management"}),e.jsx("p",{className:"text-muted-foreground",children:"Manage all tenant companies on the platform"})]}),e.jsxs("div",{className:"flex flex-col sm:flex-row items-start sm:items-center justify-between gap-4",children:[e.jsxs("div",{className:"relative flex-1 max-w-md w-full",children:[e.jsx(De,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted-foreground"}),e.jsx(o,{placeholder:"Search companies by name or tax ID...",value:A,onChange:s=>re(s.target.value),className:"pl-10"})]}),e.jsxs(r,{onClick:ue,className:"gap-2",children:[e.jsx(Me,{className:"w-4 h-4"}),"Add Company"]})]}),e.jsx("div",{className:"border rounded-lg",children:e.jsxs(Y,{children:[e.jsx(Z,{children:e.jsxs(g,{children:[e.jsx(h,{className:"w-8"}),e.jsx(h,{children:"Company"}),e.jsx(h,{children:"Tax/CVR"}),e.jsx(h,{children:"Credits"}),e.jsx(h,{children:"Members"}),e.jsx(h,{children:"Created"}),e.jsx(h,{className:"w-12"})]})}),e.jsx(ee,{children:he?e.jsx(g,{children:e.jsx(l,{colSpan:7,className:"text-center py-8 text-muted-foreground",children:"Loading companies..."})}):J.length===0?e.jsx(g,{children:e.jsx(l,{colSpan:7,className:"text-center py-8 text-muted-foreground",children:"No companies found"})}):J.map(s=>e.jsx(Ve,{open:x===s.id,onOpenChange:a=>L(a?s.id:null),asChild:!0,children:e.jsxs(e.Fragment,{children:[e.jsxs(g,{className:"cursor-pointer hover:bg-muted/50",children:[e.jsx(l,{children:e.jsx(He,{asChild:!0,children:e.jsx(r,{variant:"ghost",size:"icon",className:"w-6 h-6",children:x===s.id?e.jsx(Ge,{className:"w-4 h-4"}):e.jsx(Se,{className:"w-4 h-4"})})})}),e.jsx(l,{className:"font-medium",children:s.name}),e.jsx(l,{className:"text-muted-foreground",children:s.tax_id||"—"}),e.jsx(l,{children:e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(O,{className:"w-4 h-4 text-gold"}),s.credits?.balance??0]})}),e.jsx(l,{children:e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(W,{className:"w-4 h-4 text-muted-foreground"}),s.member_count]})}),e.jsx(l,{className:"text-muted-foreground",children:te(new Date(s.created_at),"MMM d, yyyy")}),e.jsx(l,{children:e.jsxs(Ae,{children:[e.jsx(ke,{asChild:!0,children:e.jsx(r,{variant:"ghost",size:"icon",children:e.jsx(We,{className:"w-4 h-4"})})}),e.jsxs(Te,{align:"end",children:[e.jsx(Oe,{children:"Actions"}),e.jsx(X,{}),e.jsxs(M,{onClick:()=>je(s),children:[e.jsx(Fe,{className:"w-4 h-4 mr-2"}),"Edit Company"]}),e.jsxs(M,{onClick:()=>H(s),children:[e.jsx(W,{className:"w-4 h-4 mr-2"}),"Add Member"]}),e.jsxs(M,{onClick:()=>pe(s),children:[e.jsx(O,{className:"w-4 h-4 mr-2"}),"Adjust Credits"]}),e.jsx(X,{}),e.jsxs(M,{onClick:()=>ge(s),className:"text-destructive focus:text-destructive",children:[e.jsx(Xe,{className:"w-4 h-4 mr-2"}),"Delete Company"]})]})]})})]}),e.jsx(Je,{asChild:!0,children:e.jsx(g,{className:"bg-muted/30",children:e.jsx(l,{colSpan:7,className:"p-0",children:e.jsxs("div",{className:"p-6 space-y-6",children:[e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6",children:[e.jsxs("div",{className:"space-y-4",children:[e.jsx("h4",{className:"font-semibold text-sm text-muted-foreground uppercase tracking-wide",children:"Contact Information"}),e.jsxs("div",{className:"space-y-2",children:[s.billing_email&&e.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[e.jsx(Ee,{className:"w-4 h-4 text-muted-foreground"}),s.billing_email]}),s.phone&&e.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[e.jsx(Ye,{className:"w-4 h-4 text-muted-foreground"}),s.phone]}),s.website&&e.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[e.jsx(Ze,{className:"w-4 h-4 text-muted-foreground"}),e.jsx("a",{href:s.website,target:"_blank",rel:"noopener noreferrer",className:"text-primary hover:underline",children:s.website})]})]})]}),e.jsxs("div",{className:"space-y-4",children:[e.jsx("h4",{className:"font-semibold text-sm text-muted-foreground uppercase tracking-wide",children:"Billing Address"}),e.jsxs("div",{className:"space-y-2",children:[s.tax_id&&e.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[e.jsx(Ie,{className:"w-4 h-4 text-muted-foreground"}),"Tax/CVR: ",s.tax_id]}),(s.billing_address||s.billing_city)&&e.jsxs("div",{className:"flex items-start gap-2 text-sm",children:[e.jsx(qe,{className:"w-4 h-4 text-muted-foreground mt-0.5"}),e.jsxs("div",{children:[s.billing_address&&e.jsx("div",{children:s.billing_address}),e.jsxs("div",{children:[[s.billing_postal_code,s.billing_city].filter(
1Boolean).join(" "),s.billing_country&&`, ${s.billing_country}`]})]})]})]})]}),e.jsxs("div",{className:"space-y-4",children:[e.jsx("h4",{className:"font-semibold text-sm text-muted-foreground uppercase tracking-wide",children:"Credits"}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-2 text-2xl font-bold",children:[e.jsx(O,{className:"w-6 h-6 text-gold"}),s.credits?.balance??0]}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:["Lifetime used: ",s.credits?.lifetime_used??0]})]})]})]}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("h4",{className:"font-semibold text-sm text-muted-foreground uppercase tracking-wide",children:["Members (",C?.members?.length||0,")"]}),e.jsxs(r,{size:"sm",variant:"outline",onClick:()=>H(s),children:[e.jsx(F,{className:"w-4 h-4 mr-2"}),"Add Member"]})]}),C?.members&&C.members.length>0?e.jsx("div",{className:"border rounded-lg overflow-hidden",children:e.jsxs(Y,{children:[e.jsx(Z,{children:e.jsxs(g,{children:[e.jsx(h,{children:"Email"}),e.jsx(h,{children:"Role"}),e.jsx(h,{children:"Joined"}),e.jsx(h,{className:"w-12"})]})}),e.jsx(ee,{children:C.members.map(a=>e.jsxs(g,{children:[e.jsx(l,{className:"font-medium",children:a.email}),e.jsx(l,{children:e.jsxs(se,{value:a.role,onValueChange:t=>we(a.user_id,t),children:[e.jsx(ae,{className:"w-32",children:e.jsx(ie,{})}),e.jsxs(le,{children:[e.jsx(b,{value:"owner",children:"Owner"}),e.jsx(b,{value:"admin",children:"Admin"}),e.jsx(b,{value:"member",children:"Member"})]})]})}),e.jsx(l,{className:"text-muted-foreground",children:a.joined_at?te(new Date(a.joined_at),"MMM d, yyyy"):"—"}),e.jsx(l,{children:e.jsx(r,{variant:"ghost",size:"icon",onClick:()=>Ne(a.user_id),children:e.jsx(es,{className:"w-4 h-4 text-destructive"})})})]},a.id))})]})}):e.jsx("div",{className:"text-center py-8 text-muted-foreground border rounded-lg",children:"No members in this company"})]})]})})})})]})},s.id))})]})}),e.jsx(E,{open:de,onOpenChange:f,children:e.jsxs(I,{className:"max-w-2xl",children:[e.jsxs(q,{children:[e.jsx(z,{children:D?"Edit Company":"Create New Company"}),e.jsx(R,{children:D?"Update company information and billing details.":"Create a new tenant company with full business details."})]}),e.jsx(ne,{className:"max-h-[60vh] pr-4",children:e.jsxs("div",{className:"space-y-6 py-4",children:[e.jsxs("div",{className:"space-y-4",children:[e.jsx("h4",{className:"font-medium",children:"Basic Information"}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2 md:col-span-2",children:[e.jsx(m,{htmlFor:"name",children:"Company Name *"}),e.jsx(o,{id:"name",placeholder:"Acme Design Studio",value:i.name,onChange:s=>d({...i,name:s.target.value})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(m,{htmlFor:"tax_id",children:"Tax/CVR Number"}),e.jsx(o,{id:"tax_id",placeholder:"DK12345678",value:i.tax_id,onChange:s=>d({...i,tax_id:s.target.value})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(m,{htmlFor:"billing_email",children:"Billing Email"}),e.jsx(o,{id:"billing_email",type:"email",placeholder:"[email protected]",value:i.billing_email,onChange:s=>d({...i,billing_email:s.target.value})})]})]})]}),e.jsxs("div",{className:"space-y-4",children:[e.jsx("h4",{className:"font-medium",children:"Contact Details"}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(m,{htmlFor:"phone",children:"Phone"}),e.jsx(o,{id:"phone",placeholder:"+45 12345678",value:i.phone,onChange:s=>d({...i,phone:s.target.value})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(m,{htmlFor:"website",children:"Website"}),e.jsx(o,{id:"website",placeholder:"https://company.com",value:i.website,onChange:s=>d({...i,website:s.target.value})})]})]})]}),e.jsxs("div",{className:"space-y-4",children:[e.jsx("h4",{className:"font-medium",children:"Billing Address"}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2 md:col-span-2",children:[e.jsx(m,{htmlFor:"billing_address",children:"Street Address"}),e.jsx(o,{id:"billing_address",placeholder:"123 Design Street",value:i.billing_address,onChange:s=>d({...i,billing_address:s.target.value})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(m,{htmlFor:"billing_city",children:"City"}),e.jsx(o,{id:"billing_city",placeholder:"Copenhagen",value:i.billing_city,onChange:s=>d({...i,billing_city:s.target.value})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(m,{htmlFor:"billing_postal_code",children:"Postal Code"}),e.jsx(o,{id:"billing_postal_code",placeholder:"2100",value:i.billing_postal_code,onChange:s=>d({...i,billing_postal_code:s.target.value})})]}),e.jsxs("div",{className:"space-y-2 md:col-span-2",children:[e.jsx(m,{htmlFor:"billing_country",children:"Country"}),e.jsx(o,{id:"billing_country",placeholder:"Denmark",value:i.billing_country,onChange:s=>d({...i,billing_country:s.target.value})})]})]})]})]})}),e.jsxs(B,{children:[e.jsx(r,{variant:"outline",onClick:()=>f(!1),children:"Cancel"}),e.jsx(r,{onClick:be,disabled:u.isPending,children:D?"Save Changes":"Create Company"})]})]})}),e.jsx(Re,{open:oe,onOpenChange:k,children:e.jsxs(Be,{children:[e.jsxs(Pe,{children:[e.jsx($e,{children:"Delete Company"}),e.jsxs(Le,{children:["Are you sure you want to delete ",e.jsx("span",{className:"font-medium",children:n?.name}
1),"? This action cannot be undone and will remove all company data, members, and credits."]})]}),e.jsxs(Ue,{children:[e.jsx(Ke,{children:"Cancel"}),e.jsx(Qe,{onClick:fe,className:"bg-destructive text-destructive-foreground hover:bg-destructive/90",children:"Delete Company"})]})]})}),e.jsx(E,{open:ce,onOpenChange:y,children:e.jsxs(I,{children:[e.jsxs(q,{children:[e.jsx(z,{children:"Adjust Company Credits"}),e.jsxs(R,{children:["Adjust credits for ",n?.name,". Current balance: ",n?.credits?.balance??0]})]}),e.jsxs("div",{className:"flex items-center justify-center gap-4 py-4",children:[e.jsx(r,{variant:"outline",size:"icon",onClick:()=>v(s=>s-10),children:e.jsx(ss,{className:"w-4 h-4"})}),e.jsx(o,{type:"number",value:p,onChange:s=>v(parseInt(s.target.value)||0),className:"w-24 text-center"}),e.jsx(r,{variant:"outline",size:"icon",onClick:()=>v(s=>s+10),children:e.jsx(F,{className:"w-4 h-4"})})]}),e.jsxs("p",{className:"text-center text-sm text-muted-foreground",children:["New balance: ",Math.max(0,(n?.credits?.balance??0)+p)]}),e.jsxs(B,{children:[e.jsx(r,{variant:"outline",onClick:()=>y(!1),children:"Cancel"}),e.jsxs(r,{onClick:ve,disabled:p===0,children:[p>=0?"Add":"Remove"," ",Math.abs(p)," Credits"]})]})]})}),e.jsx(E,{open:U,onOpenChange:_,children:e.jsxs(I,{children:[e.jsxs(q,{children:[e.jsxs(z,{children:["Add Member to ",n?.name]}),e.jsx(R,{children:"Select a user to add to this company and choose their role."})]}),e.jsxs("div",{className:"space-y-4 py-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(m,{children:"Role in Company"}),e.jsxs(se,{value:Q,onValueChange:s=>V(s),children:[e.jsx(ae,{children:e.jsx(ie,{})}),e.jsxs(le,{children:[e.jsx(b,{value:"owner",children:"Owner - Full access and billing"}),e.jsx(b,{value:"admin",children:"Admin - Can manage members"}),e.jsx(b,{value:"member",children:"Member - Standard access"})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(m,{children:"Select User"}),e.jsx(ne,{className:"h-[300px] border rounded-lg",children:G.length===0?e.jsx("div",{className:"p-4 text-center text-muted-foreground",children:"No available users to add"}):e.jsx("div",{className:"p-2 space-y-1",children:G.map(s=>e.jsxs("button",{onClick:()=>Ce(s.id),className:"w-full flex items-center justify-between p-3 rounded-lg hover:bg-muted transition-colors text-left",children:[e.jsxs("div",{children:[e.jsx("p",{className:"font-medium",children:s.email}),s.profile?.firm_name&&e.jsx("p",{className:"text-sm text-muted-foreground",children:s.profile.firm_name})]}),e.jsx(F,{className:"w-4 h-4 text-muted-foreground"})]},s.id))})})]})]}),e.jsx(B,{children:e.jsx(r,{variant:"outline",onClick:()=>_(!1),children:"Close"})})]})})]})};export{bs as default};

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