// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0-or-later // @source: https://gitlab.com/soapbox-pub/ditto/-/tree/f0a3877c import{r as e}from"./rolldown-runtime-QTnfLwEv.js";import{$ as t,N as n,_n as r,a as i,jn as a,qi as o}from"./lucide-icons-BdRF0moN.js";import{t as s}from"./jsx-runtime-By8HlURe.js";import{t as ee}from"./dist-DQ82bf9M.js";import{h as te}from"./useQuery-rCAlChE8.js";import{t as c}from"./fetchFreshEvent-Kkz3MzZN.js";import{s as ne,t as re}from"./useCurrentUser-C6OJJlWo.js";import{t as l}from"./utils-BFSTDHdM.js";import{t as u}from"./button-Cs26nq0r.js";import{t as ie}from"./useNostrPublish-CrMjnYm4.js";import{t as d}from"./CustomEmoji-DRcU63lJ.js";import{t as f}from"./input-CM5Fnyvy.js";import{t as ae}from"./useUploadFile-BpPXwoE0.js";import{a as p,c as m,n as h,r as g,t as _}from"./dialog-C3CFmras.js";import{t as oe}from"./textarea-B93xm6Zg.js";import{t as v}from"./label-BBIj2Nkw.js";import{t as y}from"./checkbox-CM1MZiSJ.js";import{t as b}from"./scroll-area-D2CmkWvj.js";var x=e(o(),1),S=s();function C(e){return e.toLowerCase().replace(/[^a-z0-9_]+/g,`_`).slice(0,32)}function w(e){return e.replace(/^_+|_+$/g,``)}function se(e){return w(C(e.replace(/\.[a-z0-9]+$/i,``)))}function ce(e){return e.trim().toLowerCase().replace(/[^a-z0-9]+/g,`-`).replace(/^-+|-+$/g,``)||`pack`}var T=0;function E(){return`entry-${++T}-${Date.now()}`}async function le(e){let t=e.dataTransfer.items;if(!t||t.length===0)return Array.from(e.dataTransfer.files);let n=[],r=[];for(let e=0;enew Promise(n=>{e.isFile?e.file(e=>{t.push(e),n()},()=>n()):e.isDirectory?e.createReader().readEntries(async e=>{await Promise.all(e.map(e=>i(e,t))),n()},()=>n()):n()}),a=[...r];return await Promise.all(n.map(e=>i(e,a))),a}function D({open:e,onOpenChange:n,editEvent:r}){return(0,S.jsx)(_,{open:e,onOpenChange:n,children:(0,S.jsxs)(h,{className:`sm:max-w-lg gap-0 p-0 overflow-hidden`,children:[(0,S.jsxs)(p,{className:`px-5 pt-5 pb-3`,children:[(0,S.jsxs)(m,{className:`flex items-center gap-2`,children:[(0,S.jsx)(t,{className:`size-5 text-primary`}),r?`Edit Emoji Pack`:`Create Emoji Pack`]}),(0,S.jsx)(g,{children:`Upload images, give each a shortcode, and publish a pack anyone can add.`})]}),e&&(0,S.jsx)(O,{editEvent:r,onDone:()=>n(!1)})]})})}function O({editEvent:e,onDone:t}){let{user:o}=re(),{nostr:s}=ee(),{mutateAsync:p,isPending:m}=ie(),{mutateAsync:h}=ae(),g=te(),{toast:_}=ne(),T=!!e,D=(0,x.useMemo)(()=>{if(!e)return null;let t=t=>e.tags.find(([e])=>e===t)?.[1],n=[];for(let t of e.tags)t[0]===`emoji`&&t[1]&&t[2]&&n.push({id:E(),shortcode:t[1],url:t[2],uploading:!1});return{identifier:t(`d`)??``,name:t(`name`)??t(`title`)??``,about:t(`about`)??``,icon:t(`image`)??t(`picture`)??``,emojis:n}},[e]),O=(0,x.useRef)(null),k=(0,x.useRef)(null),[A,j]=(0,x.useState)(D?.name??``),[M,ue]=(0,x.useState)(D?.about??``),[N,de]=(0,x.useState)(D?.icon??``),[P,F]=(0,x.useState)(!1),[I,L]=(0,x.useState)(D?.emojis??[]),[R,z]=(0,x.useState)(!1),[B,V]=(0,x.useState)(!T),[H,U]=(0,x.useState)(!1),fe=(0,x.useCallback)(async e=>{if(!(!e||!e.type.startsWith(`image/`))){F(!0);try{let t=await h(e);de(t[0]?.[1]??``)}catch{_({title:`Icon upload failed`,description:e.name,variant:`destructive`})}finally{F(!1)}}},[h,_]),W=(0,x.useCallback)(async e=>{if(!e)return;let t=Array.from(e).filter(e=>e.type.startsWith(`image/`));if(t.length===0){_({title:`No images`,description:`No image files found.`,variant:`destructive`});return}await Promise.all(t.map(async e=>{let t=E();L(n=>[...n,{id:t,shortcode:se(e.name),url:``,uploading:!0}]);try{let n=(await h(e))[0]?.[1]??``;L(e=>e.map(e=>e.id===t?{...e,url:n,uploading:!1}:e))}catch{L(e=>e.filter(e=>e.id!==t)),_({title:`Upload failed`,description:e.name,variant:`destructive`})}}))},[h,_]),pe=(0,x.useCallback)((e,t)=>{L(n=>n.map(n=>n.id===e?{...n,shortcode:C(t)}:n))},[]),me=(0,x.useCallback)(e=>{L(t=>t.filter(t=>t.id!==e))},[]),G=(0,x.useCallback)(e=>{e.preventDefault(),z(!1),le(e).then(W)},[W]),K=I.filter(e=>e.uploading).length,q=(0,x.useMemo)(()=>I.filter(e=>!e.uploading&&e.url),[I]),J=(0,x.useMemo)(()=>{let e=new Map;for(let t of q){let n=w(t.shortcode);n&&e.set(n,(e.get(n)??0)+1)}return e},[q]),he=e=>(J.get(w(e))??0)>1,Y=[...J.values()].filter(e=>e>1).length,X=q.filter(e=>!w(e.shortcode)).length,Z=A.trim().length>0,Q=Z&&q.length>0&&!K&&!P&&!m&&!H&&Y===0&&X===0,$=m||H,ge=$?`Publishing…`:K>0||P?`Uploading ${K+ +!!P} image${K+ +!!P===1?``:`s`}…`:X>0?`Every emoji needs a shortcode.`:Y>0?`Two emojis share a shortcode. Make each one unique.`:Z?q.length===0?`Add at least one image.`:`Anyone will be able to find and add this pack.`:`Give the pack a name.`,_e=(0,x.useCallback)(async()=>{if(!o||!Q)return;U(!0);let e=T?D.identifier:`${ce(A)}-${Math.random().toString(36).slice(2,6)}`;try{let r=[],i=null;T&&(i=await c(s,{kinds:[30030],authors:[o.pubkey],"#d":[e]}),i&&(r=i.tags.filter(([e])=>![`d`,`name`,`title`,`about`,`image`,`picture`,`emoji`].includes(e))));let a=[[`d`,e],[`title`,A.trim()],[`name`,A.trim()]];M.trim()&&a.push([`about`,M.trim()]),N&&a.push([`image`,N],[`picture`,N]),a.push(...r);for(let e of q)a.push([`emoji`,w(e.shortcode),e.url]);if(await p({kind:30030,content:``,tags:a,prev:i??void 0}),g.invalidateQueries({queryKey:[`feed`]}),g.invalidateQueries({queryKey:[`custom-emojis`]}),g.invalidateQueries({queryKey:[`emoji-list`]}),g.invalidateQueries({queryKey:[`my-published-packs`]}),g.invalidateQueries({queryKey:[`emoji-pack-index`]}),B&&!T)try{await n(e),g.invalidateQueries({queryKey:[`emoji-list`]}),g.invalidateQueries({queryKey:[`custom-emojis`]}),g.invalidateQueries({queryKey:[`my-emoji-packs`]}),_({title:`Emoji pack published`,description:`${A.trim()} — added to your emojis`})}catch{_({title:`Published, but not added to your emojis`,description:`Couldn't update your emoji list.`,variant:`destructive`})}else _({title:T?`Emoji pack updated`:`Emoji pack published`,description:A.trim()});t()}catch{_({title:`Couldn't publish pack`,description:`Publishing failed. Please try again.`,variant:`destructive`})}finally{U(!1)}async function n(e){let t=await c(s,{kinds:[10030],authors:[o.pubkey]}),n=`30030:${o.pubkey}:${e}`,r=t?.tags.filter(([e])=>e===`emoji`||e===`a`)??[];r.some(([e,t])=>e===`a`&&t===n)||await p({kind:10030,content:t?.content??``,tags:[...r,[`a`,n]],prev:t??void 0})}},[o,Q,T,D,A,M,N,q,B,s,p,g,_,t]);return o?(0,S.jsx)(b,{className:`max-h-[70vh]`,children:(0,S.jsxs)(`div`,{className:`px-5 pb-5 space-y-5`,children:[(0,S.jsx)(`input`,{ref:k,type:`file`,accept:`image/*`,className:`hidden`,onChange:e=>{fe(e.target.files?.[0]),e.target.value=``}}),(0,S.jsxs)(`div`,{className:`flex items-end gap-3`,children:[(0,S.jsx)(`button`,{type:`button`,onClick:()=>k.current?.click(),"aria-label":`Pack icon`,disabled:$,className:l(`flex size-14 shrink-0 items-center justify-center overflow-hidden rounded-lg border transition-colors`,N?`border-transparent`:`border-dashed border-border text-muted-foreground hover:border-muted-foreground/60 hover:bg-foreground/5`),children:P?(0,S.jsx)(r,{className:`size-5 animate-spin text-muted-foreground`}):N?(0,S.jsx)(`img`,{src:N,alt:``,className:`size-full object-cover`,decoding:`async`}):(0,S.jsx)(a,{className:`size-5`})}),(0,S.jsxs)(`div`,{className:`flex-1 space-y-1.5`,children:[(0,S.jsx)(v,{htmlFor:`pack-name`,children:`Pack name`}),(0,S.jsx)(f,{id:`pack-name`,value:A,onChange:e=>j(e.target.value),placeholder:`My emoji pack`,maxLength:60,disabled:$,autoFocus:!0})]})]}),(0,S.jsxs)(`div`,{className:`space-y-1.5`,children:[(0,S.jsxs)(v,{htmlFor:`pack-about`,children:[`Description `,(0,S.jsx)(`span`,{className:`text-muted-foreground font-normal`,children:`(optional)`})]}),(0,S.jsx)(oe,{id:`pack-about`,value:M,onChange:e=>ue(e.target.value),placeholder:`What's in this pack?`,maxLength:280,rows:2,disabled:$,className:`resize-none text-sm`})]}),(0,S.jsx)(`input`,{ref:O,type:`file`,accept:`image/*`,multiple:!0,className:`hidden`,onChange:e=>{W(e.target.files),e.target.value=``}}),(0,S.jsxs)(`div`,{className:`space-y-2`,children:[(0,S.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,S.jsx)(v,{children:`Emojis`}),q.length>0&&(0,S.jsx)(`span`,{className:`text-xs text-muted-foreground`,children:q.length}),I.length>0&&(0,S.jsxs)(u,{type:`button`,variant:`ghost`,size:`sm`,className:`ml-auto h-7 gap-1.5 text-xs text-muted-foreground`,onClick:()=>O.current?.click(),disabled:$,children:[(0,S.jsx)(a,{className:`size-3.5`}),`Add more`]})]}),I.length===0?(0,S.jsxs)(`button`,{type:`button`,onClick:()=>O.current?.click(),onDragOver:e=>{e.preventDefault(),z(!0)},onDragLeave:()=>z(!1),onDrop:G,className:l(`flex w-full flex-col items-center gap-2 rounded-xl border border-dashed px-6 py-8 text-center transition-colors`,R?`border-primary bg-primary/10 text-foreground`:`border-border text-muted-foreground hover:border-muted-foreground/60 hover:bg-foreground/5`),children:[(0,S.jsx)(a,{className:`size-6`}),(0,S.jsx)(`span`,{className:`text-sm font-medium text-foreground`,children:`Add emoji images`}),(0,S.jsx)(`span`,{className:`text-xs`,children:`Drop them here or a folder, or click to browse. PNG, GIF or WebP.`})]}):(0,S.jsx)(`div`,{onDragOver:e=>{e.preventDefault(),z(!0)},onDragLeave:()=>z(!1),onDrop:G,className:l(`max-h-56 space-y-1.5 overflow-y-auto rounded-xl p-1.5 transition-colors`,R?`bg-primary/10`:`bg-secondary/30`),children:I.map(e=>{let t=!e.uploading&&(!w(e.shortcode)||he(e.shortcode));return(0,S.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,S.jsx)(`span`,{className:`flex size-9 shrink-0 items-center justify-center overflow-hidden rounded-md bg-background`,children:e.uploading?(0,S.jsx)(r,{className:`size-4 animate-spin text-muted-foreground`}):(0,S.jsx)(d,{name:e.shortcode,url:e.url,className:`size-7 object-contain`})}),(0,S.jsxs)(`div`,{className:l(`flex min-w-0 flex-1 items-center rounded-md border bg-background px-2 focus-within:ring-1`,t?`border-destructive focus-within:ring-destructive`:`border-input focus-within:ring-ring`),children:[(0,S.jsx)(`span`,{className:`text-sm text-muted-foreground`,children:`:`}),(0,S.jsx)(`input`,{value:e.shortcode,onChange:t=>pe(e.id,t.target.value),placeholder:`shortcode`,"aria-label":`Emoji shortcode`,"aria-invalid":t,disabled:$,className:`min-w-0 flex-1 bg-transparent py-1.5 text-sm font-mono outline-none`}),(0,S.jsx)(`span`,{className:`text-sm text-muted-foreground`,children:`:`})]}),(0,S.jsx)(u,{type:`button`,size:`icon`,variant:`ghost`,className:`size-8 shrink-0 text-muted-foreground hover:text-destructive`,onClick:()=>me(e.id),"aria-label":`Remove ${e.shortcode||`emoji`}`,disabled:$,children:(0,S.jsx)(i,{className:`size-4`})})]},e.id)})})]}),(0,S.jsxs)(`div`,{className:`space-y-2`,children:[!T&&(0,S.jsxs)(v,{className:`flex cursor-pointer items-center gap-2 py-1 text-sm font-normal text-muted-foreground`,children:[(0,S.jsx)(y,{checked:B,onCheckedChange:e=>V(e===!0),className:`shrink-0`,disabled:$}),`Add to my emojis`]}),(0,S.jsxs)(u,{className:`w-full gap-2`,onClick:_e,disabled:!Q,children:[$&&(0,S.jsx)(r,{className:`size-4 animate-spin`}),T?`Update pack`:`Publish pack`]}),(0,S.jsxs)(`p`,{className:l(`flex items-center justify-center gap-1.5 text-center text-xs`,Y>0||X>0?`text-destructive`:`text-muted-foreground`),children:[(Y>0||X>0)&&(0,S.jsx)(n,{className:`size-3.5 shrink-0`}),ge]})]})]})}):null}export{D as EmojiPackDialog}; // @license-end