Web app (browse, search, dashboard, custom fields, family read-only links), Android capture app with offline upload queue and QR pairing, and an MCP server so an AI subscription can identify, value and list items.
71 lines
3.0 KiB
JavaScript
71 lines
3.0 KiB
JavaScript
export const esc = (s) => String(s ?? '').replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]));
|
||
|
||
export async function api(path, opts = {}) {
|
||
const init = { credentials: 'same-origin', ...opts, headers: { ...(opts.headers || {}) } };
|
||
if (opts.json !== undefined) {
|
||
init.body = JSON.stringify(opts.json);
|
||
init.headers['Content-Type'] = 'application/json';
|
||
init.method = init.method || 'POST';
|
||
}
|
||
const r = await fetch(`/api${path}`, init);
|
||
const ct = r.headers.get('content-type') || '';
|
||
const data = ct.includes('json') ? await r.json() : await r.text();
|
||
if (!r.ok) throw Object.assign(new Error(data?.error || `HTTP ${r.status}`), { status: r.status });
|
||
return data;
|
||
}
|
||
|
||
export function toast(msg, err = false) {
|
||
const t = document.createElement('div');
|
||
t.className = 'toast' + (err ? ' err' : '');
|
||
t.textContent = msg;
|
||
document.body.appendChild(t);
|
||
setTimeout(() => t.remove(), err ? 4500 : 2200);
|
||
}
|
||
|
||
export function fmtMoney(v, cur) {
|
||
if (v == null || v === '') return '';
|
||
const n = Number(v);
|
||
try {
|
||
return new Intl.NumberFormat(undefined, { style: 'currency', currency: cur || 'EUR', maximumFractionDigits: n >= 100 ? 0 : 2 }).format(n);
|
||
} catch { return `${n} ${cur || ''}`; }
|
||
}
|
||
|
||
export function fmtRange(lo, hi, cur) {
|
||
if (lo == null && hi == null) return '';
|
||
if (lo != null && hi != null && Number(lo) !== Number(hi)) return `${fmtMoney(lo, cur)}–${fmtMoney(hi, cur).replace(/^[^\d]+/, '')}`;
|
||
return fmtMoney(hi ?? lo, cur);
|
||
}
|
||
|
||
export function ago(sqlDate) {
|
||
if (!sqlDate) return '';
|
||
const d = new Date(sqlDate.replace(' ', 'T') + 'Z');
|
||
const s = (Date.now() - d.getTime()) / 1000;
|
||
if (s < 60) return 'just now';
|
||
if (s < 3600) return `${Math.floor(s / 60)}m ago`;
|
||
if (s < 86400) return `${Math.floor(s / 3600)}h ago`;
|
||
if (s < 86400 * 30) return `${Math.floor(s / 86400)}d ago`;
|
||
return d.toLocaleDateString();
|
||
}
|
||
|
||
export async function copy(text) {
|
||
try { await navigator.clipboard.writeText(text); toast('Copied'); } catch {
|
||
const ta = document.createElement('textarea'); ta.value = text; document.body.appendChild(ta); ta.select();
|
||
document.execCommand('copy'); ta.remove(); toast('Copied');
|
||
}
|
||
}
|
||
|
||
/** Downscale a photo in the browser before upload (keeps uploads fast on bad wifi). */
|
||
export async function shrinkImage(file, max = 2560, quality = 0.86) {
|
||
if (!file.type.startsWith('image/')) return file;
|
||
try {
|
||
const bmp = await createImageBitmap(file, { imageOrientation: 'from-image' });
|
||
const scale = Math.min(1, max / Math.max(bmp.width, bmp.height));
|
||
if (scale === 1 && file.size < 3e6) return file;
|
||
const c = document.createElement('canvas');
|
||
c.width = Math.round(bmp.width * scale); c.height = Math.round(bmp.height * scale);
|
||
c.getContext('2d').drawImage(bmp, 0, 0, c.width, c.height);
|
||
const blob = await new Promise((res) => c.toBlob(res, 'image/jpeg', quality));
|
||
return blob ? new File([blob], file.name.replace(/\.\w+$/, '') + '.jpg', { type: 'image/jpeg' }) : file;
|
||
} catch { return file; }
|
||
}
|