"use strict";(self.webpackChunkgrafana=self.webpackChunkgrafana||[]).push([[3278],{13278:(ee,p,a)=>{a.r(p),a.d(p,{AddToDashboard:()=>J});var t=a(65237),F=a(36011),i=a(3315),D=a(12521),T=a(89949),B=a(89822),R=a(85428),y=a(28704),P=a(38002),H=a(49784),A=a(90931),S=a(58102),C=a(5263),L=a(12007),N=a(88347),m=a(48389),V=a(70786),h=a(96237),b=a(22047),w=a(82556),f=(e=>(e.FETCH_DASHBOARD="fetch-dashboard",e.SET_DASHBOARD_LS="set-dashboard-ls-error",e))(f||{});function G(){const e=(0,b.ZQ)();return e.dashboard.panels=[],e}async function z(e){const s=W(e.queries,e.queryResponse),n={targets:e.queries,type:s,title:"New Panel",gridPos:{x:0,y:0,w:12,h:8},datasource:e.datasource};let r;if(e.dashboardUid)try{r=await w.ae.getDashboardByUid(e.dashboardUid)}catch{throw"fetch-dashboard"}else r=G();r.dashboard.panels=[n,...r.dashboard.panels??[]];try{(0,b.$M)(r)}catch{throw"set-dashboard-ls-error"}}const M=e=>!e.hide,Q=e=>s=>s.refId===e;function W(e,s){for(const{refId:n}of e.filter(M)){const r=Q(n);if(s.flameGraphFrames.some(r))return"flamegraph";if(s.graphFrames.some(r))return"timeseries";if(s.logsFrames.some(r))return"logs";if(s.nodeGraphFrames.some(r))return"nodeGraph";if(s.traceFrames.some(r))return"traces"}return"table"}var $=(e=>(e.NewDashboard="new-dashboard",e.ExistingDashboard="existing-dashboard",e))($||{});function ae(e){}function K(e){return e?`d/${e}`:"dashboard/new"}var Z=(e=>(e.UNKNOWN="unknown-error",e.NAVIGATION="navigation-error",e))(Z||{});const j=({onClose:e,exploreId:s})=>{const n=(0,i.useSelector)((0,D.F)(s)),[r,o]=(0,t.useState)(),{handleSubmit:x,control:O,formState:{errors:v},watch:X}=(0,B.cI)({defaultValues:{saveTarget:"new-dashboard"}}),Y=h.Vt.hasAccess(i.AccessControlAction.DashboardsCreate,h.Vt.isEditor),k=h.Vt.hasAccess(i.AccessControlAction.DashboardsWrite,h.Vt.isEditor),d=[];Y&&d.push({label:"New dashboard",value:"new-dashboard"}),k&&d.push({label:"Existing dashboard",value:"existing-dashboard"});const q=d.length>1?X("saveTarget"):d[0].value,_=`Add panel to ${d.length>1?"dashboard":d[0].label.toLowerCase()}`,I=async(c,l)=>{o(void 0);const u=l.saveTarget==="existing-dashboard"?l.dashboardUid:void 0;(0,y.ff)("e_2_d_submit",{newTab:c,saveTarget:l.saveTarget,queries:n.queries.length});try{await z({dashboardUid:u,datasource:n.datasourceInstance?.getRef(),queries:n.queries,queryResponse:n.queryResponse})}catch(E){switch(E){case f.FETCH_DASHBOARD:o({error:E,message:"Could not fetch dashboard information. Please try again."});break;case f.SET_DASHBOARD_LS:o({error:E,message:"Could not add panel to dashboard. Please try again."});break;default:o({error:"unknown-error",message:"Something went wrong. Please try again."})}return}const g=K(u);if(!c){e(),P.E1.push(R.u.stripBaseFromUrl(g));return}if(!!!a.g.open(H.v.appUrl+g,"_blank")){o({error:"navigation-error",message:"Could not navigate to the selected dashboard. Please try again."}),(0,b.f1)();return}e()};return(0,t.useEffect)(()=>{(0,y.ff)("e_2_d_open")},[]),t.createElement(A.u,{title:_,onDismiss:e,isOpen:!0},t.createElement("form",null,d.length>1&&t.createElement(S.g,{control:O,render:({field:{ref:c,...l}})=>t.createElement(C.g,{label:"Target dashboard",description:"Choose where to add the panel."},t.createElement(L.S,{options:d,...l,id:"e2d-save-target"})),name:"saveTarget"}),q==="existing-dashboard"&&(()=>t.createElement(S.g,{render:({field:{ref:c,value:l,onChange:u,...g}})=>t.createElement(C.g,{label:"Dashboard",description:"Select in which dashboard the panel will be created.",error:v.dashboardUid?.message,invalid:!!v.dashboardUid},t.createElement(V.o,{...g,inputId:"e2d-dashboard-picker",defaultOptions:!0,onChange:U=>u(U?.uid)})),control:O,name:"dashboardUid",shouldUnregister:!0,rules:{required:{value:!0,message:"This field is required."}}}))(),r&&t.createElement(N.b,{severity:"error",title:"Error adding the panel"},r.message),t.createElement(A.u.ButtonRow,null,t.createElement(m.zx,{type:"reset",onClick:e,fill:"outline",variant:"secondary"},"Cancel"),t.createElement(m.zx,{type:"submit",variant:"secondary",onClick:x((0,T.partial)(I,!0)),icon:"external-link-alt"},"Open in new tab"),t.createElement(m.zx,{type:"submit",variant:"primary",onClick:x((0,T.partial)(I,!1)),icon:"apps"},"Open dashboard"))))},J=({exploreId:e})=>{const[s,n]=(0,t.useState)(!1),r=(0,D.F)(e),o=!!(0,i.useSelector)(r)?.queries?.length;return t.createElement(t.Fragment,null,t.createElement(F.h,{icon:"apps",variant:"canvas",onClick:()=>n(!0),"aria-label":"Add to dashboard",disabled:!o},"Add to dashboard"),s&&t.createElement(j,{onClose:()=>n(!1),exploreId:e}))}}}]);

//# sourceMappingURL=3278.a162addc27a3f42fb5ea.js.map