// Live, in-place text editing for logged-in admins (Divi-style: click the
// text on the page itself, type, click away, it's saved) instead of a
// separate modal disconnected from what you're actually looking at.
const AdminEditContext = React.createContext({
isAdmin: false,
editMode: false,
setEditMode: () => {},
lang: 'it',
refresh: () => {},
ov: (key, fallback) => fallback,
});
// site_content has it/de/pl columns directly; only de-CH needs mapping.
function contentApiLang(lang) {
return lang === 'de-CH' ? 'de' : lang;
}
// Editable list items (services, process steps, FAQ entries, ...) are fixed
// arrays in copy.js, not simple scalar CZ_EDITABLE_KEYS fields, so they're
// saved under synthetic keys like "svc_0_title" instead. This turns one of
// those keys plus its code default into the value to actually display.
function AdminEditProvider({ children, lang, overrides, t, onSaved }) {
const [isAdmin, setIsAdmin] = React.useState(false);
const [editMode, setEditMode] = React.useState(false);
const apiLang = contentApiLang(lang);
React.useEffect(() => {
fetch('/api/config-api.php/whoami')
.then((r) => (r.ok ? r.json() : null))
.then((data) => { if (data && data.loggedIn) setIsAdmin(true); })
.catch(() => {});
}, []);
// Reserve space at the very top of the page for the admin bar so it
// doesn't sit on top of the fixed site header — see .site-header{top:
// var(--admin-bar-h)} in navigation.css.
React.useEffect(() => {
document.documentElement.style.setProperty('--admin-bar-h', isAdmin ? '44px' : '0px');
return () => document.documentElement.style.setProperty('--admin-bar-h', '0px');
}, [isAdmin]);
const ov = React.useCallback(
(key, fallback) => (overrides && overrides[key] && overrides[key][apiLang]) || fallback,
[overrides, apiLang]
);
const value = React.useMemo(
() => ({ isAdmin, editMode, setEditMode, lang: apiLang, refresh: onSaved, ov, t }),
[isAdmin, editMode, apiLang, onSaved, ov, t]
);
return {children};
}
function AdminEditToolbar({ t }) {
const { isAdmin, editMode, setEditMode } = React.useContext(AdminEditContext);
const [loggingOut, setLoggingOut] = React.useState(false);
if (!isAdmin) return null;
const logout = () => {
setLoggingOut(true);
fetch('/panel/logout.php').finally(() => window.location.reload());
};
return (