hermes-web/app/(modal)/(routes)/settings/page.tsx
2023-12-30 10:56:40 +00:00

223 lines
9.6 KiB
TypeScript

"use client";
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
import axios from "axios";
import { Button } from "@/components/ui/button";
import { DropdownMenu, DropdownMenuContent, DropdownMenuLabel, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuSeparator, DropdownMenuTrigger } from "@/components/ui/dropdown-menu";
import { Info } from "lucide-react";
import * as React from 'react';
import { Toggle } from "@/components/ui/toggle";
import { ApiKey, TwitchConnection, User } from "@prisma/client";
import { useEffect, useState } from "react";
import { useSession } from "next-auth/react";
import { Table, TableBody, TableCaption, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
import Link from "next/link";
import { TTSBadgeFilterModal } from "@/components/modals/tts-badge-filter-modal";
const SettingsPage = () => {
const { data: session, status } = useSession();
const [previousUsername, setPreviousUsername] = useState<string>()
const [userId, setUserId] = useState<string>()
useEffect(() => {
if (status !== "authenticated" || previousUsername == session.user?.name) {
return
}
setPreviousUsername(session.user?.name as string)
if (session.user?.name) {
const fetchData = async () => {
var connection: User = (await axios.get("/api/account")).data
setUserId(connection.id)
}
fetchData().catch(console.error)
}
}, [session])
// const [twitchUser, setTwitchUser] = useState<TwitchConnection | null>(null)
// useEffect(() => {
// const fetchData = async () => {
// var connection: TwitchConnection = (await axios.get("/api/settings/connections/twitch")).data
// setTwitchUser(connection)
// }
// fetchData().catch(console.error);
// }, [])
// const OnTwitchConnectionDelete = async () => {
// try {
// await axios.post("/api/settings/connections/twitch/delete")
// setTwitchUser(null)
// } catch (error) {
// console.log("ERROR", error)
// }
// }
const [apiKeyViewable, setApiKeyViewable] = useState(0)
const [apiKeyChanges, setApiKeyChanges] = useState(0)
const [apiKeys, setApiKeys] = useState<ApiKey[]>([])
useEffect(() => {
const fetchData = async () => {
try {
const keys = (await axios.get("/api/tokens")).data ?? {};
setApiKeys(keys)
console.log(keys);
} catch (error) {
console.log("ERROR", error)
}
};
fetchData().catch(console.error);
}, [apiKeyChanges]);
const onApiKeyAdd = async () => {
try {
await axios.post("/api/token", {
label: "Key label"
});
setApiKeyChanges(apiKeyChanges + 1)
} catch (error) {
console.log("ERROR", error)
}
}
const onApiKeyDelete = async (id: string) => {
try {
await axios.delete("/api/token/" + id);
setApiKeyChanges(apiKeyChanges - 1)
} catch (error) {
console.log("ERROR", error)
}
}
useEffect(() => {
const fetchData = async () => {
try {
const keys = (await axios.get("/api/tokens")).data;
setApiKeys(keys)
console.log(keys);
} catch (error) {
console.log("ERROR", error)
}
};
fetchData().catch(console.error);
}, [apiKeyViewable]);
return (
<div>
<div className="flex text-3xl justify-center">
Settings
</div>
<div className="border-solid border-white px-10 py-5 mx-5 my-10">
<div>
<div className="text-xl justify-left">Connections</div>
<div className="px-10 py-6 rounded-md bg-purple-500 max-w-sm overflow-hidden wrap-">
<div className="inline-block max-w-md">
<Avatar>
<AvatarImage src="https://cdn2.iconfinder.com/data/icons/social-aquicons/512/Twitch.png" alt="twitch" />
<AvatarFallback></AvatarFallback>
</Avatar>
</div>
{/* <div className="inline-block ml-5"> */}
<Link href={(process.env.NEXT_PUBLIC_TWITCH_OAUTH_URL as string) + userId}>Authorize</Link>
{/* <div className="inline-block text-lg">Twitch</div>
<div className={cn("hidden", twitchUser == null && "flex")}>
<ConnectTwitchModal />
</div>
<div className={cn("hidden", twitchUser != null && "flex")}>
<p>{twitchUser?.username}</p>
</div>
</div>
<div className="inline-block pl-5 ml-3 justify-right items-right">
<button onClick={OnTwitchConnectionDelete} className={cn("hidden", twitchUser != null && "flex")}>
<Avatar>
<AvatarImage src="https://upload.wikimedia.org/wikipedia/en/b/ba/Red_x.svg" alt="twitch" />
<AvatarFallback></AvatarFallback>
</Avatar>
</button>
</div> */}
</div>
</div>
<div>
<div className="text-xl justify-left mt-10">API Keys</div>
<Table className="max-w-2xl">
<TableCaption>A list of your secret API keys.</TableCaption>
<TableHeader>
<TableRow>
<TableHead>Label</TableHead>
<TableHead>Token</TableHead>
<TableHead>View</TableHead>
<TableHead>Action</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{apiKeys.map((key, index) => (
<TableRow key={key.id}>
<TableCell className="font-medium">{key.label}</TableCell>
<TableCell>{(apiKeyViewable & (1 << index)) > 0 ? key.id : "*".repeat(key.id.length)}</TableCell>
<TableCell>
<Button onClick={() => setApiKeyViewable((v) => v ^ (1 << index))}>
{(apiKeyViewable & (1 << index)) > 0 ? "HIDE" : "VIEW"}
</Button>
</TableCell>
<TableCell><Button onClick={() => onApiKeyDelete(key.id)}>DEL</Button></TableCell>
</TableRow>
))}
<TableRow key="ADD">
<TableCell className="font-medium"></TableCell>
<TableCell></TableCell>
<TableCell></TableCell>
<TableCell><Button onClick={onApiKeyAdd}>ADD</Button></TableCell>
</TableRow>
</TableBody>
</Table>
</div>
</div>
<div className="px-10 py-5 mx-5 my-10 max-w-lg inline-block">
<div className="text-xl justify-left">
<Info className="h-4 w-4 mx-1 inline" />
TTS Voice
</div>
<div className="px-10 py-6 rounded-md bg-pink-300 max-w-sm">
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="outline">Default Voice</Button>
</DropdownMenuTrigger>
<DropdownMenuContent className="w-56">
<DropdownMenuLabel>English Voices</DropdownMenuLabel>
<DropdownMenuSeparator />
<DropdownMenuRadioGroup value="voice">
<DropdownMenuRadioItem value="brian">Brian</DropdownMenuRadioItem>
<DropdownMenuRadioItem value="amy">Amy</DropdownMenuRadioItem>
<DropdownMenuRadioItem value="emma">Emma</DropdownMenuRadioItem>
</DropdownMenuRadioGroup>
</DropdownMenuContent>
</DropdownMenu>
</div>
</div>
<div className="px-10 py-5 mx-5 my-10 max-w-lg inline-block">
<div className="text-xl justify-left">TTS Message Filter</div>
<div className="grid px-10 py-6 rounded-md bg-blue-400 max-w-sm">
<TTSBadgeFilterModal />
{/* <Button aria-label="Subscription" variant="ttsmessagefilter" className="my-2">
<Info className="h-4 w-4 mx-1" />
Subscription
</Button> */}
{/* <Button aria-label="Cheers" variant="ttsmessagefilter" className="my-2">
<Info className="h-4 w-4 mx-1" />
Cheers
</Button> */}
<Button aria-label="Username" variant="ttsmessagefilter" className="my-2">
<Info className="h-4 w-4 mx-1" />
Username
</Button>
</div>
</div>
</div>
);
}
export default SettingsPage;