Skip to content

Commit

Permalink
Fix type issues
Browse files Browse the repository at this point in the history
  • Loading branch information
lukevella committed Jan 27, 2025
1 parent 784c102 commit f2dc9d6
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 149 deletions.
171 changes: 63 additions & 108 deletions apps/web/src/app/[locale]/(admin)/settings/profile/profile-page.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
"use client";
import { Alert, AlertDescription, AlertTitle } from "@rallly/ui/alert";
import { Button } from "@rallly/ui/button";
import { DialogTrigger } from "@rallly/ui/dialog";
import { Input } from "@rallly/ui/input";
import { Label } from "@rallly/ui/label";
import { InfoIcon, LogOutIcon, TrashIcon, UserXIcon } from "lucide-react";
import { LogOutIcon, TrashIcon } from "lucide-react";
import Head from "next/head";
import Link from "next/link";
import { useTranslation } from "next-i18next";

import { DeleteAccountDialog } from "@/app/[locale]/(admin)/settings/profile/delete-account-dialog";
Expand All @@ -31,112 +27,71 @@ export const ProfilePage = () => {
<Head>
<title>{t("profile")}</title>
</Head>
{user.isGuest ? (
<SettingsContent>
<SettingsSection
title={<Trans i18nKey="profile" />}
description={<Trans i18nKey="profileDescription" />}
>
<Label className="mb-2.5">
<Trans i18nKey="userId" defaults="User ID" />
</Label>
<Input
className="w-full"
value={user.id.substring(0, 10)}
readOnly
disabled
<SettingsContent>
<SettingsSection
title={<Trans i18nKey="profile" defaults="Profile" />}
description={
<Trans
i18nKey="profileDescription"
defaults="Set your public profile information"
/>
<Alert className="mt-4" icon={InfoIcon}>
<AlertTitle>
<Trans i18nKey="aboutGuest" defaults="Guest User" />
</AlertTitle>
<AlertDescription>
}
>
<ProfileSettings />
</SettingsSection>
<SettingsSection
title={
<Trans i18nKey="profileEmailAddress" defaults="Email Address" />
}
description={
<Trans
i18nKey="profileEmailAddressDescription"
defaults="Your email address is used to log in to your account"
/>
}
>
<ProfileEmailAddress />
</SettingsSection>
<hr />

<SettingsSection
title={<Trans i18nKey="logout" />}
description={
<Trans
i18nKey="logoutDescription"
defaults="Sign out of your existing session"
/>
}
>
<LogoutButton>
<LogOutIcon className="size-4" />
<Trans i18nKey="logout" defaults="Logout" />
</LogoutButton>
</SettingsSection>
{user.email ? (
<>
<hr />
<SettingsSection
title={<Trans i18nKey="dangerZone" defaults="Danger Zone" />}
description={
<Trans
i18nKey="aboutGuestDescription"
defaults="Profile settings are not available for guest users. <0>Sign in</0> to your existing account or <1>create a new account</1> to customize your profile."
components={[
<Link className="text-link" key={0} href="/login" />,
<Link className="text-link" key={1} href="/register" />,
]}
i18nKey="dangerZoneAccount"
defaults="Delete your account permanently. This action cannot be undone."
/>
</AlertDescription>
</Alert>
<LogoutButton className="mt-6" variant="destructive">
<UserXIcon className="size-4" />
<Trans i18nKey="forgetMe" />
</LogoutButton>
</SettingsSection>
</SettingsContent>
) : (
<SettingsContent>
<SettingsSection
title={<Trans i18nKey="profile" defaults="Profile" />}
description={
<Trans
i18nKey="profileDescription"
defaults="Set your public profile information"
/>
}
>
<ProfileSettings />
</SettingsSection>
<SettingsSection
title={
<Trans i18nKey="profileEmailAddress" defaults="Email Address" />
}
description={
<Trans
i18nKey="profileEmailAddressDescription"
defaults="Your email address is used to log in to your account"
/>
}
>
<ProfileEmailAddress />
</SettingsSection>
<hr />

<SettingsSection
title={<Trans i18nKey="logout" />}
description={
<Trans
i18nKey="logoutDescription"
defaults="Sign out of your existing session"
/>
}
>
<LogoutButton>
<LogOutIcon className="size-4" />
<Trans i18nKey="logout" defaults="Logout" />
</LogoutButton>
</SettingsSection>
{user.email ? (
<>
<hr />
<SettingsSection
title={<Trans i18nKey="dangerZone" defaults="Danger Zone" />}
description={
<Trans
i18nKey="dangerZoneAccount"
defaults="Delete your account permanently. This action cannot be undone."
/>
}
>
<DeleteAccountDialog email={user.email}>
<DialogTrigger asChild>
<Button className="text-destructive">
<TrashIcon className="size-4" />
<Trans
i18nKey="deleteAccount"
defaults="Delete Account"
/>
</Button>
</DialogTrigger>
</DeleteAccountDialog>
</SettingsSection>
</>
) : null}
</SettingsContent>
)}
}
>
<DeleteAccountDialog email={user.email}>
<DialogTrigger asChild>
<Button className="text-destructive">
<TrashIcon className="size-4" />
<Trans i18nKey="deleteAccount" defaults="Delete Account" />
</Button>
</DialogTrigger>
</DeleteAccountDialog>
</SettingsSection>
</>
) : null}
</SettingsContent>
</Settings>
);
};
37 changes: 19 additions & 18 deletions apps/web/src/auth/custom-prisma-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,27 @@ import type { Adapter, AdapterAccount } from "next-auth/adapters";
export function CustomPrismaAdapter(
client: ExtendedPrismaClient,
options: { migrateData: (userId: string) => Promise<void> },
): Adapter {
) {
const adapter = PrismaAdapter(client as PrismaClient);
return {
...PrismaAdapter(client as PrismaClient),
linkAccount: async (data) => {
await options.migrateData(data.userId);
return client.account.create({
...adapter,
linkAccount: async (account: AdapterAccount) => {
await options.migrateData(account.userId);
return (await client.account.create({
data: {
userId: data.userId,
type: data.type,
provider: data.provider,
providerAccountId: data.providerAccountId,
access_token: data.access_token as string,
expires_at: data.expires_at as number,
id_token: data.id_token as string,
token_type: data.token_type as string,
refresh_token: data.refresh_token as string,
scope: data.scope as string,
session_state: data.session_state as string,
userId: account.userId,
type: account.type,
provider: account.provider,
providerAccountId: account.providerAccountId,
access_token: account.access_token as string,
expires_at: account.expires_at as number,
id_token: account.id_token as string,
token_type: account.token_type as string,
refresh_token: account.refresh_token as string,
scope: account.scope as string,
session_state: account.session_state as string,
},
}) as unknown as AdapterAccount;
})) as AdapterAccount;
},
};
} satisfies Adapter;
}
10 changes: 5 additions & 5 deletions apps/web/src/components/poll/notifications-toggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const NotificationsToggle: React.FunctionComponent = () => {
queryClient.polls.getWatchers.setData(
{ pollId: poll.id },
(oldWatchers) => {
if (!oldWatchers) {
if (!oldWatchers || !user.id) {
return;
}
return [...oldWatchers, { userId: user.id }];
Expand Down Expand Up @@ -124,11 +124,11 @@ const NotificationsToggle: React.FunctionComponent = () => {
values={{
value: isWatching
? t("notificationsOn", {
defaultValue: "On",
})
defaultValue: "On",
})
: t("notificationsOff", {
defaultValue: "Off",
}),
defaultValue: "Off",
}),
}}
/>
)}
Expand Down
29 changes: 11 additions & 18 deletions apps/web/src/components/user-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { isOwner } from "@/utils/permissions";
import { useRequiredContext } from "./use-required-context";

type UserData = {
id: string;
id?: string;
name: string;
email?: string | null;
isGuest: boolean;
Expand Down Expand Up @@ -86,23 +86,16 @@ export const UserProvider = (props: { children?: React.ReactNode }) => {
return (
<UserContext.Provider
value={{
user: user
? {
id: user.id as string,
name: user.name ?? t("guest"),
email: user.email || null,
isGuest,
tier,
timeZone: user.timeZone ?? null,
image: user.image ?? null,
locale: user.locale ?? i18n.language,
}
: {
id: "",
name: t("guest"),
isGuest: true,
tier: "guest",
},
user: {
id: user?.id,
name: user?.name ?? t("guest"),
email: user?.email || null,
isGuest,
tier,
timeZone: user?.timeZone ?? null,
image: user?.image ?? null,
locale: user?.locale ?? i18n.language,
},
refresh: session.update,
logout: async () => {
await signOut();
Expand Down

0 comments on commit f2dc9d6

Please sign in to comment.