File size: 6,534 Bytes
8fc8501 f7733a1 8fc8501 f7733a1 8fc8501 79008fb 8fc8501 f7733a1 8fc8501 f7733a1 8fc8501 79008fb 8fc8501 f7733a1 8fc8501 f7733a1 8fc8501 79008fb f7733a1 79008fb f7733a1 79008fb 8fc8501 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | import { useEffect, useState } from "react";
import { FileText, Trash2, BarChart3, Database, AlertCircle } from "lucide-react";
import type { EmbedStore } from "../editor/embeds/embed-store";
import { isDraftDirty, type EmbedDraftStore } from "../editor/embeds/embed-drafts";
import type { EmbedDataFileMeta } from "../editor/embeds/embed-data-store";
import { UploadZone } from "./UploadZone";
import { formatBytes } from "../utils/data-files";
import type { UploadResult } from "../hooks/useEmbedData";
interface FilesSidebarProps {
embedStore: EmbedStore | null;
/** Studio drafts: new charts appear in the list, unapplied changes get a badge. */
drafts?: EmbedDraftStore | null;
currentSrc: string;
dataFiles: EmbedDataFileMeta[];
selectedDataFile: string | null;
/**
* Switch the embed studio to another chart by filename. Optional -
* when omitted, the chart list renders as read-only labels (the
* previous behaviour).
*/
onSelectChart?: (name: string) => void;
onSelectDataFile: (name: string | null) => void;
onUploadFiles: (files: FileList | File[]) => Promise<UploadResult[]>;
onRemoveDataFile: (name: string) => void;
}
function chartNames(embedStore: EmbedStore | null, drafts: EmbedDraftStore | null | undefined): string[] {
const names = new Set(embedStore ? embedStore.keys() : []);
for (const k of drafts?.keys() ?? []) names.add(k);
return Array.from(names);
}
export function FilesSidebar({
embedStore,
drafts,
currentSrc,
dataFiles,
selectedDataFile,
onSelectChart,
onSelectDataFile,
onUploadFiles,
onRemoveDataFile,
}: FilesSidebarProps) {
const [charts, setCharts] = useState<string[]>(() => chartNames(embedStore, drafts));
const [uploadError, setUploadError] = useState<string | null>(null);
useEffect(() => {
const update = () => setCharts(chartNames(embedStore, drafts));
update();
const offEmbeds = embedStore?.observe(update);
const offDrafts = drafts?.observe(update);
return () => {
offEmbeds?.();
offDrafts?.();
};
}, [embedStore, drafts]);
const handleUpload = async (files: FileList | File[]) => {
setUploadError(null);
const results = await onUploadFiles(files);
const firstError = results.find((r) => !r.ok);
if (firstError?.error) setUploadError(firstError.error);
const firstOk = results.find((r) => r.ok && r.name);
if (firstOk?.name) onSelectDataFile(firstOk.name);
};
return (
<div className="es-files">
<section className="es-files__section">
<header className="es-files__section-title">
<BarChart3 size={13} />
<span>Charts</span>
<span className="es-files__count">{charts.length}</span>
</header>
<ul className="es-files__list">
{charts.length === 0 && (
<li className="es-files__empty">No charts yet</li>
)}
{charts.map((name) => {
const isCurrent = name === currentSrc;
const draft = drafts?.get(name);
const draftBadge = draft?.isNew && !embedStore?.has(name) ? "new" : isDraftDirty(draft) ? "draft" : null;
const className = `es-files__item ${isCurrent ? "es-files__item--current" : ""}`;
const inner = (
<>
<FileText size={12} />
<span className="es-files__name">{name}</span>
{draftBadge && !isCurrent && (
<span className="es-files__badge es-files__badge--draft" title="Changes not applied to the article yet">
{draftBadge}
</span>
)}
{isCurrent && (
<span className="es-files__badge">editing</span>
)}
</>
);
if (!onSelectChart || isCurrent) {
return (
<li
key={name}
className={className}
title={isCurrent ? "Current chart" : name}
>
{inner}
</li>
);
}
return (
<li key={name} className={className}>
<button
type="button"
className="es-files__item-main"
onClick={() => onSelectChart(name)}
title={`Switch to ${name}`}
>
{inner}
</button>
</li>
);
})}
</ul>
</section>
<section className="es-files__section">
<header className="es-files__section-title">
<Database size={13} />
<span>Data</span>
<span className="es-files__count">{dataFiles.length}</span>
</header>
<ul className="es-files__list">
{dataFiles.length === 0 && (
<li className="es-files__empty">No data uploaded yet</li>
)}
{dataFiles.map((file) => {
const isSelected = file.name === selectedDataFile;
return (
<li
key={file.name}
className={`es-files__item es-files__item--data ${isSelected ? "es-files__item--current" : ""}`}
>
<button
type="button"
className="es-files__item-main"
onClick={() => onSelectDataFile(isSelected ? null : file.name)}
title={file.name}
>
<FileText size={12} />
<span className="es-files__name">{file.name}</span>
<span className="es-files__size">{formatBytes(file.size)}</span>
</button>
<button
type="button"
className="es-files__item-remove"
onClick={(e) => {
e.stopPropagation();
if (isSelected) onSelectDataFile(null);
onRemoveDataFile(file.name);
}}
aria-label={`Remove ${file.name}`}
title="Remove file"
>
<Trash2 size={12} />
</button>
</li>
);
})}
</ul>
<UploadZone onFiles={handleUpload} compact={dataFiles.length > 0} />
{uploadError && (
<div className="es-files__error">
<AlertCircle size={12} />
<span>{uploadError}</span>
</div>
)}
</section>
</div>
);
}
|