import { RenderConfigScreenCtx } from 'datocms-plugin-sdk'; import { Button, Canvas, CaretDownIcon, CaretUpIcon, Dropdown, DropdownMenu, DropdownOption, SelectField, TextField, } from 'datocms-react-ui'; import s from './styles.module.css'; import { useEffect, useState } from 'react'; import downloadAllRecords from '../utils/downloadAllRecords'; import downloadAllAssets from '../utils/downloadAllAssets'; import { buildClient } from '@datocms/cma-client-browser'; import LoadingOverlay from './LoadingOverlay'; type Props = { ctx: RenderConfigScreenCtx; }; type ModelObject = { name: string; id: string; }; export type AvailableFormats = 'JSON' | 'CSV' | 'XML' | 'XLSX'; export default function ConfigScreen({ ctx }: Props) { const [isLoading, setLoading] = useState(false); const [loadingStatus, setLoadingStatus] = useState(''); const [loadingProgress, setLoadingProgress] = useState( undefined ); const [isFilteredExportOpen, setIsFilteredExportOpen] = useState(false); const [selectedModels, setSelectedModels] = useState([]); const [allModels, setAllModels] = useState([]); const [selectedFormat, setSelectedFormat] = useState( (ctx.plugin.attributes.parameters.format as AvailableFormats) ?? 'JSON' ); const [textQuery, setTextQuery] = useState(''); useEffect(() => { const client = buildClient({ apiToken: ctx.currentUserAccessToken!, }); client.itemTypes.list().then((models) => { setAllModels( models .filter((model) => !model.modular_block) .map((model) => { return { name: model.name, id: model.id }; }) ); }); }, [ctx.currentUserAccessToken]); const handleRecordDownload = async ( options: { modelIDs?: string[]; textQuery?: string } = {} ) => { setLoading(true); setLoadingStatus('Initializing download...'); setLoadingProgress(0); await downloadAllRecords( ctx.currentUserAccessToken!, selectedFormat, options, (progress, msg) => { setLoadingStatus(msg); setLoadingProgress(progress); } ); setLoading(false); setLoadingStatus(''); setLoadingProgress(undefined); }; const handleAllAssets = async () => { setLoading(true); setLoadingStatus('Initializing asset download...'); setLoadingProgress(0); await downloadAllAssets( ctx.currentUserAccessToken as string, (progress, msg) => { setLoadingStatus(msg); setLoadingProgress(progress); } ); setLoading(false); setLoadingStatus(''); setLoadingProgress(undefined); }; return ( {isLoading && ( )}
Format for exports ( )} > { setSelectedFormat('JSON'); ctx .updatePluginParameters({ format: 'JSON', }) .then(() => { ctx.notice('Format for exports updated'); }); }} > JSON { setSelectedFormat('CSV'); ctx .updatePluginParameters({ format: 'CSV', }) .then(() => { ctx.notice('Format for exports updated'); }); }} > CSV { setSelectedFormat('XML'); ctx .updatePluginParameters({ format: 'XML', }) .then(() => { ctx.notice('Format for exports updated'); }); }} > XML { setSelectedFormat('XLSX'); ctx .updatePluginParameters({ format: 'XLSX', }) .then(() => { ctx.notice('Format for exports updated'); }); }} > XLSX
You can download a specific record from its own sidebar
{isFilteredExportOpen && (
{ return { label: model.name, value: model.id }; })} selectInputProps={{ isMulti: true, options: allModels.map((model) => { return { label: model.name, value: model.id }; }), }} onChange={(newValue) => setSelectedModels( newValue.map((model) => { return { name: model.label, id: model.value }; }) ) } />
setTextQuery(newValue)} />
)}
); }