import { userCache } from "@/cache"; import { client } from "@/client"; import { Avatar, AvatarImage } from "@/components/ui/avatar"; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; import { useBreakpoint } from "@/hooks/useBreakpoint"; import { useCache } from "@/hooks/useCache"; import { useLoading } from "@/hooks/useLoading"; import { DEBOUNCE } from "@/snippets"; import { UserId } from "common"; import { Role } from "common/the_api"; import { Array, Effect, HashSet, Match, Option, pipe } from "effect"; import { Loader2 } from "lucide-react"; import { useRef, useState } from "react"; export function Admin() { const [displayName, setDisplayName] = useState(""); const [role, setRole] = useState(""); const debounce = useRef(Effect.void); const breakpoint = useBreakpoint(); const columns = breakpoint ? 4 : 1; const { isLoading, error, data: userIds } = useLoading(Effect.gen(function* () { yield* debounce.current; const data = yield* client.queryUsers({ displayName: displayName !== "" ? Option.some(displayName) : Option.none(), role: role !== "" ? Option.some(role) : Option.none(), offset: 0, limit: 100, }); return data; }), [displayName, role]); return (
{ setDisplayName(e.target.value); debounce.current = DEBOUNCE; }} />
{breakpoint && ( Nazwa wyświetlana ID Role )} {isLoading ? (
Ładowanie…
) : error !== null ? (
Wystąpił błąd: {Match.value(error).pipe( Match.tag("FetchError", () => "Nie można połączyć się z serwerem"), Match.tag("Unauthenticated", () => "Zaloguj się, aby kontynuować"), Match.exhaustive, )}
) : ( userIds.map((userId) => ) )}
); } namespace UserRow { export interface Props { readonly userId: UserId; } } function UserRow(props: UserRow.Props) { const { isLoading, error, data: user } = useCache(userCache, props.userId); const breakpoint = useBreakpoint(); const columns = breakpoint ? 4 : 1; if (isLoading) { return ( Ładowanie… ); } if (error !== null) { return ( Wystąpił błąd: {Match.value(error).pipe( Match.tag("FetchError", () => "Nie można połączyć się z serwerem"), Match.tag("NotFound", () => "Użytkownik nie istnieje"), Match.tag("Unauthenticated", () => "Zaloguj się, aby kontynuować"), Match.exhaustive, )} ); } const rolesText = pipe( user.roles, HashSet.values, Array.join(", "), ); return ( {breakpoint ? (<> {Option.getOrUndefined(user.displayName)} {user.userId} {rolesText} ) : (
{Option.getOrUndefined(user.displayName)}
{user.userId}
{rolesText}
)}
); }