"use strict";(self.webpackChunkgrafana=self.webpackChunkgrafana||[]).push([[7885],{77885:(e,a,r)=>{r.r(a),r.d(a,{AddToDashboard:()=>j});var t=r(68404),s=r(18745),o=r(69783),n=r(16479),d=r(82897),i=r(80916),l=r(43215),c=r(90923),u=r(49248),h=r(47074),b=r(28659);let g;async function p(e){var a;const r=function(e,a){for(const{refId:r}of e.filter(f)){const e=v(r);if(a.graphFrames.some(e))return"timeseries";if(a.logsFrames.some(e))return"logs";if(a.nodeGraphFrames.some(e))return"nodeGraph";if(a.traceFrames.some(e))return"traces"}return"table"}(e.queries,e.queryResponse),t={targets:e.queries,type:r,title:"New Panel",gridPos:{x:0,y:0,w:12,h:8},datasource:e.datasource};let s;if(e.dashboardUid)try{s=await b.ae.getDashboardByUid(e.dashboardUid)}catch(e){throw g.FETCH_DASHBOARD}else s=function(){const e=(0,h.ZQ)();return e.dashboard.panels=[],e}();s.dashboard.panels=[t,...null!==(a=s.dashboard.panels)&&void 0!==a?a:[]];try{(0,h.$M)(s)}catch{throw g.SET_DASHBOARD_LS}}!function(e){e.FETCH_DASHBOARD="fetch-dashboard",e.SET_DASHBOARD_LS="set-dashboard-ls-error"}(g||(g={}));const f=e=>!e.hide,v=e=>a=>a.refId===e;var m=r(45916);const x=["ref"],w=["ref","value","onChange"];function y(e,a){if(null==e)return{};var r,t,s={},o=Object.keys(e);for(t=0;t=0||(s[r]=e[r]);return s}var D;!function(e){e.NewDashboard="new-dashboard",e.ExistingDashboard="existing-dashboard"}(D||(D={}));const A=[{label:"New dashboard",value:D.NewDashboard},{label:"Existing dashboard",value:D.ExistingDashboard}];var C;!function(e){e.UNKNOWN="unknown-error",e.NAVIGATION="navigation-error"}(C||(C={}));const S=e=>{let{onClose:a,exploreId:b}=e;const f=(0,s.useSelector)((0,n.F)(b)),[v,S]=(0,t.useState)(),{handleSubmit:j,control:T,formState:{errors:O},watch:I}=(0,i.cI)({defaultValues:{saveTarget:D.NewDashboard}}),k=I("saveTarget"),N=async(e,t)=>{S(void 0);const s=t.saveTarget===D.ExistingDashboard?t.dashboardUid:void 0;(0,c.reportInteraction)("e2d_submit",{newTab:e,saveTarget:t.saveTarget,queries:f.queries.length});try{var o;await p({dashboardUid:s,datasource:null===(o=f.datasourceInstance)||void 0===o?void 0:o.getRef(),queries:f.queries,queryResponse:f.queryResponse})}catch(e){switch(e){case g.FETCH_DASHBOARD:S({error:e,message:"Could not fetch dashboard information. Please try again."});break;case g.SET_DASHBOARD_LS:S({error:e,message:"Could not add panel to dashboard. Please try again."});break;default:S({error:C.UNKNOWN,message:"Something went wrong. Please try again."})}return}const n=function(e){return e?`d/${e}`:"dashboard/new"}(s);if(!e)return a(),void c.locationService.push(l.locationUtil.stripBaseFromUrl(n));if(!!!r.g.open(c.config.appUrl+n,"_blank"))return S({error:C.NAVIGATION,message:"Could not navigate to the selected dashboard. Please try again."}),void(0,h.f1)();a()};return(0,t.useEffect)((()=>{(0,c.reportInteraction)("e2d_open")}),[]),(0,m.jsx)(o.Modal,{title:"Add panel to dashboard",onDismiss:a,isOpen:!0,children:(0,m.jsxs)("form",{children:[(0,m.jsx)(o.InputControl,{control:T,render:e=>{let{}=e,a=y(e.field,x);return(0,m.jsx)(o.Field,{label:"Target dashboard",description:"Choose where to add the panel.",children:(0,m.jsx)(o.RadioButtonGroup,Object.assign({options:A},a,{id:"e2d-save-target"}))})},name:"saveTarget"}),k===D.ExistingDashboard&&(0,m.jsx)(o.InputControl,{render:e=>{var a;let{field:{onChange:r}}=e,t=y(e.field,w);return(0,m.jsx)(o.Field,{label:"Dashboard",description:"Select in which dashboard the panel will be created.",error:null===(a=O.dashboardUid)||void 0===a?void 0:a.message,invalid:!!O.dashboardUid,children:(0,m.jsx)(u.o,Object.assign({},t,{inputId:"e2d-dashboard-picker",defaultOptions:!0,onChange:e=>r(null==e?void 0:e.uid)}))})},control:T,name:"dashboardUid",shouldUnregister:!0,rules:{required:{value:!0,message:"This field is required."}}}),v&&(0,m.jsx)(o.Alert,{severity:"error",title:"Error adding the panel",children:v.message}),(0,m.jsxs)(o.Modal.ButtonRow,{children:[(0,m.jsx)(o.Button,{type:"reset",onClick:a,fill:"outline",variant:"secondary",children:"Cancel"}),(0,m.jsx)(o.Button,{type:"submit",variant:"secondary",onClick:j((0,d.partial)(N,!0)),icon:"external-link-alt",children:"Open in new tab"}),(0,m.jsx)(o.Button,{type:"submit",variant:"primary",onClick:j((0,d.partial)(N,!1)),icon:"apps",children:"Open dashboard"})]})]})})},j=e=>{var a,r;let{exploreId:d}=e;const[i,l]=(0,t.useState)(!1),c=(0,n.F)(d),u=!(null===(a=(0,s.useSelector)(c))||void 0===a||null===(r=a.queries)||void 0===r||!r.length);return(0,m.jsxs)(m.Fragment,{children:[(0,m.jsx)(o.ToolbarButton,{icon:"apps",onClick:()=>l(!0),"aria-label":"Add to dashboard",disabled:!u,children:"Add to dashboard"}),i&&(0,m.jsx)(S,{onClose:()=>l(!1),exploreId:d})]})}}}]); //# sourceMappingURL=7885.0d0ec086d97a86ba0d52.js.map