114 lines
5.7 KiB
TypeScript
114 lines
5.7 KiB
TypeScript
import React, { useRef, useState } from 'react';
|
||
import type { SitesmithRefImage } from '../../hooks/useSitesmith';
|
||
import { formatBytes } from '../../utils/sitesmith-attachments';
|
||
import { btn, btnDanger, errBox, note, thumb, warnBox } from './Section';
|
||
|
||
interface Props {
|
||
/** Mockup images already stored for this site (from refs_list). */
|
||
images: SitesmithRefImage[];
|
||
selected: string[];
|
||
onSelectedChange: (ids: string[]) => void;
|
||
/** Total image budget for one send (mockups + reference screenshots). */
|
||
maxAttach: number;
|
||
/** Images already counted by selected "Sites I like" entries. */
|
||
referenceImageCount: number;
|
||
imageUrl: (id: string) => string;
|
||
onUpload: (file: File) => Promise<{ ok: true; image?: SitesmithRefImage } | { ok: false; message: string }>;
|
||
onRemove: (id: string) => Promise<{ ok: true } | { ok: false; message: string }>;
|
||
/** Image features off: why (vision unavailable). Text-only generation is unaffected. */
|
||
unavailableReason?: string | null;
|
||
disabled?: boolean;
|
||
/** Called with the message when a file is rejected or an upload fails. */
|
||
}
|
||
|
||
/** Mockup picker: drag and drop or file picker, thumbnails, remove. Image names are rendered as text nodes only. */
|
||
export const AttachmentTray: React.FC<Props> = ({
|
||
images, selected, onSelectedChange, maxAttach, referenceImageCount, imageUrl, onUpload, onRemove, unavailableReason, disabled,
|
||
}) => {
|
||
const input = useRef<HTMLInputElement>(null);
|
||
const [drag, setDrag] = useState(false);
|
||
const [busy, setBusy] = useState(false);
|
||
const [err, setErr] = useState<string | null>(null);
|
||
const mockups = images.filter((i) => i.kind === 'mockup');
|
||
const off = !!unavailableReason || !!disabled;
|
||
const used = selected.length + referenceImageCount;
|
||
|
||
const addFiles = async (files: FileList | File[] | null) => {
|
||
if (!files || off) return;
|
||
setErr(null);
|
||
setBusy(true);
|
||
try {
|
||
for (const f of Array.from(files)) {
|
||
if (used + 1 > maxAttach) { setErr(`You can attach up to ${maxAttach} images to one request.`); break; }
|
||
const r = await onUpload(f);
|
||
if (!r.ok) { setErr(r.message); break; }
|
||
if (r.image) onSelectedChange([...selected, r.image.id]);
|
||
}
|
||
} finally { setBusy(false); }
|
||
};
|
||
|
||
const toggle = (id: string) => {
|
||
if (selected.includes(id)) onSelectedChange(selected.filter((x) => x !== id));
|
||
else if (used < maxAttach) onSelectedChange([...selected, id]);
|
||
else setErr(`You can attach up to ${maxAttach} images to one request.`);
|
||
};
|
||
|
||
const remove = async (id: string) => {
|
||
setErr(null);
|
||
const r = await onRemove(id);
|
||
if (!r.ok) { setErr(r.message); return; }
|
||
onSelectedChange(selected.filter((x) => x !== id));
|
||
};
|
||
|
||
return (
|
||
<div data-testid="ss-mockups">
|
||
<p style={note}>
|
||
Attach a picture of the page you want (a sketch, a screenshot, a design export). Sitesmith builds toward it.
|
||
The image is kept privately for Sitesmith; it is never published or added to your assets.
|
||
</p>
|
||
{unavailableReason && <div style={warnBox} role="note" data-testid="ss-vision-note">{unavailableReason}</div>}
|
||
<div
|
||
data-testid="ss-dropzone"
|
||
onDragOver={(e) => { if (off) return; e.preventDefault(); setDrag(true); }}
|
||
onDragLeave={() => setDrag(false)}
|
||
onDrop={(e) => { e.preventDefault(); setDrag(false); void addFiles(e.dataTransfer?.files ?? null); }}
|
||
style={{
|
||
border: `1px dashed ${drag ? '#a78bfa' : '#3f3f46'}`, borderRadius: 8, padding: 12, textAlign: 'center',
|
||
background: drag ? 'rgba(124,58,237,0.12)' : 'transparent', opacity: off ? 0.5 : 1,
|
||
}}
|
||
>
|
||
<button type="button" style={btn} disabled={off || busy} onClick={() => input.current?.click()}>
|
||
{busy ? 'Uploading…' : 'Choose an image'}
|
||
</button>
|
||
<span style={{ color: '#a1a1aa', fontSize: 12, marginLeft: 8 }}>or drop it here (JPEG, PNG, WebP or GIF, up to 8 MB)</span>
|
||
<input
|
||
ref={input} type="file" accept="image/jpeg,image/png,image/webp,image/gif" hidden multiple
|
||
data-testid="ss-mockup-input" disabled={off}
|
||
onChange={(e) => { void addFiles(e.target.files); e.target.value = ''; }}
|
||
/>
|
||
</div>
|
||
{err && <div role="alert" style={errBox}>{err}</div>}
|
||
{mockups.length > 0 && (
|
||
<ul style={{ listStyle: 'none', margin: '10px 0 0', padding: 0, display: 'flex', flexWrap: 'wrap', gap: 10 }}>
|
||
{mockups.map((im) => {
|
||
const on = selected.includes(im.id);
|
||
return (
|
||
<li key={im.id} style={{ width: 96 }} data-testid="ss-mockup-item">
|
||
<label style={{ display: 'block', cursor: 'pointer' }}>
|
||
<img src={imageUrl(im.id)} alt={`Mockup ${im.name}`} style={{ ...thumb, width: 96, height: 72, outline: on ? '2px solid #a78bfa' : 'none' }} />
|
||
<span style={{ display: 'flex', alignItems: 'center', gap: 4, fontSize: 11, color: '#d4d4d8', marginTop: 3 }}>
|
||
<input type="checkbox" checked={on} disabled={off} onChange={() => toggle(im.id)} aria-label={`Use ${im.name} for the next request`} />
|
||
<span style={{ overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{im.name}</span>
|
||
</span>
|
||
</label>
|
||
<div style={{ fontSize: 10, color: '#71717a' }}>{im.width && im.height ? `${im.width}×${im.height} · ` : ''}{formatBytes(im.size)}</div>
|
||
<button type="button" style={{ ...btnDanger, marginTop: 3, padding: '2px 6px', fontSize: 11 }} disabled={off} onClick={() => void remove(im.id)} aria-label={`Remove ${im.name}`}>Remove</button>
|
||
</li>
|
||
);
|
||
})}
|
||
</ul>
|
||
)}
|
||
</div>
|
||
);
|
||
};
|