-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ implement paste to post/upload
- Implement `usePasteFiles()` hook - Implement `useCreatePost()` hook - Add `acceptTypes` to `useUploadFiles()` hook for validation - Use `usePasteFiles()` hook to paste files in post or create new post
- Loading branch information
1 parent
5fe24e8
commit 2cb990d
Showing
5 changed files
with
238 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { usePocketBase } from "@/pocketbase"; | ||
import { NewFile, useUploadFiles } from "./useUploadFiles"; | ||
|
||
interface NewPost { | ||
author: string; | ||
title: string; | ||
files: NewFile[]; | ||
public?: boolean; | ||
nsfw?: boolean; | ||
} | ||
|
||
interface UseCreatePostOptions { | ||
acceptTypes: string[]; | ||
} | ||
|
||
export const useCreatePost = ({ acceptTypes }: UseCreatePostOptions) => { | ||
const pb = usePocketBase(); | ||
|
||
const { uploadFiles, uploading } = useUploadFiles({ | ||
acceptTypes, | ||
}); | ||
|
||
const createPost = (newPost: NewPost) => | ||
uploadFiles(newPost.files).then(async (records) => { | ||
if (records.length === 0) { | ||
return; | ||
} | ||
|
||
const post = await pb.collection("posts").create({ | ||
title: newPost.title, | ||
author: newPost.author, | ||
files: records.map((f) => f.id), | ||
public: newPost.public, | ||
nsfw: newPost.nsfw, | ||
}); | ||
|
||
return post; | ||
}); | ||
|
||
return { uploading, createPost }; | ||
}; |
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,45 @@ | ||
import { notifications } from "@mantine/notifications"; | ||
import { IconAlertCircle } from "@tabler/icons-react"; | ||
import { useEffect, useRef } from "react"; | ||
|
||
interface UsePasteFilesOptions { | ||
acceptTypes: string[]; | ||
onPaste: (files: File[]) => void; | ||
} | ||
|
||
export const usePasteFiles = ({ | ||
acceptTypes, | ||
onPaste, | ||
}: UsePasteFilesOptions) => { | ||
const pasteListenerRef = useRef<(ev: ClipboardEvent) => void>(); | ||
|
||
useEffect(() => { | ||
if (typeof document !== "undefined") { | ||
pasteListenerRef.current && | ||
document.removeEventListener("paste", pasteListenerRef.current); | ||
|
||
pasteListenerRef.current = (ev) => { | ||
if (!ev.clipboardData?.files.length) { | ||
return; | ||
} | ||
const files = Array.from(ev.clipboardData.files); | ||
if (files.filter((f) => !acceptTypes.includes(f.type)).length !== 0) { | ||
notifications.show({ | ||
color: "red", | ||
title: "An error occured", | ||
message: "Please contact the developers", | ||
icon: <IconAlertCircle />, | ||
}); | ||
return; | ||
} | ||
onPaste(files); | ||
}; | ||
|
||
document.addEventListener("paste", pasteListenerRef.current); | ||
|
||
return () => | ||
pasteListenerRef.current && | ||
document.removeEventListener("paste", pasteListenerRef.current); | ||
} | ||
}, [onPaste, acceptTypes]); | ||
}; |
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