import PropTypes from 'prop-types'; import { useTranslation } from 'react-i18next'; import { useDispatch } from 'react-redux'; import userService from '../api/services/userService'; import SyncIcon from '../assets/sync.svg'; import Trash from '../assets/trash.svg'; import DropdownMenu from '../components/DropdownMenu'; import { Doc, DocumentsProps } from '../models/misc'; import { getDocs } from '../preferences/preferenceApi'; import { setSourceDocs } from '../preferences/preferenceSlice'; // Utility function to format numbers const formatTokens = (tokens: number): string => { const roundToTwoDecimals = (num: number): string => { return (Math.round((num + Number.EPSILON) * 100) / 100).toString(); }; if (tokens >= 1_000_000_000) { return roundToTwoDecimals(tokens / 1_000_000_000) + 'b'; } else if (tokens >= 1_000_000) { return roundToTwoDecimals(tokens / 1_000_000) + 'm'; } else if (tokens >= 1_000) { return roundToTwoDecimals(tokens / 1_000) + 'k'; } else { return tokens.toString(); } }; const Documents: React.FC = ({ documents, handleDeleteDocument, }) => { const { t } = useTranslation(); const dispatch = useDispatch(); const syncOptions = [ { label: 'Never', value: 'never' }, { label: 'Daily', value: 'daily' }, { label: 'Weekly', value: 'weekly' }, { label: 'Monthly', value: 'monthly' }, ]; const handleManageSync = (doc: Doc, sync_frequency: string) => { userService .manageSync({ source_id: doc.id, sync_frequency }) .then(() => { return getDocs(); }) .then((data) => { dispatch(setSourceDocs(data)); }) .catch((error) => console.error(error)); }; return (
{!documents?.length && ( )} {documents && documents.map((document, index) => ( ))}
{t('settings.documents.name')} {t('settings.documents.date')} {t('settings.documents.tokenUsage')} {t('settings.documents.type')}
{t('settings.documents.noData')}
{document.name} {document.date} {document.tokens ? formatTokens(+document.tokens) : ''} {document.type === 'remote' ? 'Pre-loaded' : 'Private'}
{document.type !== 'remote' && ( Delete { event.stopPropagation(); handleDeleteDocument(index, document); }} /> )} {document.syncFrequency && (
{ handleManageSync(document, value); }} defaultValue={document.syncFrequency} icon={SyncIcon} />
)}
); }; Documents.propTypes = { documents: PropTypes.array.isRequired, handleDeleteDocument: PropTypes.func.isRequired, }; export default Documents;