import {useRef, useState} from 'react'; import {useTranslation} from 'react-i18next'; import {toast} from 'sonner'; import {saveBacklogEntry, toBacklogRequest} from '../utils/backlog.js'; /** * Returns `{save, pending, busy}`. `save(field, value)` sends that one field (PATCH .../backlog; 0 or NONE clear it), * shows the saved or error toast and calls `onSaved(entry)` with the whole saved entry. One request at a time: a call * while one runs does nothing. Resolves to true when saved. * * `pending` is `{field, value}` while saving, else null. Show `pending.value` for that field meanwhile: a failed save * then falls back to the saved value by itself. */ export default function useSaveBacklogField(appid, {onSaved} = {}) { const {t} = useTranslation(); const [pending, setPending] = useState(null); const running = useRef(false); const save = async (field, value) => { if (running.current) return false; running.current = true; setPending({field, value}); try { const response = await saveBacklogEntry(appid, {[field]: toBacklogRequest({[field]: value})[field]}); if (response.status === 401) return false; if (!response.ok) { toast.error(t('BACKLOG.SAVE_FAILED')); return false; } const entry = await response.json(); onSaved?.(entry); toast.success(t('BACKLOG.SAVED')); return true; } catch (err) { console.error('backlog save error:', err); toast.error(t('INFO.ERROR.NETWORK_FAILURE')); return false; } finally { running.current = false; setPending(null); } }; return {save, pending, busy: pending !== null}; }