mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-01 00:42:49 +08:00
Add server icons window (#77)
Right-click on the server row -> "Server-Icons..." opens a grid of the server's icon repository (TS3's special "channel 0" filebase). Reuses the file-transfer plumbing from #83 (ftgetfilelist/download/upload/delete) rather than adding new connector commands - icon_<id> files under channel 0 *are* the server icon repository, so no new protocol handling was needed. Lets you view existing icons as real thumbnails (sniffed from the downloaded bytes - PNG/GIF/JPEG/BMP), upload a new one under a chosen numeric ID, and delete icons. Out of scope for this pass: actually assigning an icon to the server/a channel/a group (there's no icon-picker wired into the server/channel edit dialogs yet) - this delivers the icon repository browser only. Demo mode gets a small fake icon repository (two placeholder icons) so the window is interactive in the GitHub Pages demo too.
This commit is contained in:
1 parent
7901aafbb1
commit
6d6803d9b3
4 files changed
+297
-12
No files matched your search
@@ -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);
|
||||
|
||||
+235
-4
@@ -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_<id>", where <id> 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<string, string>;
|
||||
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<HTMLInputElement>(null);
|
||||
const [pendingFile, setPendingFile] = useState<File | null>(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 (
|
||||
<div className="ts-dialog-backdrop" {...backdrop}>
|
||||
<div className="ts-dialog ts-server-icons-dialog" onClick={(e) => e.stopPropagation()}>
|
||||
<div className="ts-dialog-titlebar">
|
||||
<span>{t("serverIcons.title")}</span>
|
||||
<button onClick={onClose} title={t("dialog.close")}>
|
||||
✕
|
||||
</button>
|
||||
</div>
|
||||
<div className="ts-dialog-body">
|
||||
<div className="ts-file-browser-toolbar">
|
||||
<button
|
||||
onClick={() => {
|
||||
setNewIconId(String(nextFreeId()));
|
||||
fileInputRef.current?.click();
|
||||
}}
|
||||
title={t("serverIcons.upload")}
|
||||
>
|
||||
⬆️ {t("serverIcons.upload")}
|
||||
</button>
|
||||
<input
|
||||
ref={fileInputRef}
|
||||
type="file"
|
||||
accept="image/*"
|
||||
style={{ display: "none" }}
|
||||
onChange={(e) => {
|
||||
const file = e.target.files?.[0];
|
||||
e.target.value = "";
|
||||
if (file) setPendingFile(file);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
{pendingFile && (
|
||||
<form
|
||||
className="ts-dialog-row"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
const id = parseInt(newIconId, 10);
|
||||
if (!Number.isNaN(id) && pendingFile) onUpload(id, pendingFile);
|
||||
setPendingFile(null);
|
||||
}}
|
||||
>
|
||||
<label className="ts-dialog-field">
|
||||
{t("serverIcons.iconId")}
|
||||
<input
|
||||
type="number"
|
||||
autoFocus
|
||||
value={newIconId}
|
||||
onChange={(e) => setNewIconId(e.target.value)}
|
||||
/>
|
||||
</label>
|
||||
<button type="submit">{t("dialog.ok")}</button>
|
||||
<button type="button" onClick={() => setPendingFile(null)}>
|
||||
{t("dialog.cancel")}
|
||||
</button>
|
||||
</form>
|
||||
)}
|
||||
{!entries ? (
|
||||
<div className="ts-connection-info-loading">
|
||||
{timedOut ? t("connectionInfo.unavailable") : t("connectionInfo.loading")}
|
||||
</div>
|
||||
) : entries.length === 0 ? (
|
||||
<div className="ts-connection-info-loading">{t("serverIcons.empty")}</div>
|
||||
) : (
|
||||
<div className="ts-server-icons-grid">
|
||||
{entries.map((entry) => {
|
||||
const path = `/${entry.name}`;
|
||||
const base64 = images[path];
|
||||
return (
|
||||
<div className="ts-server-icons-tile" key={entry.name}>
|
||||
{base64 ? (
|
||||
<img src={iconDataUrl(base64)} alt={entry.name} />
|
||||
) : (
|
||||
<div className="ts-server-icons-tile-loading" />
|
||||
)}
|
||||
<span>{entry.name.replace("icon_", "#")}</span>
|
||||
<button onClick={() => onDelete(entry)} title={t("fileBrowser.delete")}>
|
||||
🗑
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="ts-dialog-buttons">
|
||||
<div />
|
||||
<div className="ts-dialog-buttons-right">
|
||||
<button onClick={onClose}>{t("dialog.close")}</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function PermissionOverviewDialog({
|
||||
entries,
|
||||
onClose,
|
||||
@@ -3428,6 +3577,9 @@ function AppInner() {
|
||||
);
|
||||
const [fileBrowserPath, setFileBrowserPath] = useState("/");
|
||||
const [fileBrowserEntries, setFileBrowserEntries] = useState<FileListEntry[] | null>(null);
|
||||
const [serverIconsOpen, setServerIconsOpen] = useState(false);
|
||||
const [serverIconEntries, setServerIconEntries] = useState<FileListEntry[] | null>(null);
|
||||
const [serverIconImages, setServerIconImages] = useState<Record<string, string>>({});
|
||||
const [treeWidth, setTreeWidth] = useState(260);
|
||||
const [upperHeight, setUpperHeight] = useState(340);
|
||||
const [selected, setSelected] = useState<SelectedItem | null>(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 && (
|
||||
<ServerIconsDialog
|
||||
entries={serverIconEntries}
|
||||
images={serverIconImages}
|
||||
onUpload={handleServerIconUpload}
|
||||
onDelete={handleServerIconDelete}
|
||||
onClose={() => setServerIconsOpen(false)}
|
||||
/>
|
||||
)}
|
||||
|
||||
{contactsOpen && (
|
||||
<ContactsDialog
|
||||
contacts={contacts}
|
||||
@@ -6171,6 +6392,16 @@ function AppInner() {
|
||||
<span className="ts-menu-item-label">{t("serverContext.addFavorite")}</span>
|
||||
</button>
|
||||
<div className="ts-menu-separator" />
|
||||
<button
|
||||
className="ts-menu-item"
|
||||
onClick={() => {
|
||||
handleShowServerIcons();
|
||||
setServerContextMenu(null);
|
||||
}}
|
||||
>
|
||||
<span className="ts-menu-item-icon">🖼️</span>
|
||||
<span className="ts-menu-item-label">{t("serverContext.icons")}</span>
|
||||
</button>
|
||||
<button
|
||||
className="ts-menu-item"
|
||||
onClick={() => {
|
||||
|
||||
+14
-8
@@ -82,8 +82,17 @@ const DEMO_FILES: Record<string, DemoFileEntry[]> = {
|
||||
],
|
||||
"2:/": [],
|
||||
"3:/": [],
|
||||
// Channel 0 is TS3's special server-wide icon repository, not a real channel.
|
||||
"0:/": [
|
||||
{ path: "/", name: "icon_100", size: 68, isFile: true, timestamp: "2024-01-01 12:00:00" },
|
||||
{ path: "/", name: "icon_200", size: 68, isFile: true, timestamp: "2024-01-01 12:00:00" },
|
||||
],
|
||||
};
|
||||
|
||||
// A tiny 1x1 transparent PNG, reused as the fake image data for every demo icon.
|
||||
const DEMO_ICON_PNG_BASE64 =
|
||||
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=";
|
||||
|
||||
function demoFileKey(channelId: number, path: string): string {
|
||||
return `${channelId}:${path}`;
|
||||
}
|
||||
@@ -452,14 +461,11 @@ export class DemoSocket {
|
||||
case "downloadFile": {
|
||||
const full = msg.path as string;
|
||||
const name = full.slice(full.lastIndexOf("/") + 1);
|
||||
this.after(400, () =>
|
||||
this.emit({
|
||||
type: "fileDownloadData",
|
||||
cid: msg.channelId,
|
||||
path: full,
|
||||
data: btoa(`This is a demo file. There is no real content behind "${name}" in WebSpeak3's demo mode.`),
|
||||
})
|
||||
);
|
||||
const data =
|
||||
msg.channelId === 0
|
||||
? DEMO_ICON_PNG_BASE64
|
||||
: btoa(`This is a demo file. There is no real content behind "${name}" in WebSpeak3's demo mode.`);
|
||||
this.after(400, () => this.emit({ type: "fileDownloadData", cid: msg.channelId, path: full, data }));
|
||||
break;
|
||||
}
|
||||
case "uploadFile": {
|
||||
|
||||
@@ -430,6 +430,12 @@ const translations: Record<Lang, Record<string, string>> = {
|
||||
"fileBrowser.uploading": "Wird hochgeladen...",
|
||||
"fileBrowser.downloading": "Wird heruntergeladen...",
|
||||
|
||||
"serverContext.icons": "Server-Icons...",
|
||||
"serverIcons.title": "Server-Icons verwalten",
|
||||
"serverIcons.upload": "Icon hochladen",
|
||||
"serverIcons.iconId": "Icon-ID",
|
||||
"serverIcons.empty": "Keine Icons vorhanden.",
|
||||
|
||||
"clientContext.privateChat": "Privater Chat",
|
||||
"clientContext.poke": "Anstupsen",
|
||||
"clientContext.copyName": "Namen kopieren",
|
||||
@@ -902,6 +908,12 @@ const translations: Record<Lang, Record<string, string>> = {
|
||||
"fileBrowser.uploading": "Uploading...",
|
||||
"fileBrowser.downloading": "Downloading...",
|
||||
|
||||
"serverContext.icons": "Server Icons...",
|
||||
"serverIcons.title": "Manage Server Icons",
|
||||
"serverIcons.upload": "Upload Icon",
|
||||
"serverIcons.iconId": "Icon ID",
|
||||
"serverIcons.empty": "No icons found.",
|
||||
|
||||
"clientContext.privateChat": "Private chat",
|
||||
"clientContext.poke": "Poke",
|
||||
"clientContext.copyName": "Copy name",
|
||||
|
||||
Reference in new issue
Block a user