cognee/cognee-frontend/src/modules/ingestion/DataView/DataView.tsx
Boris 0aac93e9c4
Merge dev to main (#827)
<!-- .github/pull_request_template.md -->

## Description
<!-- Provide a clear description of the changes in this PR -->

## DCO Affirmation
I affirm that all code in every commit of this pull request conforms to
the terms of the Topoteretes Developer Certificate of Origin.

---------

Co-authored-by: vasilije <vas.markovic@gmail.com>
Co-authored-by: Igor Ilic <30923996+dexters1@users.noreply.github.com>
Co-authored-by: Vasilije <8619304+Vasilije1990@users.noreply.github.com>
Co-authored-by: Igor Ilic <igorilic03@gmail.com>
Co-authored-by: Hande <159312713+hande-k@users.noreply.github.com>
Co-authored-by: Matea Pesic <80577904+matea16@users.noreply.github.com>
Co-authored-by: hajdul88 <52442977+hajdul88@users.noreply.github.com>
Co-authored-by: Daniel Molnar <soobrosa@gmail.com>
Co-authored-by: Diego Baptista Theuerkauf <34717973+diegoabt@users.noreply.github.com>
2025-05-15 13:15:49 +02:00

143 lines
3.9 KiB
TypeScript

import { useCallback, useState } from 'react';
import {
DropdownMenu,
GhostButton,
Stack,
Text,
UploadInput,
CloseIcon,
CTAButton,
useBoolean,
} from "ohmy-ui";
import { fetch } from '@/utils';
import RawDataPreview from './RawDataPreview';
import styles from "./DataView.module.css";
export interface Data {
id: string;
name: string;
mimeType: string;
extension: string;
rawDataLocation: string;
}
interface DatasetLike {
id: string;
}
interface DataViewProps {
data: Data[];
datasetId: string;
onClose: () => void;
onDataAdd: (dataset: DatasetLike, files: File[]) => void;
onCognify: () => Promise<any>;
}
export default function DataView({ datasetId, data, onClose, onDataAdd, onCognify }: DataViewProps) {
// const handleDataDelete = () => {};
const [rawData, setRawData] = useState<ArrayBuffer | null>(null);
const [selectedData, setSelectedData] = useState<Data | null>(null);
const showRawData = useCallback((dataItem: Data) => {
setSelectedData(dataItem);
fetch(`/v1/datasets/${datasetId}/data/${dataItem.id}/raw`)
.then((response) => response.arrayBuffer())
.then(setRawData);
document.body.click(); // Close the dropdown menu.
}, [datasetId]);
const resetDataPreview = useCallback(() => {
setSelectedData(null);
setRawData(null);
}, []);
const handleDataAdd = (files: File[]) => {
onDataAdd({ id: datasetId }, files);
};
const {
value: isCognifyButtonDisabled,
setTrue: disableCognifyButton,
setFalse: enableCognifyButton,
} = useBoolean(false);
const handleCognify = () => {
disableCognifyButton();
onCognify()
.finally(() => enableCognifyButton());
};
return (
<Stack orientation="vertical" gap="4">
<Stack gap="2" orientation="horizontal" align="/end">
<div>
<UploadInput onChange={handleDataAdd}>
<Text>Add data</Text>
</UploadInput>
</div>
<div>
<CTAButton disabled={isCognifyButtonDisabled} onClick={handleCognify}>
<Text>Cognify</Text>
</CTAButton>
</div>
<GhostButton hugContent onClick={onClose}>
<CloseIcon />
</GhostButton>
</Stack>
{rawData && selectedData && (
<RawDataPreview
fileName={selectedData.name}
rawData={rawData}
onClose={resetDataPreview}
/>
)}
<div className={styles.tableContainer}>
<table className={styles.dataTable}>
<thead>
<tr>
<th>Actions</th>
<th>ID</th>
<th>Name</th>
<th>File path</th>
<th>MIME type</th>
</tr>
</thead>
<tbody>
{data.map((dataItem) => (
<tr key={dataItem.id}>
<td>
<Stack orientation="horizontal" gap="2" align="center">
<DropdownMenu position="right">
<Stack gap="1" className={styles.datasetMenu} orientation="vertical">
<GhostButton onClick={() => showRawData(dataItem)}>
<Text>View raw data</Text>
</GhostButton>
{/* <NegativeButton onClick={handleDataDelete}>
<Text>Delete</Text>
</NegativeButton> */}
</Stack>
</DropdownMenu>
</Stack>
</td>
<td>
<Text>{dataItem.id}</Text>
</td>
<td>
<Text>{dataItem.name}.{dataItem.extension}</Text>
</td>
<td>
<Text>{dataItem.rawDataLocation}</Text>
</td>
<td>
<Text>{dataItem.mimeType}</Text>
</td>
</tr>
))}
</tbody>
</table>
</div>
</Stack>
);
}