diff --git a/web/src/App.css b/web/src/App.css index fc23446..5573d69 100644 --- a/web/src/App.css +++ b/web/src/App.css @@ -1198,6 +1198,42 @@ text-decoration: underline; } +.ts-server-icons-dialog { + width: 560px; +} + +.ts-server-icons-grid { + display: grid; + grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); + gap: 0.6rem; + max-height: 420px; + overflow-y: auto; + padding: 0.25rem; +} + +.ts-server-icons-tile { + display: flex; + flex-direction: column; + align-items: center; + gap: 0.25rem; + padding: 0.4rem; + border: 1px solid var(--border-soft); + border-radius: 4px; + font-size: 11px; +} + +.ts-server-icons-tile img, +.ts-server-icons-tile-loading { + width: 32px; + height: 32px; + object-fit: contain; +} + +.ts-server-icons-tile-loading { + background: var(--border-soft); + border-radius: 2px; +} + .ts-server-protocol-log-line { padding: 0.15rem 0.4rem; border-bottom: 1px solid var(--border-soft); diff --git a/web/src/App.tsx b/web/src/App.tsx index a7ff78e..c5ecbe1 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -1847,6 +1847,155 @@ function FileBrowserDialog({ ); } +/** Sniffs an image MIME type from decoded bytes so uploaded icons (which can + * be PNG/GIF/JPEG/BMP) render correctly as data URIs regardless of format. */ +function sniffImageMime(binary: string): string { + if (binary.startsWith("\x89PNG")) return "image/png"; + if (binary.startsWith("GIF87a") || binary.startsWith("GIF89a")) return "image/gif"; + if (binary.charCodeAt(0) === 0xff && binary.charCodeAt(1) === 0xd8) return "image/jpeg"; + if (binary.startsWith("BM")) return "image/bmp"; + return "image/png"; +} + +function iconDataUrl(base64: string): string { + return `data:${sniffImageMime(atob(base64))};base64,${base64}`; +} + +/** The icon repository lives under TS3's special "channel 0" - not a real, + * selectable channel, just the server-wide filebase icons are stored in. + * Icon files are always named "icon_", where is what a server/ + * channel/group's icon field references. Reuses the same ftlist/download/ + * upload/delete plumbing as the per-channel file browser. */ +function ServerIconsDialog({ + entries, + images, + onUpload, + onDelete, + onClose, +}: { + entries: FileListEntry[] | null; + images: Record; + onUpload: (iconId: number, file: File) => void; + onDelete: (entry: FileListEntry) => void; + onClose: () => void; +}) { + const t = useT(); + const backdrop = useBackdropDismiss(onClose); + const [timedOut, setTimedOut] = useState(false); + const fileInputRef = useRef(null); + const [pendingFile, setPendingFile] = useState(null); + const [newIconId, setNewIconId] = useState(""); + + useEffect(() => { + setTimedOut(false); + if (entries) return; + const id = window.setTimeout(() => setTimedOut(true), 6000); + return () => window.clearTimeout(id); + }, [entries]); + + const nextFreeId = () => { + const ids = (entries ?? []) + .map((e) => parseInt(e.name.replace("icon_", ""), 10)) + .filter((n) => !Number.isNaN(n)); + return (ids.length ? Math.max(...ids) : 99) + 1; + }; + + return ( +
+
e.stopPropagation()}> +
+ {t("serverIcons.title")} + +
+
+
+ + { + const file = e.target.files?.[0]; + e.target.value = ""; + if (file) setPendingFile(file); + }} + /> +
+ {pendingFile && ( +
{ + e.preventDefault(); + const id = parseInt(newIconId, 10); + if (!Number.isNaN(id) && pendingFile) onUpload(id, pendingFile); + setPendingFile(null); + }} + > + + + +
+ )} + {!entries ? ( +
+ {timedOut ? t("connectionInfo.unavailable") : t("connectionInfo.loading")} +
+ ) : entries.length === 0 ? ( +
{t("serverIcons.empty")}
+ ) : ( +
+ {entries.map((entry) => { + const path = `/${entry.name}`; + const base64 = images[path]; + return ( +
+ {base64 ? ( + {entry.name} + ) : ( +
+ )} + {entry.name.replace("icon_", "#")} + +
+ ); + })} +
+ )} +
+
+
+
+ +
+
+
+
+ ); +} + function PermissionOverviewDialog({ entries, onClose, @@ -3428,6 +3577,9 @@ function AppInner() { ); const [fileBrowserPath, setFileBrowserPath] = useState("/"); const [fileBrowserEntries, setFileBrowserEntries] = useState(null); + const [serverIconsOpen, setServerIconsOpen] = useState(false); + const [serverIconEntries, setServerIconEntries] = useState(null); + const [serverIconImages, setServerIconImages] = useState>({}); const [treeWidth, setTreeWidth] = useState(260); const [upperHeight, setUpperHeight] = useState(340); const [selected, setSelected] = useState(null); @@ -4035,15 +4187,31 @@ function AppInner() { setPermissionOverview(data.entries); break; case "fileList": - setFileBrowserEntries(data.entries); + // Channel 0 isn't a real, selectable channel - it's TS3's special + // server-wide icon repository, so a listing for it always means the + // server icons window, never the per-channel file browser. + if (data.cid === 0) { + setServerIconEntries( + data.entries.filter((e: FileListEntry) => e.isFile && /^icon_\d+$/.test(e.name)) + ); + } else { + setFileBrowserEntries(data.entries); + } break; case "fileDownloadData": { - const filename = data.path.split("/").filter(Boolean).pop() ?? "download"; - triggerBrowserDownload(filename, data.data); + if (data.cid === 0) { + // Icon preview fetch, not a user-initiated download - stash the + // bytes for the server icons grid instead of saving a file. + setServerIconImages((prev) => ({ ...prev, [data.path]: data.data })); + } else { + const filename = data.path.split("/").filter(Boolean).pop() ?? "download"; + triggerBrowserDownload(filename, data.data); + } break; } case "fileUploadDone": - handleFileBrowserRefresh(); + if (data.cid === 0) handleServerIconsRefresh(); + else handleFileBrowserRefresh(); break; } }; @@ -4239,6 +4407,19 @@ function AppInner() { }; }, [channelContextMenu]); + // Icon files aren't previewable from the listing alone - fetch each one's + // bytes individually once, then cache them in serverIconImages so re-runs + // (triggered by that same state update) become no-ops for already-fetched icons. + useEffect(() => { + if (!serverIconsOpen || !serverIconEntries) return; + for (const entry of serverIconEntries) { + const path = `/${entry.name}`; + if (!(path in serverIconImages)) { + socketRef.current?.send(JSON.stringify({ type: "downloadFile", channelId: 0, path })); + } + } + }, [serverIconsOpen, serverIconEntries, serverIconImages]); + useEffect(() => { if (!connected) { setWhisperChannelIds(new Set()); @@ -4911,6 +5092,36 @@ function AppInner() { reader.readAsDataURL(file); }; + const handleShowServerIcons = () => { + setServerIconsOpen(true); + setServerIconEntries(null); + setServerIconImages({}); + socketRef.current?.send(JSON.stringify({ type: "getFileList", channelId: 0, path: "/" })); + }; + + const handleServerIconsRefresh = () => { + setServerIconImages({}); + socketRef.current?.send(JSON.stringify({ type: "getFileList", channelId: 0, path: "/" })); + }; + + const handleServerIconUpload = (iconId: number, file: File) => { + const reader = new FileReader(); + reader.onload = () => { + const result = reader.result as string; + const base64 = result.slice(result.indexOf(",") + 1); + socketRef.current?.send( + JSON.stringify({ type: "uploadFile", channelId: 0, path: `/icon_${iconId}`, dataBase64: base64 }) + ); + }; + reader.readAsDataURL(file); + }; + + const handleServerIconDelete = (entry: FileListEntry) => { + if (!window.confirm(t("fileBrowser.deleteConfirm"))) return; + socketRef.current?.send(JSON.stringify({ type: "deleteFile", channelId: 0, name: `/${entry.name}` })); + window.setTimeout(handleServerIconsRefresh, 300); + }; + const handleSendPoke = () => { if (!pokeTarget) return; socketRef.current?.send( @@ -5771,6 +5982,16 @@ function AppInner() { /> )} + {serverIconsOpen && ( + setServerIconsOpen(false)} + /> + )} + {contactsOpen && ( {t("serverContext.addFavorite")}
+