/* global React, Icon */
/* Sidebar — list of saved SOPs with inline rename */
const { useState: useStateSB, useRef: useRefSB, useEffect: useEffectSB } = React;
function SidebarItem({ doc, active, onSelect, onRename, onDelete }) {
const [editing, setEditing] = useStateSB(false);
const [draft, setDraft] = useStateSB(doc.title || "");
const inputRef = useRefSB(null);
useEffectSB(() => {
if (editing && inputRef.current) {
inputRef.current.focus();
inputRef.current.select();
}
}, [editing]);
// Sync draft when doc title changes externally (e.g. AI regen)
useEffectSB(() => { if (!editing) setDraft(doc.title || ""); }, [doc.title, editing]);
const commit = () => {
const v = draft.trim();
if (v && v !== doc.title) onRename(doc.id, v);
else setDraft(doc.title || "");
setEditing(false);
};
const cancel = () => {
setDraft(doc.title || "");
setEditing(false);
};
const label = doc.title?.trim() || "Sin título";
const date = doc.date || "—";
return (
!editing && onSelect(doc.id)}
onDoubleClick={(e) => { e.stopPropagation(); setEditing(true); }}
onKeyDown={(e) => {
if (editing) return;
if (e.key === "Enter" || e.key === " ") { e.preventDefault(); onSelect(doc.id); }
if (e.key === "F2") { e.preventDefault(); setEditing(true); }
}}
>
{editing ? (
setDraft(e.target.value)}
onBlur={commit}
onClick={(e) => e.stopPropagation()}
onKeyDown={(e) => {
e.stopPropagation();
if (e.key === "Enter") commit();
if (e.key === "Escape") cancel();
}}
placeholder="Título del SOP"
/>
) : (
{label}
)}
v{doc.version || "1.0"}
·
{date}
);
}
function Sidebar({ docs, activeId, onSelect, onNew, onDelete, onRename }) {
const sorted = [...docs].sort((a, b) => (b.updatedAt || 0) - (a.updatedAt || 0));
return (
);
}
window.Sidebar = Sidebar;