import { useRef } from 'react'; import type { MediaTarget, usePlaceMedia } from './usePlaceMedia'; /** * Adding a picture or file: one target opens the file dialog at once; several ask * first where it goes (a trip keeps any file, a list a picture of the place). * The trigger is the card's action, or the empty card's own wide button. */ export function useMediaAdd(m: ReturnType) { const input = useRef(null); const target = useRef([]); /** One trip or list, or every one of them at once (`'all'`). */ const pick = (next: MediaTarget | 'all') => { target.current = next === 'all' ? m.targets : [next]; m.setChoosing(false); if (input.current) { input.current.accept = target.current.every((x) => x.kind === 'list') ? 'image/*' : ''; input.current.click(); } }; const start = () => { if (m.targets.length === 1) pick(m.targets[0]); else m.setChoosing(!m.choosing); }; const field = ( { const files = Array.from(e.target.files ?? []); e.target.value = ''; if (target.current.length) void m.upload(target.current, files); }} /> ); return { start, pick, field }; }