const Sidebar = ({ children, create, append, polys, activePolyId, setActivePoint }) => { const sidebarOpenKey = "sidebar-open"; const sidebarInit = localStorage.getItem(sidebarOpenKey); const [sidebarOpen, setSidebarOpen] = React.useState((sidebarInit === null || sidebarInit === 'true')); const [ptLabel, setPtLabel] = React.useState(""); const pform = React.useRef(null); function labelMatch(point, search) { return point.label.toLowerCase().includes(search.toLowerCase()); } function canAppend() { return activePolyId === undefined || activePolyId === "" } function handleSearchSubmit(e) { e.preventDefault(); const data = new FormData(e.target); const search = data.get("searchbox"); let matches = polys.filter(poly => { return poly.points.some(point => point.label && labelMatch(point, search)) }).map(poly => { return poly.points.filter(point => { return point.label && labelMatch(point, search); }); }); if (matches && matches.length > 0 && matches[0].length > 0) { setActivePoint(matches[0][0]); } } function toPoint(form) { const data = new FormData(form); return { x: data.get("x"), y: data.get("y"), label: data.get("label"), biome: data.get("biome"), }; } function handlePolyAppend(e) { e.preventDefault(); const point = toPoint(e.target); setActivePoint(point); append(point).then(() => { setPtLabel("") }); } function handlePolyCreate(kind) { const point = toPoint(pform.current); setActivePoint(point); create(point, kind).then(() => { setPtLabel("") }); } return ( ) }