-
Notifications
You must be signed in to change notification settings - Fork 2
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
28f6539
commit 32fb980
Showing
7 changed files
with
235 additions
and
100 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 |
---|---|---|
@@ -1,12 +1,24 @@ | ||
import { useIsAuthed } from "../queries/auth"; | ||
import { useSetting } from "../queries/setting"; | ||
import RightNavSetting from "./RightNavSetting"; | ||
import RightNavUpload from "./RightNavUpload"; | ||
|
||
export default function RightNav() { | ||
const { data: setting } = useSetting(); | ||
const isAuthed = useIsAuthed() ?? false; | ||
|
||
return ( | ||
<div className="h-full p-4"> | ||
{setting && <RightNavSetting setting={setting} />} | ||
{isAuthed && ( | ||
<> | ||
<div className="mb-4"> | ||
{setting && <RightNavSetting setting={setting} />} | ||
</div> | ||
<div> | ||
<RightNavUpload /> | ||
</div> | ||
</> | ||
)} | ||
</div> | ||
); | ||
} |
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,74 @@ | ||
import { PlusIcon } from "@heroicons/react/24/outline"; | ||
import { useRef } from "react"; | ||
import { SubmitHandler, useForm } from "react-hook-form"; | ||
|
||
import { useFileUploadMutation } from "../queries/file"; | ||
|
||
// import { PostFileQuery } from "../queries/file"; | ||
|
||
interface UploadForm { | ||
files: FileList; | ||
alt?: string; | ||
} | ||
|
||
export default function RightNavUpload() { | ||
const modalRef = useRef<HTMLDialogElement>(null); | ||
const { register, handleSubmit } = useForm<UploadForm>(); | ||
const { | ||
mutate: upload, | ||
isLoading, | ||
error, | ||
} = useFileUploadMutation(() => { | ||
modalRef.current?.close(); | ||
}); | ||
|
||
const onSubmit: SubmitHandler<UploadForm> = (data) => { | ||
if (!data.files) { | ||
return; | ||
} | ||
const file = data.files[0]; | ||
|
||
upload({ file, mediaType: file.type, alt: data.alt }); | ||
}; | ||
|
||
return ( | ||
<> | ||
<button | ||
className="flex items-center" | ||
onClick={() => { | ||
modalRef.current?.showModal(); | ||
}} | ||
> | ||
<PlusIcon className="mr-3 inline h-10 w-10" /> | ||
<span className="text-lg">Upload</span> | ||
</button> | ||
<dialog ref={modalRef} className="modal"> | ||
<div className="form-control modal-box"> | ||
<form onSubmit={handleSubmit(onSubmit)}> | ||
<input | ||
type="file" | ||
className="file-input file-input-bordered mb-4 w-full" | ||
required | ||
{...register("files")} | ||
/> | ||
<textarea | ||
className="textarea textarea-bordered mb-4 w-full" | ||
placeholder="Alt text..." | ||
{...register("alt")} | ||
/> | ||
<input | ||
type="submit" | ||
className="btn btn-primary" | ||
value="Upload" | ||
disabled={isLoading} | ||
/> | ||
{error && <div className="mt-5 text-error">{error.message}</div>} | ||
</form> | ||
</div> | ||
<form method="dialog" className="modal-backdrop"> | ||
<button>close</button> | ||
</form> | ||
</dialog> | ||
</> | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { useContext } from "react"; | ||
import { useMutation } from "react-query"; | ||
|
||
import { MutationRet } from "."; | ||
import { AccessKeyContext } from "../contexts/auth"; | ||
import { throwError } from "../dto"; | ||
|
||
export interface PostFileQuery { | ||
file: File; | ||
mediaType: string; | ||
alt?: string; | ||
} | ||
|
||
export function useFileUploadMutation( | ||
onSuccess: () => void, | ||
): MutationRet<PostFileQuery> { | ||
const [accessKey] = useContext(AccessKeyContext); | ||
|
||
return useMutation( | ||
async (payload) => { | ||
const headers: HeadersInit = { | ||
"content-type": "application/octet-stream", | ||
}; | ||
if (accessKey.length > 0) { | ||
headers["authorization"] = `Bearer ${accessKey}`; | ||
} | ||
const resp = await fetch( | ||
`/api/file?mediaType=${payload.mediaType}${ | ||
payload.alt ? "&alt=" + payload.alt : "" | ||
}`, | ||
{ | ||
method: "POST", | ||
headers, | ||
body: payload.file, | ||
}, | ||
); | ||
if (!resp.ok) { | ||
await throwError(resp); | ||
} | ||
}, | ||
{ | ||
onSuccess: () => { | ||
onSuccess(); | ||
}, | ||
}, | ||
); | ||
} |