refactor(create): update location state to keep latest selected files
All checks were successful
Open PR on Staging / audit_and_check (pull_request) Successful in 43s

This commit is contained in:
enes 2024-12-23 11:10:23 +01:00
parent abea8dcd15
commit 1827a20755

View File

@ -79,7 +79,7 @@ import {
} from '@fortawesome/free-solid-svg-icons' } from '@fortawesome/free-solid-svg-icons'
import { getSigitFile, SigitFile } from '../../utils/file.ts' import { getSigitFile, SigitFile } from '../../utils/file.ts'
import { generateTimestamp } from '../../utils/opentimestamps.ts' import { generateTimestamp } from '../../utils/opentimestamps.ts'
import { Autocomplete } from '@mui/lab' import { Autocomplete } from '@mui/material'
import _, { truncate } from 'lodash' import _, { truncate } from 'lodash'
import * as React from 'react' import * as React from 'react'
import { AvatarIconButton } from '../../components/UserAvatarIconButton' import { AvatarIconButton } from '../../components/UserAvatarIconButton'
@ -99,7 +99,7 @@ export const CreatePage = () => {
const [title, setTitle] = useState(`sigit_${formatTimestamp(Date.now())}`) const [title, setTitle] = useState(`sigit_${formatTimestamp(Date.now())}`)
const [selectedFiles, setSelectedFiles] = useState<File[]>([]) const [selectedFiles, setSelectedFiles] = useState<File[]>([...uploadedFiles])
const fileInputRef = useRef<HTMLInputElement>(null) const fileInputRef = useRef<HTMLInputElement>(null)
const handleUploadButtonClick = () => { const handleUploadButtonClick = () => {
if (fileInputRef.current) { if (fileInputRef.current) {
@ -378,12 +378,6 @@ export const CreatePage = () => {
}) })
}, [metadata, users]) }, [metadata, users])
useEffect(() => {
if (uploadedFiles) {
setSelectedFiles([...uploadedFiles])
}
}, [uploadedFiles])
useEffect(() => { useEffect(() => {
if (usersPubkey) { if (usersPubkey) {
setUsers((prev) => { setUsers((prev) => {
@ -561,11 +555,16 @@ export const CreatePage = () => {
const files = Array.from(event.target.files) const files = Array.from(event.target.files)
// Remove duplicates based on the file.name // Remove duplicates based on the file.name
setSelectedFiles((p) => setSelectedFiles((p) => {
[...p, ...files].filter( const unique = [...p, ...files].filter(
(file, i, array) => i === array.findIndex((t) => t.name === file.name) (file, i, array) => i === array.findIndex((t) => t.name === file.name)
) )
) navigate('.', {
state: { uploadedFiles: unique },
replace: true
})
return unique
})
} }
} }
@ -579,9 +578,14 @@ export const CreatePage = () => {
) => { ) => {
event.stopPropagation() event.stopPropagation()
setSelectedFiles((prevFiles) => setSelectedFiles((prevFiles) => {
prevFiles.filter((file) => file.name !== fileToRemove.name) const files = prevFiles.filter((file) => file.name !== fileToRemove.name)
) navigate('.', {
state: { uploadedFiles: files },
replace: true
})
return files
})
} }
// Validate inputs before proceeding // Validate inputs before proceeding
@ -1038,7 +1042,6 @@ export const CreatePage = () => {
try { try {
return JSON.parse(event.content) return JSON.parse(event.content)
} catch (e) { } catch (e) {
console.error(e)
return undefined return undefined
} }
} }