mirror of
https://github.com/bitbaum/chatkit.git
synced 2026-12-03 22:47:19 +01:00
The empty box's send slot records a voice message in the box's place; one tap sends it. A voice note plays in the thread in the bubble's own colours. AAC in MP4 first, so every phone plays it. Its length travels with it. Fix: the timer rounded to 0:01 at half a second and a short take was dropped without a word. Claude-Session: https://claude.ai/code/session_019MDtmsmMXvM981mcszxEA6 Co-authored-by: Claude <noreply@anthropic.com>
362 lines
14 KiB
JavaScript
362 lines
14 KiB
JavaScript
"use client";
|
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
import { DEFAULT_ATTACHMENT_POLICY, attachmentKind, checkAttachment, } from "../thread-rules.js";
|
|
import { fitWithin } from "../attachments.js";
|
|
import { newId } from "../conversations.js";
|
|
import { shrinkImage } from "../image.js";
|
|
import { refusalText, } from "../messenger.js";
|
|
export const DEFAULT_UPLOAD_OPTIONS = {
|
|
imageMaxEdge: 2048,
|
|
imageMaxBytes: 1_500_000,
|
|
};
|
|
/** Pictures a canvas re-encodes without losing what they are. An animated
|
|
* GIF would lose its animation, so it is sent as it is. */
|
|
const SHRINKABLE = /^image\/(jpeg|jpg|png|webp|heic|heif|bmp|tiff|avif)$/i;
|
|
const POSTER_EDGE = 640;
|
|
const PROBE_TIMEOUT_MS = 4_000;
|
|
async function dataUrlToBlob(dataUrl) {
|
|
const comma = dataUrl.indexOf(",");
|
|
const mime = /^data:([^;,]+)/.exec(dataUrl)?.[1] ?? "application/octet-stream";
|
|
const binary = atob(dataUrl.slice(comma + 1));
|
|
const bytes = new Uint8Array(binary.length);
|
|
for (let i = 0; i < binary.length; i++)
|
|
bytes[i] = binary.charCodeAt(i);
|
|
return new Blob([bytes], { type: mime });
|
|
}
|
|
async function imageSize(blob) {
|
|
if (typeof createImageBitmap !== "function")
|
|
return null;
|
|
try {
|
|
const bitmap = await createImageBitmap(blob);
|
|
const size = { width: bitmap.width, height: bitmap.height };
|
|
bitmap.close();
|
|
return size;
|
|
}
|
|
catch {
|
|
return null;
|
|
}
|
|
}
|
|
function renamed(name, extension) {
|
|
const dot = name.lastIndexOf(".");
|
|
return `${dot > 0 ? name.slice(0, dot) : name}.${extension}`;
|
|
}
|
|
/**
|
|
* A film's size, length and first frame, read in the browser. Best effort:
|
|
* a codec the browser cannot open still uploads, as a file tile with no
|
|
* poster, and never holds the send up for longer than the timeout.
|
|
*/
|
|
function probeVideo(blob) {
|
|
if (typeof document === "undefined")
|
|
return Promise.resolve({});
|
|
return new Promise((resolve) => {
|
|
const url = URL.createObjectURL(blob);
|
|
const video = document.createElement("video");
|
|
let settled = false;
|
|
const finish = (value) => {
|
|
if (settled)
|
|
return;
|
|
settled = true;
|
|
clearTimeout(timer);
|
|
video.removeAttribute("src");
|
|
video.load();
|
|
URL.revokeObjectURL(url);
|
|
resolve(value);
|
|
};
|
|
const facts = () => ({
|
|
width: video.videoWidth || undefined,
|
|
height: video.videoHeight || undefined,
|
|
durationMs: Number.isFinite(video.duration) ? Math.round(video.duration * 1000) : undefined,
|
|
});
|
|
const timer = setTimeout(() => finish(video.videoWidth ? facts() : {}), PROBE_TIMEOUT_MS);
|
|
video.muted = true;
|
|
video.playsInline = true;
|
|
video.preload = "auto";
|
|
video.onerror = () => finish({});
|
|
video.onloadeddata = () => {
|
|
// A frame a little way in: the very first one is often black.
|
|
const at = Number.isFinite(video.duration) ? Math.min(0.5, video.duration / 2) : 0;
|
|
if (at > 0)
|
|
video.currentTime = at;
|
|
else
|
|
draw();
|
|
};
|
|
video.onseeked = () => draw();
|
|
const draw = () => {
|
|
const known = facts();
|
|
if (!known.width || !known.height)
|
|
return finish(known);
|
|
const size = fitWithin(known.width, known.height, POSTER_EDGE);
|
|
const canvas = document.createElement("canvas");
|
|
canvas.width = size.width;
|
|
canvas.height = size.height;
|
|
const ctx = canvas.getContext("2d");
|
|
if (!ctx)
|
|
return finish(known);
|
|
try {
|
|
ctx.drawImage(video, 0, 0, size.width, size.height);
|
|
canvas.toBlob((poster) => finish({ ...known, poster: poster ?? undefined }), "image/jpeg", 0.7);
|
|
}
|
|
catch {
|
|
finish(known);
|
|
}
|
|
};
|
|
video.src = url;
|
|
});
|
|
}
|
|
function probeAudio(blob) {
|
|
if (typeof document === "undefined")
|
|
return Promise.resolve(undefined);
|
|
return new Promise((resolve) => {
|
|
const url = URL.createObjectURL(blob);
|
|
const audio = document.createElement("audio");
|
|
const finish = (ms) => {
|
|
clearTimeout(timer);
|
|
URL.revokeObjectURL(url);
|
|
resolve(ms);
|
|
};
|
|
const timer = setTimeout(() => finish(), PROBE_TIMEOUT_MS);
|
|
audio.preload = "metadata";
|
|
audio.onloadedmetadata = () => finish(Number.isFinite(audio.duration) ? Math.round(audio.duration * 1000) : undefined);
|
|
audio.onerror = () => finish();
|
|
audio.src = url;
|
|
});
|
|
}
|
|
/**
|
|
* Files on their way to a message: checked against the policy before a byte
|
|
* moves, photos shrunk in the browser, each uploaded through the app's
|
|
* `transport.upload` with its progress shown, a failure kept with Try again.
|
|
* The same rule that refuses a file here refuses it on the server
|
|
* (`@bitbaum/threadkit` `checkAttachment`), so the two cannot disagree.
|
|
*/
|
|
export function useUploads({ transport, conversationId, options = {}, labels, failedText, }) {
|
|
const [items, setItems] = useState([]);
|
|
const [note, setNote] = useState(null);
|
|
const live = useRef({ transport, conversationId, options, labels, failedText });
|
|
useEffect(() => {
|
|
live.current = { transport, conversationId, options, labels, failedText };
|
|
});
|
|
// What an item needs to be sent again, and how to stop it.
|
|
const work = useRef(new Map());
|
|
const previews = useRef(new Set());
|
|
const itemsRef = useRef([]);
|
|
useEffect(() => {
|
|
itemsRef.current = items;
|
|
}, [items]);
|
|
const used = useRef(null);
|
|
const patch = useCallback((key, change) => {
|
|
setItems((current) => current.map((i) => (i.key === key ? { ...i, ...change } : i)));
|
|
}, []);
|
|
const drop = useCallback((key) => {
|
|
const entry = work.current.get(key);
|
|
entry?.abort.abort();
|
|
work.current.delete(key);
|
|
setItems((current) => {
|
|
const gone = current.find((i) => i.key === key);
|
|
if (gone?.previewUrl) {
|
|
URL.revokeObjectURL(gone.previewUrl);
|
|
previews.current.delete(gone.previewUrl);
|
|
}
|
|
return current.filter((i) => i.key !== key);
|
|
});
|
|
}, []);
|
|
const upload = useCallback(async (key, prepared) => {
|
|
const { transport: t } = live.current;
|
|
const entry = work.current.get(key);
|
|
if (!t.upload || !entry)
|
|
return;
|
|
patch(key, { state: "uploading", progress: 0 });
|
|
try {
|
|
const attachment = await t.upload(prepared.blob, prepared.about, {
|
|
signal: entry.abort.signal,
|
|
progress: (fraction) => patch(key, { progress: Math.max(0, Math.min(1, fraction)) }),
|
|
});
|
|
if (entry.abort.signal.aborted)
|
|
return;
|
|
patch(key, { state: "done", progress: 1, attachment });
|
|
}
|
|
catch {
|
|
if (entry.abort.signal.aborted)
|
|
return;
|
|
patch(key, { state: "failed" });
|
|
setNote(live.current.failedText.replace("{name}", prepared.about.name));
|
|
}
|
|
}, [patch]);
|
|
const prepare = useCallback(async (file, key, known) => {
|
|
const { options: o, labels: l, conversationId: id, transport: t } = live.current;
|
|
const policy = o.policy ?? DEFAULT_ATTACHMENT_POLICY;
|
|
const edge = o.imageMaxEdge ?? DEFAULT_UPLOAD_OPTIONS.imageMaxEdge;
|
|
const cap = o.imageMaxBytes ?? DEFAULT_UPLOAD_OPTIONS.imageMaxBytes;
|
|
if (!id)
|
|
return l.type.replace("{name}", file.name);
|
|
let blob = file;
|
|
let name = file.name || "file";
|
|
let mimeType = file.type;
|
|
let width;
|
|
let height;
|
|
if (SHRINKABLE.test(mimeType) && edge > 0) {
|
|
const size = await imageSize(file);
|
|
const accepted = attachmentKind(mimeType, policy) === "image";
|
|
const tooBig = file.size > cap || (size && Math.max(size.width, size.height) > edge);
|
|
if (tooBig || !accepted) {
|
|
// Shrunk, and in passing turned into a JPEG every browser shows
|
|
// (a phone's HEIC is opened by Safari and by nothing else).
|
|
const dataUrl = await shrinkImage(file, edge, cap).catch(() => null);
|
|
if (dataUrl) {
|
|
blob = await dataUrlToBlob(dataUrl);
|
|
mimeType = "image/jpeg";
|
|
name = renamed(name, "jpg");
|
|
const fitted = size ? fitWithin(size.width, size.height, edge) : null;
|
|
width = fitted?.width;
|
|
height = fitted?.height;
|
|
}
|
|
}
|
|
if (width === undefined && size) {
|
|
width = size.width;
|
|
height = size.height;
|
|
}
|
|
}
|
|
if (used.current === null) {
|
|
used.current = t.usage
|
|
? t.usage().then((u) => u.usedBytes, () => undefined)
|
|
: Promise.resolve(undefined);
|
|
}
|
|
const usedBytes = await used.current;
|
|
const others = itemsRef.current.filter((i) => i.key !== key && i.state !== "failed");
|
|
const check = checkAttachment({ mimeType, bytes: blob.size }, {
|
|
onMessage: others.length,
|
|
usedBytes: usedBytes === undefined
|
|
? undefined
|
|
: usedBytes + others.reduce((sum, i) => sum + i.bytes, 0),
|
|
},
|
|
// No quota is checked in the browser when the app cannot say what is
|
|
// used; the server still decides.
|
|
usedBytes === undefined ? { ...policy, quotaBytes: null } : policy);
|
|
if (!check.ok)
|
|
return refusalText(check, name, l);
|
|
const about = {
|
|
conversationId: id,
|
|
kind: check.kind,
|
|
name,
|
|
mimeType,
|
|
bytes: blob.size,
|
|
width,
|
|
height,
|
|
};
|
|
let previewUrl;
|
|
if (check.kind === "image") {
|
|
if (width === undefined) {
|
|
const size = await imageSize(blob);
|
|
about.width = size?.width;
|
|
about.height = size?.height;
|
|
}
|
|
previewUrl = URL.createObjectURL(blob);
|
|
}
|
|
else if (check.kind === "video") {
|
|
const probed = await probeVideo(blob);
|
|
about.width = probed.width;
|
|
about.height = probed.height;
|
|
about.durationMs = probed.durationMs;
|
|
about.poster = probed.poster;
|
|
if (probed.poster)
|
|
previewUrl = URL.createObjectURL(probed.poster);
|
|
}
|
|
else if (check.kind === "audio") {
|
|
// A recording made by MediaRecorder reads back as Infinity in Chrome,
|
|
// so the length the recorder measured is the one that is kept.
|
|
about.durationMs = known?.durationMs ?? (await probeAudio(blob));
|
|
}
|
|
if (previewUrl)
|
|
previews.current.add(previewUrl);
|
|
return { blob, about, previewUrl };
|
|
}, []);
|
|
const add = useCallback((files, known) => {
|
|
if (!live.current.transport.upload)
|
|
return;
|
|
setNote(null);
|
|
for (const file of files) {
|
|
const key = newId("u");
|
|
const abort = new AbortController();
|
|
work.current.set(key, { abort });
|
|
const placeholder = {
|
|
key,
|
|
name: file.name || "file",
|
|
// A first guess, so the tile has a shape while the file is read.
|
|
kind: file.type.startsWith("image/")
|
|
? "image"
|
|
: file.type.startsWith("video/")
|
|
? "video"
|
|
: file.type.startsWith("audio/")
|
|
? "audio"
|
|
: "file",
|
|
bytes: file.size,
|
|
progress: 0,
|
|
state: "preparing",
|
|
};
|
|
itemsRef.current = [...itemsRef.current, placeholder];
|
|
setItems((current) => [...current, placeholder]);
|
|
void prepare(file, key, known).then((prepared) => {
|
|
if (abort.signal.aborted) {
|
|
if (typeof prepared !== "string" && prepared.previewUrl) {
|
|
URL.revokeObjectURL(prepared.previewUrl);
|
|
previews.current.delete(prepared.previewUrl);
|
|
}
|
|
return;
|
|
}
|
|
if (typeof prepared === "string") {
|
|
setNote(prepared);
|
|
drop(key);
|
|
return;
|
|
}
|
|
const entry = work.current.get(key);
|
|
if (entry)
|
|
entry.prepared = prepared;
|
|
patch(key, {
|
|
name: prepared.about.name,
|
|
kind: prepared.about.kind,
|
|
bytes: prepared.about.bytes,
|
|
previewUrl: prepared.previewUrl,
|
|
});
|
|
void upload(key, prepared);
|
|
}, () => {
|
|
if (abort.signal.aborted)
|
|
return;
|
|
setNote(live.current.failedText.replace("{name}", file.name || "file"));
|
|
drop(key);
|
|
});
|
|
}
|
|
}, [drop, patch, prepare, upload]);
|
|
const retry = useCallback((key) => {
|
|
const entry = work.current.get(key);
|
|
if (!entry?.prepared)
|
|
return;
|
|
entry.abort = new AbortController();
|
|
setNote(null);
|
|
void upload(key, entry.prepared);
|
|
}, [upload]);
|
|
const clear = useCallback(() => {
|
|
for (const entry of work.current.values())
|
|
entry.abort.abort();
|
|
work.current.clear();
|
|
for (const url of previews.current)
|
|
URL.revokeObjectURL(url);
|
|
previews.current.clear();
|
|
itemsRef.current = [];
|
|
setItems([]);
|
|
}, []);
|
|
// Staged files belong to the conversation they were picked in.
|
|
useEffect(() => {
|
|
return () => clear();
|
|
}, [conversationId, clear]);
|
|
return {
|
|
items,
|
|
note,
|
|
clearNote: useCallback(() => setNote(null), []),
|
|
add,
|
|
remove: drop,
|
|
retry,
|
|
clear,
|
|
attachments: items.flatMap((i) => (i.state === "done" && i.attachment ? [i.attachment] : [])),
|
|
busy: items.some((i) => i.state === "preparing" || i.state === "uploading"),
|
|
};
|
|
}
|
|
//# sourceMappingURL=use-uploads.js.map
|