From 36a70f1d2935777a587dc9b09f22e545802a6480 Mon Sep 17 00:00:00 2001 From: Alex Kern Date: Wed, 18 Sep 2024 00:22:56 -0700 Subject: [PATCH] some refactoring --- src/app/page.tsx | 32 +++++++++++++++---------------- src/components/Downloader.tsx | 17 ++++++++-------- src/components/SubtitleText.tsx | 11 +++++++++++ src/components/TitleText.tsx | 11 +++++++++++ src/components/UploadFileList.tsx | 23 ++++++++++------------ 5 files changed, 56 insertions(+), 38 deletions(-) create mode 100644 src/components/SubtitleText.tsx create mode 100644 src/components/TitleText.tsx diff --git a/src/app/page.tsx b/src/app/page.tsx index ca2e032..e59e918 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -14,6 +14,8 @@ import Wordmark from '../components/Wordmark' import CancelButton from '../components/CancelButton' import { useMemo } from 'react' import { getFileName } from '../fs' +import TitleText from '../components/TitleText' +import SubtitleText from '../components/SubtitleText' const queryClient = new QueryClient() @@ -26,7 +28,7 @@ function PageWrapper({ }): JSX.Element { return ( -
+
{children} @@ -43,12 +45,10 @@ function InitialState({ return (
-

- Peer-to-peer file transfers in your browser. -

-

+ Peer-to-peer file transfers in your browser. + We never store anything. Files only served fresh. -

+
@@ -70,23 +70,23 @@ function ConfirmUploadState({ onChangePassword, onCancel, onStart, - onFileListChange, + onRemoveFile, }: { uploadedFiles: UploadedFile[] password: string onChangePassword: (pw: string) => void onCancel: () => void onStart: () => void - onFileListChange: (updatedFiles: UploadedFile[]) => void + onRemoveFile: (index: number) => void }): JSX.Element { const fileListData = useUploaderFileListData(uploadedFiles) return ( -

+ You are about to start uploading {uploadedFiles.length}{' '} {uploadedFiles.length === 1 ? 'file' : 'files'}. -

- + +
@@ -108,10 +108,10 @@ function UploadingState({ const fileListData = useUploaderFileListData(uploadedFiles) return ( -

+ You are uploading {uploadedFiles.length}{' '} {uploadedFiles.length === 1 ? 'file' : 'files'}. -

+ @@ -146,8 +146,8 @@ export default function UploadPage(): JSX.Element { setUploading(false) }, []) - const handleFileListChange = useCallback((updatedFiles: UploadedFile[]) => { - setUploadedFiles(updatedFiles) + const handleRemoveFile = useCallback((index: number) => { + setUploadedFiles((fs) => fs.filter((_, i) => i !== index)) }, []) if (!uploadedFiles.length) { @@ -162,7 +162,7 @@ export default function UploadPage(): JSX.Element { onChangePassword={handleChangePassword} onCancel={handleCancel} onStart={handleStart} - onFileListChange={handleFileListChange} + onRemoveFile={handleRemoveFile} /> ) } diff --git a/src/components/Downloader.tsx b/src/components/Downloader.tsx index 43f0e80..f455b09 100644 --- a/src/components/Downloader.tsx +++ b/src/components/Downloader.tsx @@ -21,6 +21,7 @@ import UploadFileList from './UploadFileList' import DownloadButton from './DownloadButton' import StopButton from './StopButton' import ProgressBar from './ProgressBar' +import TitleText from './TitleText' const baseURL = process.env.NEXT_PUBLIC_BASE_URL ?? 'http://localhost:3000' @@ -315,9 +316,7 @@ export default function Downloader({ if (done && filesInfo) { return (
-

- You downloaded {filesInfo.length} files. -

+ You downloaded {filesInfo.length} files.
@@ -329,9 +328,9 @@ export default function Downloader({ if (downloading && filesInfo) { return (
-

+ You are about to start downloading {filesInfo.length} files. -

+
@@ -344,9 +343,9 @@ export default function Downloader({ if (open && filesInfo) { return (
-

+ You are about to start downloading {filesInfo.length} files. -

+
@@ -371,9 +370,9 @@ export default function Downloader({ >
{errorMessage ? ( -

{errorMessage}

+ {errorMessage} ) : ( -

This download requires a password.

+ This download requires a password. )} = ({ children }) => { + return

{children}

+} + +export default SubtitleText diff --git a/src/components/TitleText.tsx b/src/components/TitleText.tsx new file mode 100644 index 0000000..074a1c4 --- /dev/null +++ b/src/components/TitleText.tsx @@ -0,0 +1,11 @@ +import React from 'react' + +export default function TitleText({ + children, +}: { + children: React.ReactNode +}): JSX.Element { + return ( +

{children}

+ ) +} diff --git a/src/components/UploadFileList.tsx b/src/components/UploadFileList.tsx index 978c74c..4adb888 100644 --- a/src/components/UploadFileList.tsx +++ b/src/components/UploadFileList.tsx @@ -8,30 +8,23 @@ type UploadedFileLike = { export default function UploadFileList({ files, - onChange, + onRemove, }: { files: UploadedFileLike[] - onChange?: (updatedFiles: UploadedFileLike[]) => void + onRemove?: (index: number) => void }): JSX.Element { - function handleRemove(index: number): void { - if (onChange) { - const updatedFiles = files.filter((_, i) => i !== index) - onChange(updatedFiles) - } - } - const items = files.map((f: UploadedFileLike, i: number) => (

{f.fileName}

- {onChange && ( + {onRemove && (
)) - return
{items}
+ return ( +
+ {items} +
+ ) }