-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db49c09
commit 8f16f57
Showing
4 changed files
with
172 additions
and
40 deletions.
There are no files selected for viewing
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,49 @@ | ||
/* Demo.css */ | ||
|
||
.file-drop { | ||
/* relatively position the container bc the contents are absolute */ | ||
position: relative; | ||
height: 100%; | ||
width: 100%; | ||
display: flex; | ||
flex-wrap: wrap; | ||
gap: 30; | ||
} | ||
|
||
.file-drop > .file-drop-target { | ||
/* basic styles */ | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
height: 100%; | ||
width: 100%; | ||
border-radius: 2px; | ||
|
||
/* horizontally and vertically center all content */ | ||
display: flex; | ||
display: -webkit-box; | ||
display: -webkit-flex; | ||
display: -ms-flexbox; | ||
|
||
flex-direction: column; | ||
-webkit-box-orient: vertical; | ||
-webkit-box-direction: normal; | ||
-webkit-flex-direction: column; | ||
-ms-flex-direction: column; | ||
|
||
align-items: center; | ||
-webkit-box-align: center; | ||
-webkit-align-items: center; | ||
-ms-flex-align: center; | ||
|
||
justify-content: center; | ||
-webkit-box-pack: center; | ||
-webkit-justify-content: center; | ||
-ms-flex-pack: center; | ||
|
||
align-content: center; | ||
-webkit-align-content: center; | ||
-ms-flex-line-pack: center; | ||
|
||
text-align: center; | ||
} |
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,41 @@ | ||
import * as React from 'react' | ||
import { useClient } from '@based/react' | ||
|
||
export function useUploadFile() { | ||
const client = useClient() | ||
const [status, setStatus] = React.useState< | ||
'initial' | 'uploading' | 'success' | 'error' | ||
>('initial') | ||
const [progress, setProgress] = React.useState(0) | ||
|
||
function handleFileInputChange( | ||
successHandler: (uploadedFile: { id: string; src: string } | null) => void | ||
) { | ||
return async function (file?: File) { | ||
try { | ||
if (!file) { | ||
successHandler(null) | ||
return | ||
} | ||
setStatus('uploading') | ||
|
||
const { id, src } = await client.stream( | ||
'db:file-upload', | ||
{ | ||
contents: file, | ||
}, | ||
(value) => { | ||
setProgress(value * 100) | ||
} | ||
) | ||
|
||
setStatus('success') | ||
successHandler({ id, src }) | ||
} catch { | ||
setStatus('error') | ||
} | ||
} | ||
} | ||
|
||
return { status, progress, handleFileInputChange } | ||
} |