refactor: utilities function

This commit is contained in:
2025-10-15 11:35:39 +03:00
parent 45ef0c61c7
commit a8f5ba44ea
3 changed files with 23 additions and 30 deletions

View File

@@ -64,6 +64,14 @@ const AppSettings = {
} }
}; };
// Shared utility: Format bytes for display
function formatBytes(bytes) {
if (bytes === null || bytes === undefined) return 'N/A';
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
return `${(bytes / (1024 * 1024)).toFixed(2)} MB`;
}
// Sample Vega-Lite specification // Sample Vega-Lite specification
const sampleSpec = { const sampleSpec = {
"$schema": "https://vega.github.io/schema/vega-lite/v5.json", "$schema": "https://vega.github.io/schema/vega-lite/v5.json",

View File

@@ -216,11 +216,9 @@ const DatasetStorage = {
} }
}; };
// Format bytes for display // Helper: Get currently selected dataset
function formatDatasetSize(bytes) { async function getCurrentDataset() {
if (bytes < 1024) return `${bytes} B`; return window.currentDatasetId ? await DatasetStorage.getDataset(window.currentDatasetId) : null;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
return `${(bytes / (1024 * 1024)).toFixed(2)} MB`;
} }
// Fetch URL data and calculate metadata // Fetch URL data and calculate metadata
@@ -287,12 +285,12 @@ async function renderDatasetList() {
if (dataset.source === 'url') { if (dataset.source === 'url') {
// Show metadata if available, otherwise just URL and format // Show metadata if available, otherwise just URL and format
if (dataset.rowCount !== null && dataset.size !== null) { if (dataset.rowCount !== null && dataset.size !== null) {
metaText = `URL • ${dataset.rowCount} rows • ${dataset.format.toUpperCase()}${formatDatasetSize(dataset.size)}`; metaText = `URL • ${dataset.rowCount} rows • ${dataset.format.toUpperCase()}${formatBytes(dataset.size)}`;
} else { } else {
metaText = `URL • ${dataset.format.toUpperCase()}`; metaText = `URL • ${dataset.format.toUpperCase()}`;
} }
} else { } else {
metaText = `${dataset.rowCount} rows • ${dataset.format.toUpperCase()}${formatDatasetSize(dataset.size)}`; metaText = `${dataset.rowCount} rows • ${dataset.format.toUpperCase()}${formatBytes(dataset.size)}`;
} }
return ` return `
@@ -344,7 +342,7 @@ async function selectDataset(datasetId) {
document.getElementById('dataset-detail-comment').value = dataset.comment; document.getElementById('dataset-detail-comment').value = dataset.comment;
document.getElementById('dataset-detail-rows').textContent = dataset.rowCount !== null ? dataset.rowCount : 'N/A'; document.getElementById('dataset-detail-rows').textContent = dataset.rowCount !== null ? dataset.rowCount : 'N/A';
document.getElementById('dataset-detail-columns').textContent = dataset.columnCount !== null ? dataset.columnCount : 'N/A'; document.getElementById('dataset-detail-columns').textContent = dataset.columnCount !== null ? dataset.columnCount : 'N/A';
document.getElementById('dataset-detail-size').textContent = dataset.size !== null ? formatDatasetSize(dataset.size) : 'N/A'; document.getElementById('dataset-detail-size').textContent = formatBytes(dataset.size);
document.getElementById('dataset-detail-created').textContent = new Date(dataset.created).toLocaleString(); document.getElementById('dataset-detail-created').textContent = new Date(dataset.created).toLocaleString();
document.getElementById('dataset-detail-modified').textContent = new Date(dataset.modified).toLocaleString(); document.getElementById('dataset-detail-modified').textContent = new Date(dataset.modified).toLocaleString();
@@ -573,13 +571,11 @@ async function saveNewDataset() {
// Delete current dataset // Delete current dataset
async function deleteCurrentDataset() { async function deleteCurrentDataset() {
if (!window.currentDatasetId) return; const dataset = await getCurrentDataset();
const dataset = await DatasetStorage.getDataset(window.currentDatasetId);
if (!dataset) return; if (!dataset) return;
if (confirm(`Delete dataset "${dataset.name}"? This action cannot be undone.`)) { if (confirm(`Delete dataset "${dataset.name}"? This action cannot be undone.`)) {
await DatasetStorage.deleteDataset(window.currentDatasetId); await DatasetStorage.deleteDataset(dataset.id);
document.getElementById('dataset-details').style.display = 'none'; document.getElementById('dataset-details').style.display = 'none';
window.currentDatasetId = null; window.currentDatasetId = null;
await renderDatasetList(); await renderDatasetList();
@@ -587,22 +583,18 @@ async function deleteCurrentDataset() {
} }
// Copy dataset reference to clipboard // Copy dataset reference to clipboard
function copyDatasetReference() { async function copyDatasetReference() {
if (!window.currentDatasetId) return; const dataset = await getCurrentDataset();
if (!dataset) return;
DatasetStorage.getDataset(window.currentDatasetId).then(dataset => { const reference = `"data": {"name": "${dataset.name}"}`;
const reference = `"data": {"name": "${dataset.name}"}`; await navigator.clipboard.writeText(reference);
navigator.clipboard.writeText(reference).then(() => { alert('Dataset reference copied to clipboard!');
alert('Dataset reference copied to clipboard!');
});
});
} }
// Refresh metadata for URL dataset // Refresh metadata for URL dataset
async function refreshDatasetMetadata() { async function refreshDatasetMetadata() {
if (!window.currentDatasetId) return; const dataset = await getCurrentDataset();
const dataset = await DatasetStorage.getDataset(window.currentDatasetId);
if (!dataset || dataset.source !== 'url') return; if (!dataset || dataset.source !== 'url') return;
const refreshBtn = document.getElementById('refresh-metadata-btn'); const refreshBtn = document.getElementById('refresh-metadata-btn');

View File

@@ -796,13 +796,6 @@ function calculateStorageUsage() {
return new Blob([snippetsData]).size; return new Blob([snippetsData]).size;
} }
// Format bytes to human-readable size
function formatBytes(bytes) {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
return `${(bytes / (1024 * 1024)).toFixed(2)} MB`;
}
// Update storage monitor display // Update storage monitor display
function updateStorageMonitor() { function updateStorageMonitor() {
const usedBytes = calculateStorageUsage(); const usedBytes = calculateStorageUsage();