feat(frontend): avatar upload QOL — feedback, re-crop, drop, progress, history

Task 12 batch around the existing crop/preview flow:
- Success Notice (auto-dismiss ~2.5s) after an avatar upload.
- "Reposition" re-crops the current avatar without re-picking a file.
- Drag & drop an image straight onto the profile avatar circle.
- Real upload progress via a new XHR-backed api.upload (fetch has none);
  refreshOnce now also stores the renewed token (reload-restore fix).
- Local avatar history (last 5, IndexedDB, this-device-only) strip that
  re-uploads a stored crop. Stored as ArrayBuffer (Blob is not cloneable
  under structuredClone); label makes the per-device scope explicit.
- Added a "success" tone to Notice.

Infra (same session): registered @shared/@features/@pages/@app/@test path
aliases in vite.config + tsconfigs and rewrote deep ../../../ imports;
moved avatar components into components/avatar/ to keep folders at <=4 files.

Tests: avatarHistory cap/order, recrop visibility, drop-opens-cropper,
client upload/progress/401-retry. bun test 103 pass, lint 0 errors, build ok.
This commit is contained in:
Loki 2026-09-28 22:56:01 +02:00
parent e79b0aedd2
commit fd2e29cede
Signed by: boba
GPG key ID: 253067914055423B
92 changed files with 808 additions and 287 deletions

View file

@ -1,60 +0,0 @@
/** Pure geometry for the circular avatar cropper. No DOM/canvas here so it's plain-arithmetic testable. */
export const MIN_ZOOM = 1
export const MAX_ZOOM = 4
/** The zoom that makes the image exactly cover a `viewport`x`viewport` square at zoom=1 (our baseline). */
export function coverScale(naturalWidth: number, naturalHeight: number, viewport: number): number {
return viewport / Math.min(naturalWidth, naturalHeight)
}
/**
* Clamp a pan offset (in viewport pixels, center-relative) so the scaled image never
* shows empty space inside the `viewport`x`viewport` crop square.
*/
export function clampPan(
offset: { x: number; y: number },
naturalWidth: number,
naturalHeight: number,
viewport: number,
zoom: number,
): { x: number; y: number } {
const scale = coverScale(naturalWidth, naturalHeight, viewport) * zoom
const scaledW = naturalWidth * scale
const scaledH = naturalHeight * scale
const maxX = Math.max(0, (scaledW - viewport) / 2)
const maxY = Math.max(0, (scaledH - viewport) / 2)
return {
x: Math.min(maxX, Math.max(-maxX, offset.x)),
y: Math.min(maxY, Math.max(-maxY, offset.y)),
}
}
export function clampZoom(zoom: number): number {
return Math.min(MAX_ZOOM, Math.max(MIN_ZOOM, zoom))
}
/**
* Given the image's natural size, the crop viewport size, the current pan offset (viewport px,
* center-relative) and zoom, compute the source rectangle (in natural image pixels) that should
* be drawn onto the output canvas.
*/
export function sourceRect(
naturalWidth: number,
naturalHeight: number,
viewport: number,
zoom: number,
offset: { x: number; y: number },
): { sx: number; sy: number; sw: number; sh: number } {
const scale = coverScale(naturalWidth, naturalHeight, viewport) * zoom
const sw = viewport / scale
const sh = viewport / scale
const centerX = naturalWidth / 2 - offset.x / scale
const centerY = naturalHeight / 2 - offset.y / scale
return {
sx: centerX - sw / 2,
sy: centerY - sh / 2,
sw,
sh,
}
}