Files
Mycelium-ui-private/src/lib/utils.ts
syoul f28d0e1338 P4: messages, topics, pubkey
Backend
- api/messages.rs covers send/pop/reply/status with an externally
  tagged MessageDestination enum that matches the daemon's
  {ip|pk: ...} body shape; pop_message uses an inflated request
  timeout to outlast the long-poll window
- api/topics.rs implements default action, topic CRUD, sources
  whitelist, and forward-socket get/set/remove. POST /topics ships
  the raw base64 string as the body (not JSON); path segments are
  percent-encoded inline (topics contain '/' and '+')
- api/pubkey.rs resolves an overlay IPv6 to a hex public key
- poller spawns a third long-poll loop on /messages?peek=false
  that fans every inbound message into a 200-deep ring buffer and
  emits messages://incoming for the UI

Frontend
- messages store: live inbox via the event, persisted outbox via
  tauri-plugin-store keyed under outbox.json
- ComposeMessage form: ip/pk toggle, optional UTF-8 topic and
  payload that get base64-encoded with a TextEncoder-based helper
- MessageList renders printable payloads decoded; binary payloads
  fall back to a "(N bytes binary)" hint
- Topics view: split layout with whitelist on the left, per-topic
  sources/forward editor on the right; default-action toggle is
  surfaced at the top
2026-04-25 23:10:21 +02:00

58 lines
1.7 KiB
TypeScript

import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
export function formatBytes(bytes: number): string {
if (bytes === 0) return "0 B";
const k = 1024;
const sizes = ["B", "KiB", "MiB", "GiB", "TiB"];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return `${(bytes / Math.pow(k, i)).toFixed(i === 0 ? 0 : 2)} ${sizes[i]}`;
}
export function formatRate(bytesPerSec: number): string {
return `${formatBytes(bytesPerSec)}/s`;
}
export function shortenIpv6(addr: string): string {
if (addr.length <= 24) return addr;
return `${addr.slice(0, 10)}${addr.slice(-8)}`;
}
// ─── base64 helpers (UTF-8 safe) ─────────────────────────────────────────────
export function utf8ToBase64(s: string): string {
const bytes = new TextEncoder().encode(s);
let bin = "";
for (const b of bytes) bin += String.fromCharCode(b);
return btoa(bin);
}
export function base64ToUtf8(b64: string): string {
try {
const bin = atob(b64);
const bytes = new Uint8Array(bin.length);
for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
return new TextDecoder("utf-8", { fatal: false }).decode(bytes);
} catch {
return b64;
}
}
export function isPrintableUtf8(b64: string): boolean {
try {
const bin = atob(b64);
for (let i = 0; i < bin.length; i++) {
const c = bin.charCodeAt(i);
// Reject NUL and most C0 except CR/LF/TAB.
if (c === 0 || (c < 0x20 && c !== 9 && c !== 10 && c !== 13)) return false;
}
return true;
} catch {
return false;
}
}