Skip to content

feat: allow multiple file uploads #253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client'

import React, { JSX, useCallback, useState } from 'react'
import React, { JSX, useCallback, useState, useRef } from 'react'
import WebRTCPeerProvider from '../components/WebRTCProvider'
import DropZone from '../components/DropZone'
import UploadFileList from '../components/UploadFileList'
Expand Down Expand Up @@ -59,22 +59,56 @@ function ConfirmUploadState({
onCancel,
onStart,
onRemoveFile,
onAddMoreFiles,
}: {
uploadedFiles: UploadedFile[]
password: string
onChangePassword: (pw: string) => void
onCancel: () => void
onStart: () => void
onRemoveFile: (index: number) => void
onAddMoreFiles: (files: File[]) => void
}): JSX.Element {
const fileListData = useUploaderFileListData(uploadedFiles)
const fileInputRef = useRef<HTMLInputElement>(null)

const handleAddMoreClick = useCallback(() => {
fileInputRef.current?.click()
}, [])

const handleFileInputChange = useCallback(
(e: React.ChangeEvent<HTMLInputElement>) => {
if (e.target.files) {
const files = Array.from(e.target.files) as UploadedFile[]
onAddMoreFiles(files)
e.target.value = ''
}
},
[onAddMoreFiles],
)

return (
<PageWrapper>
<TitleText>
You are about to start uploading{' '}
{pluralize(uploadedFiles.length, 'file', 'files')}.
</TitleText>
<UploadFileList files={fileListData} onRemove={onRemoveFile} />
<div className="flex justify-center w-full">
<button
onClick={handleAddMoreClick}
className="px-4 py-2 text-sm transition-colors bg-transparent border rounded-md text-stone-700 dark:text-stone-300 border-stone-400 dark:border-stone-600 hover:bg-stone-100 dark:hover:bg-stone-800"
>
Add more files
</button>
</div>
<input
type="file"
ref={fileInputRef}
className="hidden"
onChange={handleFileInputChange}
multiple
/>
<PasswordField value={password} onChange={onChangePassword} />
<div className="flex space-x-4">
<CancelButton onClick={onCancel} />
Expand Down Expand Up @@ -113,7 +147,7 @@ export default function UploadPage(): JSX.Element {
const [uploading, setUploading] = useState(false)

const handleDrop = useCallback((files: UploadedFile[]): void => {
setUploadedFiles(files)
setUploadedFiles((currentFiles) => [...currentFiles, ...files])
}, [])

const handleChangePassword = useCallback((pw: string) => {
Expand All @@ -137,6 +171,13 @@ export default function UploadPage(): JSX.Element {
setUploadedFiles((fs) => fs.filter((_, i) => i !== index))
}, [])

const handleAddMoreFiles = useCallback((files: File[]) => {
setUploadedFiles((currentFiles) => [
...currentFiles,
...(files as UploadedFile[]),
])
}, [])

if (!uploadedFiles.length) {
return <InitialState onDrop={handleDrop} />
}
Expand All @@ -150,6 +191,7 @@ export default function UploadPage(): JSX.Element {
onCancel={handleCancel}
onStart={handleStart}
onRemoveFile={handleRemoveFile}
onAddMoreFiles={handleAddMoreFiles}
/>
)
}
Expand Down