All files / packages/app/features/user user-screen.tsx

86.66% Statements 26/30
50% Branches 2/4
63.63% Functions 7/11
86.66% Lines 26/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146                                                          23x   98x 98x     98x 98x             98x           98x     302x                                                               54x 54x       23x   97x 97x 97x                 23x 75x 75x 75x 75x 75x   75x       1x 1x           75x 2x     75x                                          
import React from "react";
 
import {
	useMutation,
	useQueryClient,
	useSuspenseQuery,
} from "@tanstack/react-query";
import { useTranslation } from "react-i18next";
import { z } from "zod";
 
import { PageHeader, SkeletonPageHeader } from "~app/components/page-header";
import { suspendedFallback } from "~app/components/suspense-wrapper";
import { NavigationContext } from "~app/contexts/navigation-context";
import { EmailVerificationCard } from "~app/features/email-verification/email-verification-card";
import { ChangePasswordScreen } from "~app/features/user/change-password";
import { useTrpcMutationOptions } from "~app/hooks/use-trpc-mutation-options";
import { useAppForm } from "~app/utils/forms";
import { noBatchContext, useTRPC } from "~app/utils/trpc";
import { peerNameSchema } from "~app/utils/validation";
import { Button } from "~components/button";
import { Icon } from "~components/icons";
import { SaveButton } from "~components/save-button";
import { SkeletonInput } from "~components/skeleton-input";
import { View } from "~components/view";
import { options as userChangeNameOptions } from "~mutations/user/change-name";
import { options as userLogoutOptions } from "~mutations/user/logout";
 
import { UserAvatarInput } from "./user-avatar-input";
 
const UserNameInput = suspendedFallback(
	() => {
		const { t } = useTranslation("user");
		const trpc = useTRPC();
		const {
			data: { user, peer },
		} = useSuspenseQuery(trpc.user.get.queryOptions());
		const updateNameMutation = useMutation(
			trpc.user.changeName.mutationOptions(
				useTrpcMutationOptions(userChangeNameOptions, {
					context: { id: user.id },
				}),
			),
		);
		const form = useAppForm({
			defaultValues: { value: peer.name },
			validators: { onChange: z.object({ value: peerNameSchema }) },
			onSubmit: ({ value }) => updateNameMutation.mutate({ name: value.value }),
		});
 
		return (
			<form.AppField name="value">
				{(field) => (
					<field.TextField
						value={field.state.value}
						onValueChange={field.setValue}
						name={field.name}
						onBlur={field.handleBlur}
						fieldError={
							field.state.meta.isDirty ? field.state.meta.errors : undefined
						}
						label={t("form.name.label")}
						mutation={updateNameMutation}
						endContent={
							peer.name === field.state.value ? null : (
								<form.Subscribe selector={(state) => state.canSubmit}>
									{(canSubmit) => (
										<SaveButton
											title={t("form.name.saveButton")}
											onPress={() => {
												void field.form.handleSubmit();
											}}
											isLoading={updateNameMutation.isPending}
											isDisabled={!canSubmit}
										/>
									)}
								</form.Subscribe>
							)
						}
					/>
				)}
			</form.AppField>
		);
	},
	() => {
		const { t } = useTranslation("user");
		return <SkeletonInput label={t("form.name.label")} />;
	},
);
 
const UserHeader: React.FC = suspendedFallback(
	() => {
		const trpc = useTRPC();
		const { data: user } = useSuspenseQuery(trpc.user.get.queryOptions());
		return (
			<PageHeader startContent={<Icon name="user" className="size-9" />}>
				{user.peer.name}
			</PageHeader>
		);
	},
	<SkeletonPageHeader startContent={<Icon name="user" className="size-9" />} />,
);
 
export const UserScreen = () => {
	const { t } = useTranslation("user");
	const trpc = useTRPC();
	const { useNavigate } = React.use(NavigationContext);
	const navigate = useNavigate();
	const queryClient = useQueryClient();
 
	const logoutMutation = useMutation(
		trpc.user.logout.mutationOptions(
			useTrpcMutationOptions(userLogoutOptions, {
				onSuccess: () => {
					void queryClient.resetQueries();
					navigate({ to: "/", replace: true });
				},
				trpc: { context: noBatchContext },
			}),
		),
	);
	const logout = React.useCallback(
		() => logoutMutation.mutate(),
		[logoutMutation],
	);
	return (
		<>
			<EmailVerificationCard />
			<UserHeader />
			<UserAvatarInput>
				<UserNameInput />
			</UserAvatarInput>
			<ChangePasswordScreen />
			<View className="flex items-end pt-4">
				<Button
					isDisabled={logoutMutation.isPending}
					onPress={logout}
					color="warning"
					isLoading={logoutMutation.isPending}
				>
					{t("logoutButton")}
				</Button>
			</View>
		</>
	);
};