ToastContext-VFOrCCoA.js 8.1 KB

1
  1. import{i as e}from"./chunk-aKtaBQYM.js";import{C as t,D as n,w as r}from"./client-BdOyoZJz.js";import{t as i}from"./createLucideIcon-Yo2YoYjm.js";import{a,i as o,n as s,r as c,t as l}from"./info-DcT0QU8O.js";import{t as u}from"./loader-circle-CB4leTZa.js";import{t as d}from"./x-Dctff5l9.js";import{t as f}from"./file-DDkFXH-b.js";var p=i(`circle-check-big`,[[`path`,{d:`M21.801 10A10 10 0 1 1 17 3.335`,key:`yps3ct`}],[`path`,{d:`m9 11 3 3L22 4`,key:`1pflzl`}]]),m=e(n(),1),h=t(),g=(0,m.createContext)(void 0);function _(){let e=(0,m.useContext)(g);if(!e)throw Error(`useToast must be used within a ToastProvider`);return e}var v={success:(0,h.jsx)(p,{className:`w-5 h-5 text-green-400`}),error:(0,h.jsx)(s,{className:`w-5 h-5 text-red-400`}),warning:(0,h.jsx)(c,{className:`w-5 h-5 text-yellow-400`}),info:(0,h.jsx)(l,{className:`w-5 h-5 text-blue-400`}),loading:(0,h.jsx)(u,{className:`w-5 h-5 text-bambu-green animate-spin`})},y={success:`bg-green-500/10 border-green-500/30`,error:`bg-red-500/10 border-red-500/30`,warning:`bg-yellow-500/10 border-yellow-500/30`,info:`bg-blue-500/10 border-blue-500/30`,loading:`bg-bambu-green/10 border-bambu-green/30`},b=`background-dispatch`,x=3500,S=3e3,C=2*S,w=new Set([`error`,`warning`]);function T(e){return e.status===`processing`&&typeof e.uploadProgressPct==`number`&&e.uploadProgressPct>=99.9}function E(e){return{total:e.length,processing:e.filter(e=>e.status===`processing`).length,completed:e.filter(e=>e.status===`completed`).length,failed:e.filter(e=>e.status===`failed`).length,jobs:e}}function D({children:e}){let{t}=r(),[n,i]=(0,m.useState)([]),[s,c]=(0,m.useState)(!1),[l,u]=(0,m.useState)(!1),p=(0,m.useRef)(new Map),_=(0,m.useRef)(!0);(0,m.useEffect)(()=>{_.current=!0;let e=p.current;return()=>{_.current=!1,e.forEach(e=>clearTimeout(e)),e.clear()}},[]);let D=(0,m.useCallback)((e,t=`success`)=>{if(!_.current)return;let n=Math.random().toString(36).substr(2,9);i(r=>[...r,{id:n,message:e,type:t}]);let r=setTimeout(()=>{_.current&&(i(e=>e.filter(e=>e.id!==n)),p.current.delete(n))},w.has(t)?C:S);p.current.set(n,r)},[]),O=(0,m.useCallback)((e,t,n=`info`,r)=>{_.current&&i(i=>i.find(t=>t.id===e)?i.map(i=>i.id===e?{...i,message:t,type:n,persistent:!0,action:r?.action}:i):[...i,{id:e,message:t,type:n,persistent:!0,action:r?.action}])},[]),k=(0,m.useCallback)(e=>{if(!_.current)return;let t=p.current.get(e);t&&(clearTimeout(t),p.current.delete(e)),i(t=>t.filter(t=>t.id!==e))},[]);return(0,m.useEffect)(()=>{let e=e=>{if(!_.current)return;let n=e.detail;if(!n||typeof n.queue_item_id!=`number`)return;let r=n.queue_item_id;i(e=>{let i=e.find(e=>e.id===b),a=i?.dispatchData?.jobs??[],o=a.findIndex(e=>e.jobId===r),s=o>=0?a[o]:void 0,c=null,l=n.file_name||s?.sourceName||t(`dispatchToast.untitled`),u=n.printer_name||s?.printerName||(n.printer_id?`Printer ${n.printer_id}`:``);switch(n.type){case`queue_item_uploading`:c={jobId:r,sourceName:l,printerName:u,status:`processing`,uploadBytes:0,uploadTotalBytes:n.total_bytes,uploadProgressPct:0};break;case`queue_item_upload_progress`:if(!s)return e;c={...s,uploadBytes:n.bytes_transferred,uploadTotalBytes:n.total_bytes??s.uploadTotalBytes,uploadProgressPct:n.pct};break;case`queue_item_acked`:if(!s)return e;c={...s,status:`completed`,uploadProgressPct:100};break;case`queue_item_failed`:if(!s)return e;c={...s,status:`failed`,failReason:n.reason};break;default:return e}let d;s?(d=[...a],d[o]=c):d=[...a,c];let f=E(d),p={id:b,message:t(`dispatchToast.startingPrints`),type:`loading`,persistent:!0,dispatchData:f};return i?e.map(e=>e.id===b?p:e):[...e,p]})};return window.addEventListener(`bambuddy:dispatch-toast`,e),()=>window.removeEventListener(`bambuddy:dispatch-toast`,e)},[t]),(0,m.useEffect)(()=>{let e=n.find(e=>e.id===b);if(!e?.dispatchData)return;let t=e.dispatchData;if(t.total===0||t.processing!==0)return;let r=p.current.get(b);r&&clearTimeout(r);let a=setTimeout(()=>{_.current&&(i(e=>e.filter(e=>e.id!==b)),p.current.delete(b))},x);p.current.set(b,a)},[n]),(0,h.jsxs)(g.Provider,{value:{showToast:D,showPersistentToast:O,dismissToast:k,setViewportSuppressed:c},children:[e,(0,h.jsx)(`div`,{"data-testid":`toast-viewport`,className:`fixed z-[60] flex flex-col items-end gap-2 ${s?`hidden`:``}`,style:{bottom:`calc(1rem + env(safe-area-inset-bottom))`,right:`calc(5rem + env(safe-area-inset-right))`},children:n.map(e=>(0,h.jsx)(`div`,{className:`rounded-lg border shadow-lg backdrop-blur-sm animate-slide-in ${y[e.type]} ${e.dispatchData?`w-[420px] p-3`:`flex items-center gap-3 px-4 py-3`}`,style:{maxWidth:`calc(100vw - 6rem - env(safe-area-inset-left) - env(safe-area-inset-right))`},"data-testid":e.dispatchData?`dispatch-toast-wrapper`:void 0,children:e.dispatchData?(0,h.jsxs)(h.Fragment,{children:[(0,h.jsxs)(`div`,{className:`flex items-start justify-between gap-3`,children:[(0,h.jsxs)(`div`,{className:`flex items-start gap-2`,children:[v[e.type],(0,h.jsxs)(`div`,{children:[(0,h.jsx)(`p`,{className:`text-white text-sm font-medium`,children:t(`dispatchToast.startingPrints`)}),(0,h.jsx)(`p`,{className:`text-xs text-bambu-gray mt-0.5`,children:t(`dispatchToast.progressSummary`,{complete:e.dispatchData.completed+e.dispatchData.failed,total:e.dispatchData.total,processing:e.dispatchData.processing})})]})]}),(0,h.jsxs)(`div`,{className:`flex items-center gap-1`,children:[(0,h.jsx)(`button`,{onClick:()=>u(e=>!e),className:`text-bambu-gray hover:text-white transition-colors`,"aria-label":t(l?`dispatchToast.expandDetails`:`dispatchToast.collapseDetails`),"data-testid":`dispatch-toast-collapse`,children:l?(0,h.jsx)(o,{className:`w-4 h-4`}):(0,h.jsx)(a,{className:`w-4 h-4`})}),(0,h.jsx)(`button`,{onClick:()=>k(e.id),className:`text-bambu-gray hover:text-white transition-colors`,"aria-label":t(`dispatchToast.dismiss`),"data-testid":`dispatch-toast-dismiss`,children:(0,h.jsx)(d,{className:`w-4 h-4`})})]})]}),!l&&(0,h.jsx)(`div`,{className:`mt-3 space-y-2 max-h-64 overflow-y-auto pr-1`,children:e.dispatchData.jobs.map(e=>{let n=T(e);return(0,h.jsxs)(`div`,{className:`rounded border border-white/10 bg-black/15 p-2`,"data-testid":`dispatch-toast-job-${e.jobId}`,children:[(0,h.jsxs)(`div`,{className:`flex items-center justify-between gap-2`,children:[(0,h.jsx)(`span`,{className:`text-xs text-white truncate min-w-0 flex-1`,title:e.sourceName,children:e.sourceName}),(0,h.jsx)(`span`,{className:`text-[11px] uppercase tracking-wide text-bambu-gray shrink-0`,"data-testid":`dispatch-toast-status-${e.jobId}`,children:t(`dispatchToast.status.${e.status}`)})]}),e.printerName&&(0,h.jsx)(`div`,{className:`text-[11px] text-bambu-gray truncate`,title:e.printerName,children:e.printerName}),e.status===`processing`?n?(0,h.jsx)(`div`,{className:`text-[11px] text-bambu-gray truncate`,children:t(`dispatchToast.awaitingPrinter`)}):typeof e.uploadBytes==`number`&&typeof e.uploadTotalBytes==`number`&&e.uploadTotalBytes>0?(0,h.jsxs)(`div`,{className:`text-[11px] text-bambu-gray truncate`,children:[f(e.uploadBytes),` / `,f(e.uploadTotalBytes),typeof e.uploadProgressPct==`number`?` (${e.uploadProgressPct.toFixed(1)}%)`:``]}):null:e.status===`failed`&&e.failReason?(0,h.jsx)(`div`,{className:`text-[11px] text-red-400 truncate`,children:t(`dispatchToast.failed.${e.failReason}`,{defaultValue:t(`dispatchToast.failed.generic`)})}):null,(0,h.jsx)(`div`,{className:`mt-1 h-1.5 w-full rounded bg-white/10 overflow-hidden`,children:(0,h.jsx)(`div`,{className:`h-full ${{processing:`bg-bambu-green`,completed:`bg-green-500`,failed:`bg-red-500`}[e.status]} transition-all duration-300 ${n?`animate-pulse`:``}`,style:{width:`${e.status===`processing`&&typeof e.uploadProgressPct==`number`?Math.max(0,Math.min(100,e.uploadProgressPct)):{processing:60,completed:100,failed:100}[e.status]}%`}})})]},e.jobId)})})]}):(0,h.jsxs)(h.Fragment,{children:[v[e.type],(0,h.jsx)(`span`,{className:`text-white text-sm`,children:e.message}),e.action&&(0,h.jsx)(`a`,{href:e.action.href,target:`_blank`,rel:`noopener noreferrer`,onClick:()=>{e.action?.onClick?.(),k(e.id)},className:`ml-2 px-2 py-1 rounded text-xs font-medium bg-bambu-green/20 text-bambu-green hover:bg-bambu-green/30 whitespace-nowrap`,children:e.action.label}),(0,h.jsx)(`button`,{onClick:()=>k(e.id),className:`ml-2 text-bambu-gray hover:text-white transition-colors`,children:(0,h.jsx)(d,{className:`w-4 h-4`})})]})},e.id))})]})}export{_ as n,p as r,D as t};