@letanure/resend-cli
Version:
A command-line interface for Resend email API
53 lines • 3.14 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import { Alert, Spinner } from '@inkjs/ui';
import { Box, Text, useInput } from 'ink';
import React from 'react';
import { ErrorDisplay } from '../ui/ErrorDisplay.js';
import { Layout } from '../ui/layout.js';
import { Table } from '../ui/Table.js';
import { config } from '../../config/config.js';
import { useDryRun } from '../../contexts/DryRunProvider.js';
import { useResend } from '../../contexts/ResendProvider.js';
const DEFAULT_LOAD_DATA = {};
export function ListDisplay({ title, onExit, loadFunction, displayFields, formatData, loadData = DEFAULT_LOAD_DATA, noDataMessage = 'No items found.', }) {
const [result, setResult] = React.useState(null);
const [loading, setLoading] = React.useState(false);
const { isDryRun } = useDryRun();
const { apiKey } = useResend();
const handleLoad = React.useCallback(async () => {
setLoading(true);
const loadResult = await loadFunction(loadData, apiKey);
setResult(loadResult);
setLoading(false);
}, [loadFunction, loadData, apiKey]);
React.useEffect(() => {
if (!isDryRun) {
handleLoad();
}
}, [handleLoad, isDryRun]);
useInput((input, key) => {
if ((input === 'q' || key.escape || key.leftArrow) && !loading) {
onExit();
}
if (input === 'r' && !loading) {
handleLoad();
}
// Prevent Enter key from doing anything (no default action for lists)
if (key.return && !loading) {
// Do nothing - just consume the key event
return;
}
});
if (loading) {
return (_jsx(Layout, { headerText: `${config.baseTitle} - ${title}`, showNavigationInstructions: false, navigationContext: "none", children: _jsx(Box, { marginBottom: 1, children: _jsx(Spinner, { label: `Loading ${title.toLowerCase()}...` }) }) }));
}
if (result) {
if (result.success && result.data) {
const tableData = formatData(result.data);
return (_jsx(Layout, { headerText: `${config.baseTitle} - ${title}`, showNavigationInstructions: true, navigationContext: "result", footerText: "Press r to refresh", children: _jsx(Box, { flexDirection: "column", children: tableData.length === 0 ? (_jsx(Box, { marginBottom: 1, children: _jsx(Text, { color: "gray", children: noDataMessage }) })) : (_jsx(Table, { data: tableData, fields: displayFields, title: title })) }) }));
}
return (_jsx(Layout, { headerText: `${config.baseTitle} - ${title}`, showNavigationInstructions: true, navigationContext: "result", footerText: "Press r to retry", children: _jsx(ErrorDisplay, { message: result.error || `Failed to load ${title.toLowerCase()}` }) }));
}
return (_jsx(Layout, { headerText: `${config.baseTitle} - ${title}`, showNavigationInstructions: true, navigationContext: "result", footerText: "Press r to load", children: isDryRun && (_jsx(Box, { marginBottom: 1, children: _jsx(Alert, { variant: "warning", children: "DRY RUN MODE - No API calls will be made" }) })) }));
}
//# sourceMappingURL=ListDisplay.js.map