-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
272 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { Box, TextField, IconButton, Typography } from '@mui/material' | ||
import { Add, Delete } from '@mui/icons-material' | ||
import { useState } from 'react' | ||
|
||
interface LabelsInputProps { | ||
onChange: (labels: Record<string, string>) => void | ||
disabled: boolean | ||
} | ||
|
||
const LabelsInput = ({ onChange, disabled }: LabelsInputProps) => { | ||
const [labels, setLabels] = useState<Record<string, string>>({}) | ||
const [newKey, setNewKey] = useState('') | ||
const [newValue, setNewValue] = useState('') | ||
|
||
const handleAddLabel = () => { | ||
if (newKey && newValue) { | ||
const updatedLabels = { ...labels, [newKey]: newValue } | ||
setLabels(updatedLabels) | ||
onChange(updatedLabels) | ||
setNewKey('') | ||
setNewValue('') | ||
} | ||
} | ||
|
||
const handleDeleteLabel = (key: string) => { | ||
const { [key]: _, ...remainingLabels } = labels | ||
setLabels(remainingLabels) | ||
onChange(remainingLabels) | ||
} | ||
|
||
const handleChange = | ||
(setValue: React.Dispatch<React.SetStateAction<string>>) => | ||
(e: React.ChangeEvent<HTMLInputElement>) => { | ||
const regex = /^[a-zA-Z0-9-_]*$/ | ||
if (regex.test(e.target.value)) { | ||
setValue(e.target.value) | ||
} | ||
} | ||
|
||
return ( | ||
<Box> | ||
<Typography variant="h6" component="h2" sx={{ mt: 2 }}> | ||
Labels | ||
</Typography> | ||
<Box display="flex" gap={1} sx={{ mt: 2 }}> | ||
<TextField | ||
disabled={disabled} | ||
label="Key" | ||
value={newKey} | ||
onChange={handleChange(setNewKey)} | ||
variant="outlined" | ||
/> | ||
<TextField | ||
disabled={disabled} | ||
label="Value" | ||
value={newValue} | ||
onChange={handleChange(setNewValue)} | ||
variant="outlined" | ||
/> | ||
<IconButton | ||
disabled={disabled} | ||
color="primary" | ||
onClick={handleAddLabel} | ||
> | ||
<Add /> | ||
</IconButton> | ||
</Box> | ||
{Object.entries(labels).map(([key, value]) => ( | ||
<Box | ||
display="flex" | ||
alignItems="center" | ||
gap={1} | ||
key={key} | ||
sx={{ mt: 2 }} | ||
> | ||
<TextField | ||
label="Key" | ||
value={key} | ||
InputProps={{ | ||
readOnly: true, | ||
}} | ||
variant="outlined" | ||
/> | ||
<TextField | ||
label="Value" | ||
value={value} | ||
InputProps={{ | ||
readOnly: true, | ||
}} | ||
variant="outlined" | ||
/> | ||
<IconButton | ||
disabled={disabled} | ||
color="secondary" | ||
onClick={() => handleDeleteLabel(key)} | ||
> | ||
<Delete /> | ||
</IconButton> | ||
</Box> | ||
))} | ||
</Box> | ||
) | ||
} | ||
|
||
export default LabelsInput |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import UploadIcon from '@mui/icons-material/Upload' | ||
import { | ||
Box, | ||
Button, | ||
Modal, | ||
Typography, | ||
MenuItem, | ||
Select, | ||
Paper, | ||
CircularProgress, | ||
} from '@mui/material' | ||
import { ExtractionGraph, IndexifyClient } from 'getindexify' | ||
import { useState } from 'react' | ||
import LabelsInput from './Inputs/LabelsInput' | ||
|
||
interface Props { | ||
client: IndexifyClient | ||
} | ||
|
||
const UploadButton = ({ client }: Props) => { | ||
const [open, setOpen] = useState(false) | ||
const [file, setFile] = useState<Blob | null>(null) | ||
const [labels, setLabels] = useState<Record<string, string>>({}) | ||
const [fileName, setFileName] = useState('') | ||
const [extractionGraphName, setExtractionGraphName] = useState('') | ||
const [loading, setLoading] = useState(false) | ||
const [extractionGraphs, setExtractionGraphs] = useState<ExtractionGraph[]>( | ||
client.extractionGraphs | ||
) | ||
|
||
const handleOpen = () => setOpen(true) | ||
const handleClose = () => setOpen(false) | ||
|
||
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
if (event.target.files && event.target.files[0]) { | ||
setFile(event.target.files[0]) | ||
setFileName(event.target.files[0].name) | ||
} | ||
} | ||
|
||
const modalStyle = { | ||
position: 'absolute' as 'absolute', | ||
top: '50%', | ||
left: '50%', | ||
transform: 'translate(-50%, -50%)', | ||
width: '80%', | ||
maxWidth: '800px', | ||
bgcolor: 'background.paper', | ||
maxHeight: '100%', | ||
overflow: 'scroll', | ||
boxShadow: 24, | ||
p: 4, | ||
} | ||
|
||
const upload = async () => { | ||
if (file && extractionGraphName) { | ||
setLoading(true) | ||
await client.uploadFile(extractionGraphName, file, labels) | ||
window.location.reload() | ||
} | ||
} | ||
|
||
const updateExtractionGraphs = async () => { | ||
const graphs = await client.getExtractionGraphs() | ||
setExtractionGraphs(graphs) | ||
} | ||
|
||
return ( | ||
<> | ||
<Modal | ||
open={open} | ||
onClose={handleClose} | ||
aria-labelledby="modal-modal-title" | ||
aria-describedby="modal-modal-description" | ||
> | ||
<Paper sx={modalStyle}> | ||
<Typography id="modal-modal-title" variant="h6" component="h2"> | ||
Upload Content | ||
</Typography> | ||
<Typography id="modal-modal-description" sx={{ mt: 2 }}> | ||
Select a file to upload and choose an extraction graph. | ||
</Typography> | ||
<Select | ||
disabled={loading} | ||
onFocus={updateExtractionGraphs} | ||
value={extractionGraphName} | ||
onChange={(e) => setExtractionGraphName(e.target.value)} | ||
displayEmpty | ||
fullWidth | ||
sx={{ mt: 2 }} | ||
> | ||
<MenuItem value="" disabled> | ||
Select Extraction Graph | ||
</MenuItem> | ||
{extractionGraphs.map((graph) => ( | ||
<MenuItem key={graph.name} value={graph.name}> | ||
{graph.name} | ||
</MenuItem> | ||
))} | ||
</Select> | ||
<Box display="flex" alignItems={'center'} gap={2}> | ||
<Button | ||
disabled={loading} | ||
variant="contained" | ||
component="label" | ||
sx={{ mt: 2 }} | ||
> | ||
Choose File | ||
<input type="file" hidden onChange={handleFileChange} /> | ||
</Button> | ||
{fileName && ( | ||
<Typography variant="body2" sx={{ mt: 1 }}> | ||
Selected File: {fileName} | ||
</Typography> | ||
)} | ||
</Box> | ||
<LabelsInput | ||
disabled={loading} | ||
onChange={(val) => { | ||
setLabels(val) | ||
}} | ||
/> | ||
|
||
<Box sx={{ mt: 2, position: 'relative' }}> | ||
<Button | ||
variant="contained" | ||
onClick={upload} | ||
disabled={!file || !extractionGraphName || loading} | ||
> | ||
Upload | ||
</Button> | ||
{loading && ( | ||
<CircularProgress | ||
size={24} | ||
sx={{ | ||
position: 'absolute', | ||
top: '50%', | ||
left: '50%', | ||
marginTop: '-12px', | ||
marginLeft: '-12px', | ||
}} | ||
/> | ||
)} | ||
</Box> | ||
</Paper> | ||
</Modal> | ||
<Button onClick={handleOpen} size="small" endIcon={<UploadIcon />}> | ||
Upload | ||
</Button> | ||
</> | ||
) | ||
} | ||
|
||
export default UploadButton |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters