-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add channels to CurrentGuildContext, switch to discord-api-type…
…s library and implement WelcomeForm
- Loading branch information
Showing
23 changed files
with
2,200 additions
and
1,029 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 |
---|---|---|
@@ -1,24 +1,33 @@ | ||
"use client" | ||
|
||
import { createContext, useContext } from "react" | ||
import { PartialGuild } from "../types" | ||
import { APIGuild, APIChannel } from "discord-api-types/v10" | ||
|
||
const Context = createContext<PartialGuild | null>(null) | ||
interface CurrentGuild { | ||
guild: APIGuild | ||
channels: APIChannel[] | ||
} | ||
|
||
const Context = createContext<CurrentGuild | null>(null) | ||
|
||
export default function CurrentGuildContext({ | ||
guild, | ||
channels, | ||
children, | ||
}: { | ||
guild: PartialGuild | ||
}: CurrentGuild & { | ||
children: React.ReactNode | ||
}) { | ||
return <Context.Provider value={guild}>{children}</Context.Provider> | ||
return ( | ||
<Context.Provider value={{ guild, channels }}>{children}</Context.Provider> | ||
) | ||
} | ||
|
||
export function useCurrentGuild(): PartialGuild { | ||
const guild = useContext(Context) | ||
if (!guild) { | ||
throw new Error("No GuildContext.Provider found when calling useGuild") | ||
export function useCurrentGuild(): CurrentGuild { | ||
const currentGuild = useContext(Context) | ||
if (!currentGuild?.guild || !currentGuild.channels) { | ||
throw new Error( | ||
"No CurrentGuildContext.Provider found when calling useGuild" | ||
) | ||
} | ||
return guild | ||
return currentGuild | ||
} |
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,8 +1,13 @@ | ||
"use client" | ||
import WelcomeForm from "@/components/WelcomeForm" | ||
import { ensureGuildExists } from "@/lib/db" | ||
|
||
import { useCurrentGuild } from "@/app/contexts/CurrentGuildContext" | ||
export const dynamic = "force-dynamic" | ||
|
||
export default async function Dashboard() { | ||
const guild = useCurrentGuild() | ||
return <h1>{JSON.stringify(guild)}</h1> | ||
export default async function Page({ | ||
params, | ||
}: { | ||
params: { guildId: string } | ||
}) { | ||
const guild = await ensureGuildExists(params.guildId) | ||
return <WelcomeForm guild={guild} /> | ||
} |
This file was deleted.
Oops, something went wrong.
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,80 @@ | ||
"use client" | ||
|
||
import { zodResolver } from "@hookform/resolvers/zod" | ||
import { useForm } from "react-hook-form" | ||
import { z } from "zod" | ||
import { Button } from "@/components/ui/button" | ||
import { | ||
Form, | ||
FormControl, | ||
FormDescription, | ||
FormField, | ||
FormItem, | ||
FormLabel, | ||
FormMessage, | ||
} from "@/components/ui/form" | ||
import { Textarea } from "./ui/textarea" | ||
import ChannelSelect from "./form/ChannelSelect" | ||
import { addData } from "@/lib/actions" | ||
import { Guild } from "@prisma/client" | ||
|
||
const FormSchema = z.object({ | ||
welcomeChannel: z.string(), | ||
welcomeMessage: z.string().min(2), | ||
}) | ||
|
||
export default function WelcomeForm({ guild }: { guild: Guild }) { | ||
const form = useForm<z.infer<typeof FormSchema>>({ | ||
resolver: zodResolver(FormSchema), | ||
defaultValues: { | ||
welcomeChannel: guild.welcomeChannel ?? undefined, | ||
welcomeMessage: guild.welcomeMessage ?? undefined, | ||
}, | ||
}) | ||
|
||
async function onSubmit(data: z.infer<typeof FormSchema>) { | ||
console.log(data) | ||
await addData(guild.id, data) | ||
} | ||
|
||
return ( | ||
<Form {...form}> | ||
<form onSubmit={form.handleSubmit(onSubmit)}> | ||
<FormField | ||
control={form.control} | ||
name="welcomeChannel" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Welcome channel</FormLabel> | ||
<ChannelSelect {...field} /> | ||
<FormDescription>Select a channel</FormDescription> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
|
||
<FormField | ||
control={form.control} | ||
name="welcomeMessage" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Welcome message</FormLabel> | ||
<FormControl> | ||
<Textarea | ||
placeholder="Message" | ||
className="resize-none" | ||
{...field} | ||
/> | ||
</FormControl> | ||
<FormDescription> | ||
The message that will be sent to the welcome channel | ||
</FormDescription> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
<Button type="submit">Save</Button> | ||
</form> | ||
</Form> | ||
) | ||
} |
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,38 @@ | ||
"use client" | ||
|
||
import { useCurrentGuild } from "@/app/contexts/CurrentGuildContext" | ||
import { FormControl } from "@/components/ui/form" | ||
import { | ||
Select, | ||
SelectContent, | ||
SelectItem, | ||
SelectTrigger, | ||
SelectValue, | ||
} from "@/components/ui/select" | ||
|
||
export default function ChannelSelect({ | ||
onChange, | ||
value, | ||
}: { | ||
onChange: (value: string) => void | ||
value: string | ||
}) { | ||
const { channels } = useCurrentGuild() | ||
const textChannels = channels.filter((channel) => channel.type === 0) | ||
return ( | ||
<Select onValueChange={onChange} defaultValue={value}> | ||
<FormControl> | ||
<SelectTrigger> | ||
<SelectValue placeholder="Select a channel" /> | ||
</SelectTrigger> | ||
</FormControl> | ||
<SelectContent> | ||
{textChannels.map(({ id, name }) => ( | ||
<SelectItem key={id} value={id}> | ||
#{name} | ||
</SelectItem> | ||
))} | ||
</SelectContent> | ||
</Select> | ||
) | ||
} |
Oops, something went wrong.