import { useEffect, useState } from 'react'; import { __, _x, sprintf } from '@wordpress/i18n'; import { api, cfg } from '../api.js'; import { Button, Card, Field, Icon, Notice, Switch, toast } from '../ui.jsx'; export default function Settings({ data, setData }) { const [s, setS] = useState(data.settings); const [busy, setBusy] = useState(false); const [err, setErr] = useState(''); const dirty = JSON.stringify(s) !== JSON.stringify(data.settings); const set = (k) => (v) => setS((x) => ({ ...x, [k]: v })); const status = data.status; useEffect(() => { const h = (e) => { if (dirty) { e.preventDefault(); e.returnValue = ''; } }; window.addEventListener('beforeunload', h); return () => window.removeEventListener('beforeunload', h); }, [dirty]); const pickLogo = () => { if (!window.wp?.media) return; const frame = window.wp.media({ title: __('Choose an app logo', 'dashbread'), button: { text: __('Use this logo', 'dashbread') }, library: { type: 'image' }, multiple: false }); frame.on('select', () => { const a = frame.state().get('selection').first().toJSON(); setS((x) => ({ ...x, logo_id: a.id, logo_url: a.sizes?.medium?.url || a.url })); }); frame.open(); }; const save = async (e) => { e.preventDefault(); setBusy(true); setErr(''); try { const res = await api.saveSettings(s); setData((d) => ({ ...d, settings: res.settings, status: res.status })); setS(res.settings); toast.show(__('Settings saved', 'dashbread')); } catch (ex) { setErr(ex.message); } setBusy(false); }; return (
); }