import { useState, useEffect } from '@wordpress/element';
import {Icon, chevronDownSmall} from '@wordpress/icons';
import { Button, Modal } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import LCPDataGrid from './LCPDataGrid';
const LCPDatasetBuilder = ({ attributes, setAttributes }) => {
const [isOpen, setIsOpen] = useState(false);
const [activeTab, setActiveTab] = useState(0); // Track the active tab
const [datasets, setDatasets] = useState(attributes.datasets); // Use useState for datasets
// Sync state with attributes if they change outside the component
useEffect(() => {
setDatasets(attributes.datasets);
}, [attributes.datasets]);
const handleUpdateDataset = (index, newData) => {
// console.log("newData (formatted for copy-pasting):", JSON.stringify(newData, null, 2));
// Check if newData.data is an array
if (Array.isArray(newData.data)) {
// Create a new reference for the datasets array
const updatedDatasets = datasets.map((dataset, i) => {
if (i === index) {
// If this is the dataset we're updating, replace it with a new object
return {
...dataset, // Copy the existing dataset
data: [...newData.data], // Deep copy the data array
};
}
return dataset; // Leave other datasets unchanged
});
// console.log("Updated Datasets:", JSON.stringify(updatedDatasets, null, 2));
// Call setAttributes with the updated datasets
setAttributes({
...attributes, // Ensure we're not overriding other attributes accidentally
datasets: updatedDatasets, // Set the new datasets array
});
// console.log("Attributes after setAttributes:", JSON.stringify({ ...attributes, datasets: updatedDatasets }, null, 2));
} else {
// console.error("Invalid data format in newData:", newData);
}
};
return (
<>
{isOpen && (
setIsOpen(false)}
title={__('Dataset Builder', 'lcp-visualize')}
style={{ width: '90vw', height: '90vh',
background:'#efefef',
padding:'0px',
}}
>