Repository: LAION-AI/Open-Assistant
File: website/src/pages/admin/manage_user/[id].tsx (TypeScript)

Code before:
import { getValidDisplayName } from "src/lib/display_name_validation";
import { userlessApiClient } from "src/lib/oasst_client_factory";
import prisma from "src/lib/prismadb";
import { getFrontendUserIdForDiscordUser } from "src/lib/users";
import { LeaderboardEntity, LeaderboardTimeFrame } from "src/types/Leaderboard";
import { User } from "src/types/Users";
import uswSWRImmutable from "swr/immutable";
import useSWRMutation from "swr/mutation";

interface UserForm {
  user_id: string;
  id: string;
  auth_method: string;
  display_name: string;
  role: Role;
  notes: string;
  show_on_leaderboard: boolean;
}

const ManageUser = ({ user }: InferGetServerSidePropsType<typeof getServerSideProps>) => {
  const toast = useToast();

  const { data: stats } = uswSWRImmutable<Partial<{ [time in LeaderboardTimeFrame]: LeaderboardEntity }>>(
    "/api/user_stats?uid=" + user.id,
    get,
    {
      fallbackData: {},
    }
  );

  // Trigger to let us update the user's role.  Triggers a toast when complete.
  const { trigger } = useSWRMutation("/api/admin/update_user", post, {
    onSuccess: () => {
      toast({
        title: "Updated user",
        status: "success",
        duration: 1000,
        isClosable: true,
      });
    },
    onError: () => {
      toast({
        title: "User Role update failed",
        status: "error",
        duration: 1000,
        isClosable: true,
      });
    },
  });

  const { register, handleSubmit } = useForm<UserForm>({

Review comment (bug):
fix a bug we already have now:

if you go to the prod deployment / admin / open a user that is logged in with  discord, the stats are missing at the bottom.

Code after:
import { getValidDisplayName } from "src/lib/display_name_validation";
import { userlessApiClient } from "src/lib/oasst_client_factory";
import prisma from "src/lib/prismadb";
import { getFrontendUserIdForUser } from "src/lib/users";
import { LeaderboardEntity, LeaderboardTimeFrame } from "src/types/Leaderboard";
import { AuthMethod } from "src/types/Providers";
import { User } from "src/types/Users";
import uswSWRImmutable from "swr/immutable";
import useSWRMutation from "swr/mutation";

interface UserForm {
  user_id: string;
  id: string;
  auth_method: AuthMethod;
  display_name: string;
  role: Role;
  notes: string;
  show_on_leaderboard: boolean;
}

const ManageUser = ({ user }: InferGetServerSidePropsType<typeof getServerSideProps>) => {
  const toast = useToast();

  const { data: stats } = uswSWRImmutable<Partial<{ [time in LeaderboardTimeFrame]: LeaderboardEntity }>>(
    "/api/user_stats?" +
      new URLSearchParams({ id: user.id, auth_method: user.auth_method, display_name: user.display_name }),
    get,
    {
      fallbackData: {},
    }
  );

  // Trigger to let us update the user's role.  Triggers a toast when complete.
  const { trigger } = useSWRMutation("/api/admin/update_user", post, {
    onSuccess: () => {
      toast({
        title: "Updated user",
        status: "success",
        duration: 1000,
        isClosable: true,
      });
    },
    onError: () => {
      toast({
        title: "User Role update failed",
        status: "error",
        duration: 1000,
        isClosable: true,
      });
    },
  });