Changed settings layout + separated settings into different pages. Cleaned some code. Working on tts pages (no db/rest calls yet).

This commit is contained in:
Tom
2023-12-31 10:41:55 +00:00
parent 8eb9b9096f
commit a3352af981
20 changed files with 1083 additions and 287 deletions

View File

@ -0,0 +1,250 @@
"use client";
import axios from "axios";
import * as React from 'react';
import { Calendar, Check, ChevronsUpDown, MoreHorizontal, Plus, Tags, Trash, User } from "lucide-react"
import { ApiKey, TwitchConnection } from "@prisma/client";
import { useEffect, useState } from "react";
import { useSession } from "next-auth/react";
import Link from "next/link";
import { cn } from "@/lib/utils";
import { Skeleton } from "@/components/ui/skeleton";
import { Button } from "@/components/ui/button"
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from "@/components/ui/command"
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"
import { Table, TableBody, TableCaption, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
import { Input } from "@/components/ui/input";
import { Textarea } from "@/components/ui/textarea";
import { useRouter } from "next/navigation";
import { useForm } from "react-hook-form";
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form";
import * as z from "zod";
import { zodResolver } from "@hookform/resolvers/zod";
import { DropdownMenu, DropdownMenuGroup, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuShortcut, DropdownMenuSub, DropdownMenuSubContent, DropdownMenuSubTrigger } from "@/components/ui/dropdown-menu";
import { DropdownMenuContent, DropdownMenuTrigger } from "@radix-ui/react-dropdown-menu";
import { Label } from "@/components/ui/label";
const TTSFiltersPage = () => {
const { data: session, status } = useSession();
const [loading, setLoading] = useState<boolean>(true)
const [moreOpen, setMoreOpen] = useState<number>(0)
const [tag, setTag] = useState("blacklisted")
const [open, setOpen] = useState<boolean>(false)
const [userTags, setUserTag] = useState<{ username: string, tag: string }[]>([{ username: "test", tag:"blacklisted" }, { username: "hello world", tag:"moderator" }])
const router = useRouter();
const labels = [
"blacklisted",
"priority"
]
// Username blacklist
const usernameFilteredFormSchema = z.object({
username: z.string().trim().min(4).max(25).regex(new RegExp("[a-zA-Z0-9][a-zA-Z0-9_]{3, 24}"), "Must be a valid twitch username.")
});
const usernameFilteredForm = useForm({
resolver: zodResolver(usernameFilteredFormSchema),
defaultValues: {
username: ""
}
});
useEffect(() => {
const fetchData = async () => {
try {
// const userFiltersData = (await axios.get("/api/settings/tts/filters/users")).data ?? {};
// setApiKeys(userFiltersData)
// console.log(userFiltersData);
} catch (error) {
console.log("ERROR", error)
}
};
fetchData().catch(console.error);
}, []);
const onApiKeyAdd = async () => {
///let usernames = blacklistedUsers.split("\n");
//console.log(usernames)
// try {
// await axios.post("/api/settings/tts/filters/users", {
// usernames: []
// });
// setUserFilters(userFilters.concat(usernames))
// } catch (error) {
// console.log("ERROR", error)
// }
}
const onApiKeyDelete = async (username: string) => {
try {
await axios.delete("/api/settings/tts/filters/users/" + username);
//setUserFilters(userFilters.filter((u) => u != username))
} catch (error) {
console.log("ERROR", error)
}
}
const isLoading = usernameFilteredForm.formState.isSubmitting;
const addTwitchUser = (values: z.infer<typeof usernameFilteredFormSchema>) => {
let response = null;
console.log("TEST")
console.log(values)
// try {
// response = await axios.post("/api/settings/tts/filter/badges", values);
// } catch (error) {
// console.log("[CONNECTIONS/TWITCH/POST]", error);
// return;
// }
userTags.push({ username: values.username, tag: tag })
setUserTag(userTags)
usernameFilteredForm.reset();
router.refresh();
//window.location.reload();
}
return (
<div>
<div className="text-2xl text-center pt-[50px]">TTS Filters</div>
<div className="px-10 py-10 w-full h-full flex-grow inset-y-1/2">
<div>
{userTags.map((user, index) => (
<div className="flex w-full flex-col items-start justify-between rounded-md border px-4 py-3 sm:flex-row sm:items-center">
<p className="text-sm font-medium leading-none">
<span className="mr-2 rounded-lg bg-primary px-2 py-1 text-xs text-primary-foreground">
{user.tag}
</span>
<span className="text-muted-foreground">{user.username}</span>
</p>
<DropdownMenu open={(moreOpen & (1 << index)) > 0} onOpenChange={() => setMoreOpen((v) => v ^ (1 << index))}>
<DropdownMenuTrigger asChild>
<Button variant="ghost" size="sm">
<MoreHorizontal />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end" className="w-[200px]">
<DropdownMenuLabel>Actions</DropdownMenuLabel>
<DropdownMenuGroup>
<DropdownMenuSub>
<DropdownMenuSubTrigger>
<Tags className="mr-2 h-4 w-4" />
Apply label
</DropdownMenuSubTrigger>
<DropdownMenuSubContent className="p-0">
<Command>
<CommandInput
placeholder="Filter label..."
autoFocus={true}
/>
<CommandList>
<CommandEmpty>No label found.</CommandEmpty>
<CommandGroup>
{labels.map((label) => (
<CommandItem
key={label}
value={label}
onSelect={(value) => {
userTags[index].tag = value
setUserTag(userTags)
setMoreOpen(0)
}}
>
{label}
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</DropdownMenuSubContent>
</DropdownMenuSub>
<DropdownMenuSeparator />
<DropdownMenuItem onClick={() => {
userTags.splice(index, 1)
setUserTag(userTags)
}} className="text-red-600">
<Trash className="mr-2 h-4 w-4" />
Delete
</DropdownMenuItem>
</DropdownMenuGroup>
</DropdownMenuContent>
</DropdownMenu>
</div>
))}
<Form {...usernameFilteredForm}>
<form onSubmit={usernameFilteredForm.handleSubmit(addTwitchUser)}>
<div className="flex w-full flex-col items-start justify-between rounded-md border px-4 py-3 sm:flex-row sm:items-center">
<Label className=" mr-2 rounded-lg bg-primary px-2 py-1 text-xs text-primary-foreground">
{tag}
</Label>
<FormField
control={usernameFilteredForm.control}
name="username"
render={({ field }) => (
<FormItem className="flex-grow">
<FormControl>
<Input id="username" placeholder="Enter a twitch username" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<Button variant="ghost" size="sm" type="submit">
<Plus />
</Button>
<DropdownMenu open={open} onOpenChange={setOpen}>
<DropdownMenuTrigger asChild>
<Button variant="ghost" size="sm" {...usernameFilteredForm}>
<MoreHorizontal />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end" className="w-[200px]">
<DropdownMenuLabel>Actions</DropdownMenuLabel>
<DropdownMenuGroup>
<DropdownMenuSub>
<DropdownMenuSubTrigger>
<Tags className="mr-2 h-4 w-4" />
Apply label
</DropdownMenuSubTrigger>
<DropdownMenuSubContent className="p-0">
<Command>
<CommandInput
placeholder="Filter label..."
autoFocus={true}
/>
<CommandList>
<CommandEmpty>No label found.</CommandEmpty>
<CommandGroup>
{labels.map((label) => (
<CommandItem
value={label}
onSelect={(value) => {
setTag(value)
setOpen(false)
}}
>
{label}
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</DropdownMenuSubContent>
</DropdownMenuSub>
</DropdownMenuGroup>
</DropdownMenuContent>
</DropdownMenu>
</div>
</form>
</Form>
</div>
</div>
</div>
);
}
export default TTSFiltersPage;

View File

@ -0,0 +1,110 @@
"use client";
import axios from "axios";
import * as React from 'react';
import { Check, ChevronsUpDown } from "lucide-react"
import { ApiKey, TwitchConnection, User } from "@prisma/client";
import { useEffect, useState } from "react";
import { useSession } from "next-auth/react";
import Link from "next/link";
import { cn } from "@/lib/utils";
import { Skeleton } from "@/components/ui/skeleton";
import { Button } from "@/components/ui/button"
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem } from "@/components/ui/command"
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"
import { Label } from "@/components/ui/label";
const TTSFiltersPage = () => {
const { data: session, status } = useSession();
const [loading, setLoading] = useState<boolean>(true)
const [open, setOpen] = useState(false)
const [value, setValue] = useState<string>("")
const voices = [
{
value: "brian",
label: "Brian",
gender: "Male",
language: "en"
},
{
value: "sveltekit",
label: "SvelteKit",
gender: "Male"
},
{
value: "nuxt.js",
label: "Nuxt.js",
gender: "Male",
language: "en"
},
{
value: "remix",
label: "Remix",
gender: "Male",
language: "en"
},
{
value: "astro",
label: "Astro",
gender: "Male",
language: "en"
},
]
return (
<div>
<div className="text-2xl text-center pt-[50px]">TTS Voices</div>
<div className="px-10 py-10 w-full h-full flex-grow inset-y-1/2">
<div>
<div id="defaultvoice" className="inline-block text-lg">Default Voice</div>
<Label htmlFor="defaultvoice" className=" pl-[10px] inline-block">Voice used without any voice modifiers</Label>
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button
variant="outline"
role="combobox"
aria-expanded={open}
className="w-[200px] justify-between"
>
{value
? voices.find((voice) => voice.value === value)?.label
: "Select voice..."}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</PopoverTrigger>
<PopoverContent className="w-[200px] p-0">
<Command>
<CommandInput placeholder="Search voice..." />
<CommandEmpty>No framework found.</CommandEmpty>
<CommandGroup>
{voices.map((voice) => (
<CommandItem
key={voice.value}
value={voice.value}
onSelect={(currentValue) => {
setValue(currentValue === value ? "" : currentValue)
setOpen(false)
}}
>
<Check
className={cn(
"mr-2 h-4 w-4",
value === voice.value ? "opacity-100" : "opacity-0"
)}
/>
{voice.label}
</CommandItem>
))}
</CommandGroup>
</Command>
</PopoverContent>
</Popover>
</div>
</div>
</div>
);
}
export default TTSFiltersPage;