-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add server uploads to pg (#1055)
- Loading branch information
1 parent
a7eab3e
commit 8b41a25
Showing
6 changed files
with
284 additions
and
36 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,35 @@ | ||
import * as React from "react"; | ||
import cx from "clsx"; | ||
|
||
export function Input({ | ||
className, | ||
type, | ||
...props | ||
}: React.ComponentProps<"input">) { | ||
return ( | ||
<input | ||
type={type} | ||
className={cx( | ||
className, | ||
"border-input focus-visible:border-ring focus-visible:ring-ring/30 flex w-full rounded-lg border px-3 py-2 text-sm shadow-sm shadow-black/5 transition-shadow focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50", | ||
type === "search" && | ||
"[&::-webkit-search-cancel-button]:appearance-none [&::-webkit-search-decoration]:appearance-none [&::-webkit-search-results-button]:appearance-none [&::-webkit-search-results-decoration]:appearance-none", | ||
type === "file" && | ||
"file:border-input p-0 pr-3 italic file:me-3 file:h-full file:border-0 file:border-r file:border-solid file:bg-transparent file:px-3 file:text-sm file:font-medium file:not-italic", | ||
)} | ||
{...props} | ||
/> | ||
); | ||
} | ||
|
||
export function Label({ className, ...props }: React.ComponentProps<"label">) { | ||
return ( | ||
<label | ||
className={cx( | ||
"text-sm font-medium leading-4 peer-disabled:cursor-not-allowed peer-disabled:opacity-70", | ||
className, | ||
)} | ||
{...props} | ||
/> | ||
); | ||
} |
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,35 +1,87 @@ | ||
"use client"; | ||
|
||
import { useActionState, useEffect, useRef } from "react"; | ||
import { useRouter } from "next/navigation"; | ||
|
||
import { generateReactHelpers, generateUploadButton } from "@uploadthing/react"; | ||
|
||
import { UploadRouter } from "../app/api/uploadthing/route"; | ||
import { uploadFiles } from "../lib/actions"; | ||
import { Input, Label } from "./fieldset"; | ||
|
||
export const UTButton = generateUploadButton<UploadRouter>(); | ||
export const { useUploadThing } = generateReactHelpers<UploadRouter>(); | ||
|
||
function ServerUploader(props: { type: "file" | "url" }) { | ||
const formRef = useRef<HTMLFormElement>(null); | ||
const [state, dispatch, isUploading] = useActionState(uploadFiles, { | ||
success: false, | ||
error: "", | ||
}); | ||
|
||
useEffect(() => { | ||
if (state.success === false && state.error) { | ||
window.alert(state.error); | ||
} | ||
}, [state.success]); | ||
|
||
return ( | ||
<form ref={formRef} action={dispatch}> | ||
<div className="space-y-1"> | ||
<Label>Upload (server {props.type})</Label> | ||
<Input | ||
name="files" | ||
multiple | ||
disabled={isUploading} | ||
className="h-10 p-0 file:me-3 file:border-0 file:border-e" | ||
type={props.type === "file" ? "file" : "text"} | ||
onChange={() => { | ||
if (props.type === "file") { | ||
formRef.current?.requestSubmit(); | ||
return; | ||
} | ||
}} | ||
/> | ||
</div> | ||
|
||
<noscript> | ||
<button type="submit" disabled={isUploading}> | ||
{isUploading ? "⏳" : `Upload (server ${props.type})`} | ||
</button> | ||
</noscript> | ||
</form> | ||
); | ||
} | ||
|
||
export function Uploader() { | ||
const router = useRouter(); | ||
|
||
return ( | ||
<UTButton | ||
endpoint="anything" | ||
input={{}} | ||
onUploadError={(error) => { | ||
window.alert(error.message); | ||
}} | ||
onClientUploadComplete={() => { | ||
router.refresh(); | ||
}} | ||
content={{ | ||
allowedContent: <></>, | ||
button: ({ isUploading }) => (isUploading ? null : "Upload"), | ||
}} | ||
appearance={{ | ||
button: "!text-sm/6", | ||
allowedContent: "!h-0", | ||
}} | ||
/> | ||
<div className="flex gap-4"> | ||
<div className="space-y-1"> | ||
<Label>Upload (client)</Label> | ||
<UTButton | ||
endpoint="anything" | ||
input={{}} | ||
onUploadError={(error) => { | ||
window.alert(error.message); | ||
}} | ||
onClientUploadComplete={() => { | ||
router.refresh(); | ||
}} | ||
content={{ | ||
allowedContent: <></>, | ||
button: ({ isUploading }) => | ||
isUploading ? null : "Upload (Client)", | ||
}} | ||
appearance={{ | ||
button: "!text-sm/6", | ||
allowedContent: "!h-0", | ||
}} | ||
/> | ||
</div> | ||
<ServerUploader type="file" /> | ||
<ServerUploader type="url" /> | ||
</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,35 @@ | ||
import * as React from "react"; | ||
import cx from "clsx"; | ||
|
||
export function Input({ | ||
className, | ||
type, | ||
...props | ||
}: React.ComponentProps<"input">) { | ||
return ( | ||
<input | ||
type={type} | ||
className={cx( | ||
className, | ||
"border-input focus-visible:border-ring focus-visible:ring-ring/30 flex w-full rounded-lg border px-3 py-2 text-sm shadow-sm shadow-black/5 transition-shadow focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50", | ||
type === "search" && | ||
"[&::-webkit-search-cancel-button]:appearance-none [&::-webkit-search-decoration]:appearance-none [&::-webkit-search-results-button]:appearance-none [&::-webkit-search-results-decoration]:appearance-none", | ||
type === "file" && | ||
"file:border-input p-0 pr-3 italic file:me-3 file:h-full file:border-0 file:border-r file:border-solid file:bg-transparent file:px-3 file:text-sm file:font-medium file:not-italic", | ||
)} | ||
{...props} | ||
/> | ||
); | ||
} | ||
|
||
export function Label({ className, ...props }: React.ComponentProps<"label">) { | ||
return ( | ||
<label | ||
className={cx( | ||
"text-sm font-medium leading-4 peer-disabled:cursor-not-allowed peer-disabled:opacity-70", | ||
className, | ||
)} | ||
{...props} | ||
/> | ||
); | ||
} |
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,35 +1,87 @@ | ||
"use client"; | ||
|
||
import { useActionState, useEffect, useRef } from "react"; | ||
import { useRouter } from "next/navigation"; | ||
|
||
import { generateReactHelpers, generateUploadButton } from "@uploadthing/react"; | ||
|
||
import { UploadRouter } from "../app/api/uploadthing/route"; | ||
import { uploadFiles } from "../lib/actions"; | ||
import { Input, Label } from "./fieldset"; | ||
|
||
export const UTButton = generateUploadButton<UploadRouter>(); | ||
export const { useUploadThing } = generateReactHelpers<UploadRouter>(); | ||
|
||
function ServerUploader(props: { type: "file" | "url" }) { | ||
const formRef = useRef<HTMLFormElement>(null); | ||
const [state, dispatch, isUploading] = useActionState(uploadFiles, { | ||
success: false, | ||
error: "", | ||
}); | ||
|
||
useEffect(() => { | ||
if (state.success === false && state.error) { | ||
window.alert(state.error); | ||
} | ||
}, [state.success]); | ||
|
||
return ( | ||
<form ref={formRef} action={dispatch}> | ||
<div className="space-y-1"> | ||
<Label>Upload (server {props.type})</Label> | ||
<Input | ||
name="files" | ||
multiple | ||
disabled={isUploading} | ||
className="h-10 p-0 file:me-3 file:border-0 file:border-e" | ||
type={props.type === "file" ? "file" : "text"} | ||
onChange={() => { | ||
if (props.type === "file") { | ||
formRef.current?.requestSubmit(); | ||
return; | ||
} | ||
}} | ||
/> | ||
</div> | ||
|
||
<noscript> | ||
<button type="submit" disabled={isUploading}> | ||
{isUploading ? "⏳" : `Upload (server ${props.type})`} | ||
</button> | ||
</noscript> | ||
</form> | ||
); | ||
} | ||
|
||
export function Uploader() { | ||
const router = useRouter(); | ||
|
||
return ( | ||
<UTButton | ||
endpoint={(rr) => rr.anything} | ||
input={{}} | ||
onUploadError={(error) => { | ||
window.alert(error.message); | ||
}} | ||
onClientUploadComplete={() => { | ||
router.refresh(); | ||
}} | ||
content={{ | ||
allowedContent: <></>, | ||
button: ({ isUploading }) => (isUploading ? null : "Upload"), | ||
}} | ||
appearance={{ | ||
button: "!text-sm/6", | ||
allowedContent: "!h-0", | ||
}} | ||
/> | ||
<div className="flex gap-4"> | ||
<div className="space-y-1"> | ||
<Label>Upload (client)</Label> | ||
<UTButton | ||
endpoint={(rr) => rr.anything} | ||
input={{}} | ||
onUploadError={(error) => { | ||
window.alert(error.message); | ||
}} | ||
onClientUploadComplete={() => { | ||
router.refresh(); | ||
}} | ||
content={{ | ||
allowedContent: <></>, | ||
button: ({ isUploading }) => | ||
isUploading ? null : "Upload (Client)", | ||
}} | ||
appearance={{ | ||
button: "!text-sm/6", | ||
allowedContent: "!h-0", | ||
}} | ||
/> | ||
</div> | ||
<ServerUploader type="file" /> | ||
<ServerUploader type="url" /> | ||
</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