Skip to content

A modern and easy to use dashboard. 80+ integrations. 20K+ icons built in. Authentication out of the box. No YAML, drag and drop configuration.

3.9 KB
Raw Blame
import { notFound } from "next/navigation";
import { Alert, Stack, Title } from "@mantine/core";
import { IconExclamationCircle } from "@tabler/icons-react";

import { api } from "@homarr/api/server";
import { auth } from "@homarr/auth/next";
import { createLogger } from "@homarr/core/infrastructure/logs";
import { getI18n } from "@homarr/translation/server";

import { DangerZoneItem, DangerZoneRoot } from "~/components/manage/danger-zone";
import { catchTrpcNotFound } from "~/errors/trpc-catch-error";
import { canAccessUserEditPage } from "../access";
import { getUserEditDataAsync } from "../_data";
import { DeleteUserButton } from "./_components/_delete-user-button";
import { UserGeneralSettingsForm } from "./_components/_general-settings-form";
import { OAuthConnections } from "./_components/_oauth-connections";
import { UserProfileAvatarForm } from "./_components/_profile-avatar-form";
import { ResetTours } from "./_components/_reset-tours";

const logger = createLogger({ module: "userGeneralSettings" });

interface Props {
  params: Promise<{
    userId: string;
  }>;
}

export async function generateMetadata(props: Props) {
  const params = await props.params;
  const session = await auth();
  const user = await getUserEditDataAsync(params.userId).catch(() => null);

  if (!user || !canAccessUserEditPage(session, user.id)) {
    return {};
  }

  const t = await getI18n("management.page.user.edit");

  return {
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
    title: t("metaTitle", { username: user.name! }),
  };
}

export default async function EditUserPage(props: Props) {
  const params = await props.params;
  const tUserManagement = await getI18n("management.page.user");
  const tUser = await getI18n("user");
  const tGeneral = await getI18n("management.page.user.setting.general");
  const session = await auth();
  const user = await getUserEditDataAsync(params.userId).catch(catchTrpcNotFound);

  if (!canAccessUserEditPage(session, user.id)) {
    notFound();
  }

  const isSelf = session?.user.id === user.id;
  const [boards, searchEngines, connections, tourStatus] = await Promise.all([
    api.board.getManageOverview({ fullPreview: false, userId: user.id }),
    api.searchEngine.getSelectable(),
    isSelf
      ? api.apiKeys.getOwnOAuth().catch((error: unknown) => {
          logger.error(new Error("Failed to load the user's OAuth connections", { cause: error }));
          return undefined;
        })
      : undefined,
    isSelf
      ? api.user.getTourStatus().catch((error: unknown) => {
          logger.error(new Error("Failed to load the user's onboarding tour status", { cause: error }));
          return undefined;
        })
      : undefined,
  ]);
  const isCredentialsUser = user.provider === "credentials";

  return (
    <Stack>
      {!isCredentialsUser && (
        <Alert variant="light" color="yellow" icon={<IconExclamationCircle size="1rem" stroke={1.5} />}>
          {tUserManagement("fieldsDisabledExternalProvider")}
        </Alert>
      )}
      <Title>{tGeneral("title")}</Title>
      <UserGeneralSettingsForm
        user={user}
        boardsData={boards.map((board) => ({
          id: board.id,
          name: board.name,
          logoImageUrl: board.logoImageUrl,
          preview: board.preview,
        }))}
        searchEnginesData={searchEngines}
        showLanguageSelector={isSelf}
        profileAvatar={<UserProfileAvatarForm user={user} />}
      />

      {isSelf && <OAuthConnections key={user.id} initialConnections={connections} />}

      {isSelf && (
        <Stack mb="lg">
          <Title order={2}>{tGeneral("item.onboardingTours.title")}</Title>
          <ResetTours initialTourStatus={tourStatus} />
        </Stack>
      )}

      <DangerZoneRoot>
        <DangerZoneItem
          label={tUser("action.delete.label")}
          description={tUser("action.delete.description")}
          action={<DeleteUserButton user={user} />}
        />
      </DangerZoneRoot>
    </Stack>
  );
}