|
| 1 | +import { Box, Button, Checkbox, FormControl, FormLabel, Input, VStack } from "@chakra-ui/react"; |
| 2 | +import React, { useState, Dispatch, SetStateAction } from "react"; |
| 3 | +import { createGraph, rdfGraphToNodes } from "../utils"; |
| 4 | +import { GraphData } from "react-force-graph-3d"; |
| 5 | + |
| 6 | +const parseRDF = async ( |
| 7 | + rdfData: string, |
| 8 | + removeUnconnectedNodes: boolean, |
| 9 | + callback: (data: GraphData) => void |
| 10 | +) => { |
| 11 | + const store = createGraph(rdfData, "http://schema.org/"); |
| 12 | + const graphData = rdfGraphToNodes(store, removeUnconnectedNodes); |
| 13 | + callback(graphData); |
| 14 | +}; |
| 15 | + |
| 16 | +interface SelectionsProps { |
| 17 | + setGraphData: Dispatch<SetStateAction<GraphData>>; |
| 18 | +} |
| 19 | + |
| 20 | +const Selections: React.FC<SelectionsProps> = ({ setGraphData }: SelectionsProps) => { |
| 21 | + const [file, setFile] = useState<File | null>(null); |
| 22 | + const [isChecked, setIsChecked] = useState(false); |
| 23 | + |
| 24 | + const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => { |
| 25 | + const selectedFile = event.target.files?.[0] || null; |
| 26 | + setFile(selectedFile); |
| 27 | + }; |
| 28 | + |
| 29 | + const handleCheckboxChange = (event: React.ChangeEvent<HTMLInputElement>) => { |
| 30 | + setIsChecked(event.target.checked); |
| 31 | + }; |
| 32 | + |
| 33 | + const handleSubmit = () => { |
| 34 | + if (!file) { |
| 35 | + console.log("No file selected."); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + const reader = new FileReader(); |
| 40 | + |
| 41 | + // Add the load event listener |
| 42 | + reader.onload = () => { |
| 43 | + const fileContent = reader.result; |
| 44 | + console.log("File Content:", fileContent); // Log the content of the file |
| 45 | + if (typeof fileContent === "string") { |
| 46 | + // Ensure the content is a string |
| 47 | + parseRDF(fileContent, isChecked, setGraphData); |
| 48 | + } else { |
| 49 | + console.error("File content is not a string."); |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + // Add error event listener for debugging |
| 54 | + reader.onerror = (error) => { |
| 55 | + console.error("Error reading file:", error); |
| 56 | + }; |
| 57 | + |
| 58 | + console.log("Reading file:", file.name); |
| 59 | + reader.readAsText(file); // Read the file as text |
| 60 | + }; |
| 61 | + |
| 62 | + return ( |
| 63 | + <Box p={4} borderWidth="1px" borderRadius="lg" width="100%" mx="auto"> |
| 64 | + <VStack spacing={4}> |
| 65 | + <FormControl> |
| 66 | + <FormLabel htmlFor="file-upload">Upload a file</FormLabel> |
| 67 | + <Input id="file-upload" type="file" accept=".rdf, .ttl" onChange={handleFileChange} /> |
| 68 | + </FormControl> |
| 69 | + |
| 70 | + <FormControl> |
| 71 | + <Checkbox isChecked={isChecked} onChange={handleCheckboxChange}> |
| 72 | + Remove nodes that are not linked |
| 73 | + </Checkbox> |
| 74 | + </FormControl> |
| 75 | + |
| 76 | + <Button colorScheme="blue" onClick={handleSubmit}> |
| 77 | + Submit |
| 78 | + </Button> |
| 79 | + </VStack> |
| 80 | + </Box> |
| 81 | + ); |
| 82 | +}; |
| 83 | + |
| 84 | +export default Selections; |
0 commit comments