feat(mobile): setting profile page (#2985)

This commit is contained in:
Innei
2025-03-06 20:24:53 +08:00
committed by GitHub
parent bb591f6b6a
commit 1c73e46eb6
20 changed files with 689 additions and 68 deletions
@@ -111,7 +111,6 @@ function EmailManagementForm() {
const updateEmailMutation = useMutation({
mutationFn: async (values: z.infer<typeof formSchema>) => {
await new Promise((resolve) => setTimeout(resolve, 1000))
const res = await changeEmail({ newEmail: values.email })
if (res.error) {
throw new Error(res.error.message)
+6
View File
@@ -110,6 +110,12 @@ export default ({ config }: ConfigContext): ExpoConfig => ({
"expo-secure-store",
"@react-native-firebase/app",
"@react-native-firebase/crashlytics",
[
"expo-image-picker",
{
photosPermission: "Allow $(PRODUCT_NAME) to access your photos.",
},
],
],
experiments: {
typedRoutes: true,
+1
View File
@@ -54,6 +54,7 @@
"expo-file-system": "18.0.11",
"expo-haptics": "14.0.1",
"expo-image": "2.0.6",
"expo-image-picker": "~16.0.6",
"expo-linear-gradient": "14.0.2",
"expo-linking": "7.0.5",
"expo-localization": "16.0.1",
@@ -3,8 +3,14 @@ import type { NativeStackNavigationOptions } from "@react-navigation/native-stac
import { Stack } from "expo-router"
import type { FC, PropsWithChildren } from "react"
import { useContext, useEffect, useMemo, useRef, useState } from "react"
import type { ScrollView, ScrollViewProps } from "react-native"
import type {
NativeScrollEvent,
NativeSyntheticEvent,
ScrollView,
ScrollViewProps,
} from "react-native"
import { Animated as RNAnimated, useAnimatedValue, View } from "react-native"
import type { SharedValue } from "react-native-reanimated"
import type { ReanimatedScrollEvent } from "react-native-reanimated/lib/typescript/hook/commonTypes"
import { useSafeAreaFrame, useSafeAreaInsets } from "react-native-safe-area-context"
@@ -30,6 +36,9 @@ type SafeNavigationScrollViewProps = Omit<ScrollViewProps, "onScroll"> & {
// For scroll view content adjustment behavior
withTopInset?: boolean
withBottomInset?: boolean
// to sharedValue
reanimatedScrollY?: SharedValue<number>
} & PropsWithChildren
export const SafeNavigationScrollView: FC<SafeNavigationScrollViewProps> = ({
@@ -40,6 +49,7 @@ export const SafeNavigationScrollView: FC<SafeNavigationScrollViewProps> = ({
withBottomInset = false,
withTopInset = false,
reanimatedScrollY,
...props
}) => {
@@ -70,6 +80,11 @@ export const SafeNavigationScrollView: FC<SafeNavigationScrollViewProps> = ({
ref={scrollViewRef}
onScroll={RNAnimated.event([{ nativeEvent: { contentOffset: { y: scrollY } } }], {
useNativeDriver: true,
listener(event: NativeSyntheticEvent<NativeScrollEvent>) {
if (reanimatedScrollY) {
reanimatedScrollY.value = event.nativeEvent.contentOffset.y
}
},
})}
automaticallyAdjustContentInsets={false}
automaticallyAdjustsScrollIndicatorInsets={false}
@@ -12,6 +12,7 @@ interface UserAvatarProps {
className?: string
color?: string
}
export const UserAvatar = ({ image, size = 24, name, className, color }: UserAvatarProps) => {
if (!image) {
return (
@@ -24,7 +25,13 @@ export const UserAvatar = ({ image, size = 24, name, className, color }: UserAva
style={{ width: size, height: size }}
>
{name ? (
<Text className="text-secondary-label text-xs">{name.slice(0, 2)}</Text>
<Text
className="text-secondary-label p-2 text-center uppercase"
style={{ fontSize: size }}
adjustsFontSizeToFit
>
{name.slice(0, 2)}
</Text>
) : (
<User4CuteReIcon width={size} height={size} color={color} />
)}
@@ -1,3 +1,4 @@
import { cn } from "@follow/utils"
import type { PressableProps } from "react-native"
import { Pressable, TouchableOpacity } from "react-native"
import Animated, { FadeIn, FadeOut } from "react-native-reanimated"
@@ -28,7 +29,10 @@ export const UIBarButton = ({
const ButtonComponent = hasDifferentIcon ? Pressable : TouchableOpacity
return (
<ButtonComponent
className={"relative flex size-10 items-center justify-center"}
className={cn(
"relative flex size-10 items-center justify-center",
props.disabled && "opacity-50",
)}
// @ts-expect-error
onPress={onPress}
aria-label={label}
@@ -1,9 +1,13 @@
import { composeEventHandlers } from "@follow/utils"
import { cn } from "@follow/utils/src/utils"
import { forwardRef } from "react"
import { forwardRef, useEffect, useImperativeHandle, useRef, useState } from "react"
import type { StyleProp, TextInputProps, ViewStyle } from "react-native"
import { StyleSheet, Text, TextInput, View } from "react-native"
import { StyleSheet, Text, TextInput, TouchableWithoutFeedback, View } from "react-native"
import Animated, { useAnimatedStyle, useSharedValue, withSpring } from "react-native-reanimated"
import { accentColor } from "@/src/theme/colors"
import { gentleSpringPreset } from "@/src/constants/spring"
import { CloseCircleFillIcon } from "@/src/icons/close_circle_fill"
import { accentColor, useColor } from "@/src/theme/colors"
import { FormLabel } from "./Label"
@@ -84,6 +88,70 @@ export const NumberField = forwardRef<
/>
))
export const PlainTextField = forwardRef<TextInput, TextInputProps>((props, ref) => {
const secondaryLabelColor = useColor("secondaryLabel")
const [isFocused, setIsFocused] = useState(false)
const textInputRef = useRef<TextInput>(null)
useImperativeHandle(ref, () => textInputRef.current!)
const pressableButtonXSharedValue = useSharedValue(20)
const pressableButtonOpacity = useSharedValue(0)
const animatedStyle = useAnimatedStyle(() => {
return {
transform: [{ translateX: pressableButtonXSharedValue.value }],
opacity: pressableButtonOpacity.value,
position: "absolute",
right: 0,
}
})
const pressableWidthSharedValue = useSharedValue(isFocused ? 20 : 0)
useEffect(() => {
if (isFocused) {
pressableButtonXSharedValue.value = withSpring(0, gentleSpringPreset)
pressableButtonOpacity.value = withSpring(1, gentleSpringPreset)
pressableWidthSharedValue.value = withSpring(20, gentleSpringPreset)
} else {
pressableButtonXSharedValue.value = withSpring(20, gentleSpringPreset)
pressableButtonOpacity.value = withSpring(0, gentleSpringPreset)
pressableWidthSharedValue.value = withSpring(0, gentleSpringPreset)
}
}, [isFocused, pressableButtonXSharedValue, pressableButtonOpacity, pressableWidthSharedValue])
return (
<View className="flex-1 flex-row items-center">
<TextInput
{...props}
ref={textInputRef}
onFocus={composeEventHandlers(props.onFocus, () => setIsFocused(true))}
onBlur={composeEventHandlers(props.onBlur, () => setIsFocused(false))}
selectionColor={accentColor}
className={cn("text-label placeholder:text-secondary-label w-full flex-1", props.className)}
/>
<Animated.View
className="ml-2 shrink-0"
style={{
width: pressableWidthSharedValue,
}}
/>
<Animated.View style={animatedStyle}>
<TouchableWithoutFeedback
onPress={() => {
textInputRef.current?.clear()
props.onChangeText?.("")
}}
>
<CloseCircleFillIcon height={16} width={16} color={secondaryLabelColor} />
</TouchableWithoutFeedback>
</Animated.View>
</View>
)
})
const styles = StyleSheet.create({
textField: {
fontSize: 16,
@@ -2,8 +2,9 @@ import { cn } from "@follow/utils"
import type { FC, PropsWithChildren } from "react"
import * as React from "react"
import { Fragment } from "react"
import type { ViewProps } from "react-native"
import type { PressableProps, ViewProps } from "react-native"
import { Pressable, StyleSheet, Text, View } from "react-native"
import Animated, { FadeIn, FadeOut } from "react-native-reanimated"
import { MingcuteRightLine } from "@/src/icons/mingcute_right_line"
import { useColor } from "@/src/theme/colors"
@@ -12,6 +13,17 @@ type GroupedInsetListCardProps = {
showSeparator?: boolean
}
interface BaseCellClassNames {
leftClassName?: string
rightClassName?: string
}
export const GroupedOutlineDescription: FC<{
description: string
}> = ({ description }) => {
return <Text className="text-secondary-label mx-9 mt-1 text-sm">{description}</Text>
}
export const GroupedInsetListCard: FC<
PropsWithChildren & ViewProps & GroupedInsetListCardProps
> = ({ children, className, showSeparator = true, ...props }) => {
@@ -76,12 +88,15 @@ export const GroupedInsetListBaseCell: FC<PropsWithChildren & ViewProps> = ({
)
}
export const GroupedInsetListNavigationLink: FC<{
label: string
icon?: React.ReactNode
onPress: () => void
disabled?: boolean
}> = ({ label, icon, onPress, disabled }) => {
export const GroupedInsetListNavigationLink: FC<
{
label: string
icon?: React.ReactNode
onPress: () => void
disabled?: boolean
postfix?: React.ReactNode
} & BaseCellClassNames
> = ({ label, icon, onPress, disabled, leftClassName, rightClassName, postfix }) => {
const rightIconColor = useColor("tertiaryLabel")
return (
@@ -90,12 +105,13 @@ export const GroupedInsetListNavigationLink: FC<{
<GroupedInsetListBaseCell
className={cn(pressed ? "bg-system-fill" : undefined, disabled && "opacity-40")}
>
<View className={"flex-1 flex-row items-center justify-between"}>
<View className={cn("flex-1 flex-row items-center justify-between", leftClassName)}>
<View className="flex-row items-center">
{icon}
<Text className={"text-label text-[16px]"}>{label}</Text>
<Text className={"text-label"}>{label}</Text>
</View>
<View className="-mr-2 ml-4">
<View className={cn("-mr-2 ml-4 flex-row", rightClassName)}>
{postfix}
<MingcuteRightLine height={18} width={18} color={rightIconColor} />
</View>
</View>
@@ -122,21 +138,23 @@ export const GroupedInsetListNavigationLinkIcon: FC<
)
}
export const GroupedInsetListCell: FC<{
label: string
description?: string
children?: React.ReactNode
}> = ({ label, description, children }) => {
export const GroupedInsetListCell: FC<
{
label: string
description?: string
children?: React.ReactNode
} & BaseCellClassNames
> = ({ label, description, children, leftClassName, rightClassName }) => {
return (
<GroupedInsetListBaseCell className="flex-1">
<View className="flex-1">
<View className={cn("flex-1", leftClassName)}>
<Text className="text-label">{label}</Text>
{!!description && (
<Text className="text-secondary-label text-sm leading-tight">{description}</Text>
)}
</View>
<View className="mb-auto ml-4 shrink-0">{children}</View>
<View className={cn("mb-auto ml-4 shrink-0", rightClassName)}>{children}</View>
</GroupedInsetListBaseCell>
)
}
@@ -218,3 +236,32 @@ export const GroupedInformationCell: FC<{
</GroupedInsetListBaseCell>
)
}
export const GroupedPlainButtonCell: FC<
{
label: string
textClassName?: string
} & PressableProps
> = ({ label, textClassName, ...props }) => {
return (
<GroupedInsetListBaseCell className="p-0">
<Pressable {...props} className="flex-1">
{({ pressed }) => {
return (
<View className="flex-1 px-5 py-4">
{/* Pressed Overlay Effect */}
{pressed && (
<Animated.View
className="bg-system-fill absolute inset-0"
entering={FadeIn.duration(100)}
exiting={FadeOut.duration(100)}
/>
)}
<Text className={cn("text-accent text-center", textClassName)}>{label}</Text>
</View>
)
}}
</Pressable>
</GroupedInsetListBaseCell>
)
}
+3
View File
@@ -39,13 +39,16 @@ const authClient = createAuthClient({
// @keep-sorted
export const {
changeEmail,
forgetPassword,
getCookie,
getProviders,
sendVerificationEmail,
signIn,
signOut,
signUp,
twoFactor,
updateUser,
useSession,
} = authClient
+31
View File
@@ -0,0 +1,31 @@
import * as ImagePicker from "expo-image-picker"
export const pickImage = async (
options?: ImagePicker.ImagePickerOptions & {
fileName?: string
},
) => {
const result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ["images"],
allowsEditing: true,
aspect: [1, 1],
quality: 1,
...options,
})
if (result.assets?.[0]) {
const image = result.assets[0]
const imageResponse = await fetch(image.uri)
const imageBlob = await imageResponse.blob()
const formData = new FormData()
formData.append("file", imageBlob, options?.fileName ?? image.fileName ?? "untitled.jpg")
formData.append("type", image.mimeType ?? "image/png")
return {
formData,
blob: imageBlob,
image,
}
}
return null
}
@@ -130,7 +130,11 @@ export const UserHeaderBanner = ({
<View className="mt-2 items-center">
{user?.name ? (
<Text
className={cn("text-2xl font-bold", gradientLight ? "text-black" : "text-white/95")}
numberOfLines={1}
className={cn(
"px-8 text-2xl font-bold",
gradientLight ? "text-black" : "text-white/95",
)}
>
{user.name}
</Text>
@@ -1,14 +1,14 @@
import * as FileSystem from "expo-file-system"
import { Text, TouchableOpacity, View } from "react-native"
import { Alert, View } from "react-native"
import {
NavigationBlurEffectHeader,
SafeNavigationScrollView,
} from "@/src/components/layouts/views/SafeNavigationScrollView"
import {
GroupedInsetListBaseCell,
GroupedInsetListCard,
GroupedInsetListSectionHeader,
GroupedPlainButtonCell,
} from "@/src/components/ui/grouped/GroupedList"
import { getDbPath } from "@/src/database"
import { signOut } from "@/src/lib/auth"
@@ -21,18 +21,25 @@ export const AccountScreen = () => {
<View className="mt-6">
<GroupedInsetListSectionHeader label="Danger Zone" />
<GroupedInsetListCard>
<GroupedInsetListBaseCell>
<TouchableOpacity
onPress={async () => {
await signOut()
const dbPath = getDbPath()
await FileSystem.deleteAsync(dbPath)
await expo.reloadAppAsync("User sign out")
}}
>
<Text className="text-red">Delete account</Text>
</TouchableOpacity>
</GroupedInsetListBaseCell>
<GroupedPlainButtonCell
label="Delete account"
textClassName="text-red text-left"
onPress={async () => {
Alert.alert("Delete account", "Are you sure you want to delete your account?", [
{ text: "Cancel", style: "cancel" },
{
text: "Delete",
style: "destructive",
onPress: async () => {
await signOut()
const dbPath = getDbPath()
await FileSystem.deleteAsync(dbPath)
await expo.reloadAppAsync("User sign out")
},
},
])
}}
/>
</GroupedInsetListCard>
</View>
</SafeNavigationScrollView>
@@ -0,0 +1,207 @@
import { withOpacity } from "@follow/utils/src/color"
import { useMutation } from "@tanstack/react-query"
import { router } from "expo-router"
import type { FC } from "react"
import { useState } from "react"
import {
ActivityIndicator,
Text,
TouchableOpacity,
TouchableWithoutFeedback,
View,
} from "react-native"
import { KeyboardController } from "react-native-keyboard-controller"
import { RotateableLoading } from "@/src/components/common/RotateableLoading"
import {
NavigationBlurEffectHeader,
SafeNavigationScrollView,
} from "@/src/components/layouts/views/SafeNavigationScrollView"
import { UserAvatar } from "@/src/components/ui/avatar/UserAvatar"
import { UIBarButton } from "@/src/components/ui/button/UIBarButton"
import { PlainTextField } from "@/src/components/ui/form/TextField"
import {
GroupedInsetListCard,
GroupedInsetListCell,
GroupedInsetListNavigationLink,
GroupedOutlineDescription,
} from "@/src/components/ui/grouped/GroupedList"
import { CheckCircleCuteReIcon } from "@/src/icons/check_circle_cute_re"
import { CheckLineIcon } from "@/src/icons/check_line"
import { CloseCircleFillIcon } from "@/src/icons/close_circle_fill"
import { apiClient } from "@/src/lib/api-fetch"
import { pickImage } from "@/src/lib/native/picker"
import { toast } from "@/src/lib/toast"
import { useWhoami } from "@/src/store/user/hooks"
import type { MeModel } from "@/src/store/user/store"
import { userSyncService } from "@/src/store/user/store"
import type { UserProfileEditable } from "@/src/store/user/types"
import { accentColor, useColor } from "@/src/theme/colors"
export const EditProfileScreen = () => {
const whoami = useWhoami()
if (!whoami) {
return (
<View className="flex-1 items-center justify-center">
<ActivityIndicator />
</View>
)
}
return (
<SafeNavigationScrollView className="bg-system-grouped-background">
<AvatarSection whoami={whoami} />
<ProfileForm whoami={whoami} />
</SafeNavigationScrollView>
)
}
const AvatarSection: FC<{
whoami: MeModel
}> = ({ whoami }) => {
return (
<View className="mt-6 items-center justify-center">
<UserAvatar
image={whoami?.image}
name={whoami?.name}
size={80}
className={!whoami?.name || !whoami.image ? "bg-system-background" : ""}
/>
<TouchableOpacity
className="mt-2"
hitSlop={10}
onPress={async () => {
const result = await pickImage({
fileName: "avatar.jpg",
})
if (!result) return
const { formData } = result
const res = await apiClient.upload.avatar.$post({
body: {
file: formData,
},
})
void res
}}
>
<Text className="text-accent text-lg">Set Avatar</Text>
</TouchableOpacity>
</View>
)
}
const ProfileForm: FC<{
whoami: MeModel
}> = ({ whoami }) => {
const [dirtyFields, setDirtyFields] = useState<Partial<UserProfileEditable>>({})
const { mutateAsync: updateProfile, isPending } = useMutation({
mutationFn: async () => {
await userSyncService.updateProfile(dirtyFields)
},
onSuccess: () => {
toast.success("Profile updated")
},
onError: (error) => {
toast.error(error.message)
},
})
const label = useColor("label")
return (
<View className="mt-4">
<NavigationBlurEffectHeader
headerRight={() => (
<UIBarButton
label="Save"
disabled={isPending || Object.keys(dirtyFields).length === 0}
normalIcon={
isPending ? (
<RotateableLoading size={20} color={withOpacity(label, 0.5)} />
) : (
<CheckLineIcon height={20} width={20} />
)
}
onPress={() => {
updateProfile()
}}
/>
)}
title="Edit Profile"
/>
<TouchableWithoutFeedback
onPress={() => {
KeyboardController.dismiss()
}}
>
<View className="w-full">
<GroupedInsetListCard>
<GroupedInsetListCell
label="Display Name"
leftClassName="flex-none"
rightClassName="flex-1"
>
<View className="flex-1">
<PlainTextField
className="text-secondary-label w-full flex-1 text-right"
value={dirtyFields.name ?? whoami?.name ?? ""}
hitSlop={10}
selectionColor={accentColor}
onChangeText={(text) => {
setDirtyFields({ ...dirtyFields, name: text })
}}
/>
</View>
</GroupedInsetListCell>
</GroupedInsetListCard>
<GroupedOutlineDescription description="This is the name that will be displayed to other users." />
{/* User name */}
<GroupedInsetListCard className="mt-4">
<GroupedInsetListCell label="Handle" leftClassName="flex-none" rightClassName="flex-1">
<View className="flex-1">
<PlainTextField
className="text-secondary-label w-full flex-1 text-right"
value={dirtyFields.handle ?? whoami?.handle ?? ""}
hitSlop={10}
selectionColor={accentColor}
onChangeText={(text) => {
setDirtyFields({ ...dirtyFields, handle: text })
}}
/>
</View>
</GroupedInsetListCell>
</GroupedInsetListCard>
<GroupedOutlineDescription description="Your handle is used to identify you on Follow." />
{/* Email */}
<GroupedInsetListCard className="mt-4">
<GroupedInsetListNavigationLink
label="Email"
onPress={() => {
router.push("/edit-email")
}}
leftClassName="flex-none"
rightClassName="flex-1"
postfix={
<View className="ml-auto flex-row gap-2">
<Text className="text-secondary-label">{whoami.email}</Text>
{whoami.emailVerified ? (
<CheckCircleCuteReIcon height={18} width={18} color={"#00C75F"} />
) : (
<CloseCircleFillIcon height={18} width={18} color={"#FF3B30"} />
)}
</View>
}
/>
</GroupedInsetListCard>
</View>
</TouchableWithoutFeedback>
</View>
)
}
@@ -6,6 +6,7 @@ import { AchievementScreen } from "./Achievement"
import { ActionsScreen } from "./Actions"
import { AppearanceScreen } from "./Appearance"
import { DataScreen } from "./Data"
import { EditProfileScreen } from "./EditProfile"
import { FeedsScreen } from "./Feeds"
import { GeneralScreen } from "./General"
import { ListsScreen } from "./Lists"
@@ -13,21 +14,26 @@ import { ManageListScreen } from "./ManageList"
import { NotificationsScreen } from "./Notifications"
import { PrivacyScreen } from "./Privacy"
export const SettingRoutes = (Stack: TypedNavigator<any, any>) => {
return [
// <Stack.Screen key="Profile" name="Profile" component={ProfileScreen} />,
<Stack.Screen key="Achievement" name="Achievement" component={AchievementScreen} />,
<Stack.Screen key="General" name="General" component={GeneralScreen} />,
<Stack.Screen key="Notifications" name="Notifications" component={NotificationsScreen} />,
<Stack.Screen key="Appearance" name="Appearance" component={AppearanceScreen} />,
<Stack.Screen key="Data" name="Data" component={DataScreen} />,
<Stack.Screen key="Account" name="Account" component={AccountScreen} />,
<Stack.Screen key="Actions" name="Actions" component={ActionsScreen} />,
<Stack.Screen key="Lists" name="Lists" component={ListsScreen} />,
<Stack.Screen key="Feeds" name="Feeds" component={FeedsScreen} />,
<Stack.Screen key="Privacy" name="Privacy" component={PrivacyScreen} />,
<Stack.Screen key="About" name="About" component={AboutScreen} />,
// @ts-expect-error
<Stack.Screen key="ManageList" name="ManageList" component={ManageListScreen} />,
]
const SettingFlatRoutes = (Stack: TypedNavigator<any, any>) => {
return (
<Stack.Group>
<Stack.Screen key="Achievement" name="Achievement" component={AchievementScreen} />
<Stack.Screen key="General" name="General" component={GeneralScreen} />
<Stack.Screen key="Notifications" name="Notifications" component={NotificationsScreen} />
<Stack.Screen key="Appearance" name="Appearance" component={AppearanceScreen} />
<Stack.Screen key="Data" name="Data" component={DataScreen} />
<Stack.Screen key="Account" name="Account" component={AccountScreen} />
<Stack.Screen key="Actions" name="Actions" component={ActionsScreen} />
<Stack.Screen key="Lists" name="Lists" component={ListsScreen} />
<Stack.Screen key="Feeds" name="Feeds" component={FeedsScreen} />
<Stack.Screen key="Privacy" name="Privacy" component={PrivacyScreen} />
<Stack.Screen key="About" name="About" component={AboutScreen} />
{/* @ts-expect-error */}
<Stack.Screen key="ManageList" name="ManageList" component={ManageListScreen} />
<Stack.Screen key="EditProfile" name="EditProfile" component={EditProfileScreen} />
</Stack.Group>
)
}
export const SettingRoutes = SettingFlatRoutes
@@ -12,4 +12,5 @@ export type SettingsStackParamList = {
Privacy: undefined
About: undefined
ManageList: { id: string }
EditProfile: undefined
}
@@ -0,0 +1,90 @@
import { useMutation } from "@tanstack/react-query"
import { useRouter } from "expo-router"
import { useMemo, useState } from "react"
import { View } from "react-native"
import { ModalHeaderSubmitButton } from "@/src/components/common/ModalSharedComponents"
import { ModalHeader } from "@/src/components/layouts/header/ModalHeader"
import { SafeModalScrollView } from "@/src/components/layouts/views/SafeModalScrollView"
import { PlainTextField } from "@/src/components/ui/form/TextField"
import {
GroupedInsetListCard,
GroupedInsetListCell,
GroupedInsetListSectionHeader,
GroupedOutlineDescription,
GroupedPlainButtonCell,
} from "@/src/components/ui/grouped/GroupedList"
import { toast } from "@/src/lib/toast"
import { useWhoami } from "@/src/store/user/hooks"
import { userSyncService } from "@/src/store/user/store"
const EditEmailScreen = () => {
const whoami = useWhoami()
const [email, setEmail] = useState(whoami?.email ?? "")
const [isDirty, setIsDirty] = useState(false)
const isValidate = whoami?.emailVerified
const newEmailIsValid = useMemo(() => {
return email.match(/^[^\s@]+@[^\s@][^\s.@]*\.[^\s@]+$/)
}, [email])
const navigation = useRouter()
const { mutate: updateEmail, isPending } = useMutation({
mutationFn: async () => {
await userSyncService.updateEmail(email)
},
onSuccess: () => {
toast.info("Please check your email inbox to verify your new email")
navigation.dismiss()
},
})
return (
<SafeModalScrollView className="bg-system-grouped-background">
<ModalHeader
headerTitle="Edit Email"
headerRight={
<ModalHeaderSubmitButton
isLoading={isPending}
isValid={!!(email && newEmailIsValid && isDirty)}
onPress={() => {
updateEmail()
}}
/>
}
/>
<View className="mt-8 w-full">
<GroupedInsetListSectionHeader label="Email" />
<GroupedInsetListCard>
<GroupedInsetListCell label="Email" rightClassName="flex-1" leftClassName="flex-none">
<PlainTextField
autoCapitalize="none"
value={email}
onChangeText={(text) => {
setEmail(text)
setIsDirty(true)
}}
placeholder="Enter your email"
className="text-secondary-label w-full flex-1 text-left"
/>
</GroupedInsetListCell>
</GroupedInsetListCard>
<GroupedOutlineDescription
description={`Your email is ${isValidate ? "verified" : "not verified"}. \n\nIf you want to change your email, you should verify your new email.`}
/>
{/* Buttons */}
{!isValidate && (
<GroupedInsetListCard className="mt-6">
<GroupedPlainButtonCell label="Send Verification Email" />
</GroupedInsetListCard>
)}
</View>
</SafeModalScrollView>
)
}
export default EditEmailScreen
@@ -2,7 +2,7 @@ import { useIsFocused } from "@react-navigation/native"
import { createNativeStackNavigator } from "@react-navigation/native-stack"
import { createContext, useCallback, useContext, useEffect, useState } from "react"
import type { NativeScrollEvent, NativeSyntheticEvent, ScrollView } from "react-native"
import { findNodeHandle, Text, UIManager } from "react-native"
import { findNodeHandle, Text, TouchableOpacity, UIManager, View } from "react-native"
import type { SharedValue } from "react-native-reanimated"
import Animated, { useAnimatedStyle, useSharedValue, withTiming } from "react-native-reanimated"
import { useSafeAreaFrame, useSafeAreaInsets } from "react-native-safe-area-context"
@@ -17,6 +17,7 @@ import {
useRegisterNavigationScrollView,
} from "@/src/components/layouts/tabbar/hooks"
import { getDefaultHeaderHeight } from "@/src/components/layouts/utils"
import { useSettingsNavigation } from "@/src/modules/settings/hooks"
import { SettingRoutes } from "@/src/modules/settings/routes"
import { SettingsList } from "@/src/modules/settings/SettingsList"
import { UserHeaderBanner } from "@/src/modules/settings/UserHeaderBanner"
@@ -131,15 +132,33 @@ const SettingHeader = ({ scrollY }: { scrollY: SharedValue<number> }) => {
}
})
const whoami = useWhoami()
return (
<Animated.View
pointerEvents="none"
className="border-b-hairline border-opaque-separator pt-safe absolute inset-x-0 top-0 flex-row items-center px-4 pb-2"
style={styles}
>
<BlurEffect />
<Text className="text-label flex-1 text-center text-[17px] font-semibold">Settings</Text>
</Animated.View>
<View className="pt-safe absolute inset-x-0 top-0" style={{ height: headerHeight }}>
<Animated.View
pointerEvents="none"
className="border-b-hairline border-opaque-separator absolute inset-x-0 top-0 flex-row items-center px-4 pb-2"
style={styles}
>
<BlurEffect />
<Text className="text-label flex-1 text-center text-[17px] font-semibold">Settings</Text>
</Animated.View>
{!!whoami?.id && <EditProfileButton />}
</View>
)
}
const EditProfileButton = () => {
const navigation = useSettingsNavigation()
return (
<TouchableOpacity
activeOpacity={0.7}
className="absolute bottom-2 right-4 overflow-hidden rounded-full px-3 py-1.5"
onPress={() => navigation.navigate("EditProfile")}
>
<BlurEffect />
<Text className="text-label text-sm font-medium">Edit</Text>
</TouchableOpacity>
)
}
+80 -2
View File
@@ -2,14 +2,21 @@ import analytics from "@react-native-firebase/analytics"
import type { UserSchema } from "@/src/database/schemas/types"
import { apiClient } from "@/src/lib/api-fetch"
import { changeEmail, sendVerificationEmail, updateUser } from "@/src/lib/auth"
import { toast } from "@/src/lib/toast"
import { UserService } from "@/src/services/user"
import { createImmerSetter, createTransaction, createZustandStore } from "../internal/helper"
import type { UserProfileEditable } from "./types"
export type UserModel = UserSchema
export type MeModel = UserModel & {
emailVerified?: boolean
}
type UserStore = {
users: Record<string, UserModel>
whoami: UserModel | null
whoami: MeModel | null
}
export const useUserStore = createZustandStore<UserStore>("user")(() => ({
@@ -17,7 +24,7 @@ export const useUserStore = createZustandStore<UserStore>("user")(() => ({
whoami: null,
}))
// const set = useUserStore.setState
const get = useUserStore.getState
const immerSet = createImmerSetter(useUserStore)
class UserSyncService {
@@ -48,6 +55,77 @@ class UserSyncService {
return null
}
}
async updateProfile(data: Partial<UserProfileEditable>) {
const me = get().whoami
if (!me) return
const tx = createTransaction(me)
tx.store(() => {
immerSet((state) => {
if (!state.whoami) return
state.whoami = { ...state.whoami, ...data }
})
})
tx.request(async () => {
await updateUser({
...data,
})
})
tx.persist(async () => {
const { whoami } = get()
if (!whoami) return
const nextUser = {
...whoami,
...data,
}
userActions.upsertMany([nextUser])
})
tx.rollback(() => {
immerSet((state) => {
if (!state.whoami) return
state.whoami = me
})
})
await tx.run()
}
async sendVerificationEmail() {
const me = get().whoami
if (!me?.email) return
await sendVerificationEmail({ email: me.email! })
toast.success("Verification email sent")
}
async updateEmail(email: string) {
const oldEmail = get().whoami?.email
if (!oldEmail) return
const tx = createTransaction(oldEmail)
tx.store(() => {
immerSet((state) => {
if (!state.whoami) return
state.whoami = { ...state.whoami, email }
})
})
tx.request(async () => {
const { whoami } = get()
if (!whoami) return
await changeEmail({ newEmail: email })
})
tx.rollback(() => {
immerSet((state) => {
if (!state.whoami) return
state.whoami.email = oldEmail
})
})
tx.persist(async () => {
const { whoami } = get()
if (!whoami) return
userActions.upsertMany([{ ...whoami, email }])
})
await tx.run()
}
}
class UserActions {
+6
View File
@@ -0,0 +1,6 @@
export interface UserProfileEditable {
email: string
name: string
handle: string
image: string
}
+22
View File
@@ -859,6 +859,9 @@ importers:
expo-image:
specifier: 2.0.6
version: 2.0.6(expo@52.0.37(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@expo/metro-runtime@4.0.1(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5)))(bufferutil@4.0.9)(encoding@0.1.13)(graphql@16.8.1)(react-native-webview@13.12.5(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5))(react@18.3.1))(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5))(react@18.3.1)(utf-8-validate@6.0.5))(react-native-web@0.19.13(encoding@0.1.13)(react-dom@18.3.1(react@18.3.1))(react@18.3.1))(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5))(react@18.3.1)
expo-image-picker:
specifier: ~16.0.6
version: 16.0.6(expo@52.0.37(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@expo/metro-runtime@4.0.1(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5)))(bufferutil@4.0.9)(encoding@0.1.13)(graphql@16.8.1)(react-native-webview@13.12.5(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5))(react@18.3.1))(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5))(react@18.3.1)(utf-8-validate@6.0.5))
expo-linear-gradient:
specifier: 14.0.2
version: 14.0.2(expo@52.0.37(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@expo/metro-runtime@4.0.1(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5)))(bufferutil@4.0.9)(encoding@0.1.13)(graphql@16.8.1)(react-native-webview@13.12.5(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5))(react@18.3.1))(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5))(react@18.3.1)(utf-8-validate@6.0.5))(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5))(react@18.3.1)
@@ -8549,6 +8552,16 @@ packages:
peerDependencies:
expo: '*'
expo-image-loader@5.0.0:
resolution: {integrity: sha512-Eg+5FHtyzv3Jjw9dHwu2pWy4xjf8fu3V0Asyy42kO+t/FbvW/vjUixpTjPtgKQLQh+2/9Nk4JjFDV6FwCnF2ZA==}
peerDependencies:
expo: '*'
expo-image-picker@16.0.6:
resolution: {integrity: sha512-HN4xZirFjsFDIsWFb12AZh19fRzuvZjj2ll17cGr19VNRP06S/VPQU3Tdccn5vwUzQhOBlLu704CnNm278boiQ==}
peerDependencies:
expo: '*'
expo-image@2.0.6:
resolution: {integrity: sha512-NHpIZmGnrPbyDadil6eK+sUgyFMQfapEVb7YaGgxSFWBUQ1rSpjqdIQrCD24IZTO9uSH8V+hMh2ROxrAjAixzQ==}
peerDependencies:
@@ -24730,6 +24743,15 @@ snapshots:
dependencies:
expo: 52.0.37(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@expo/metro-runtime@4.0.1(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5)))(bufferutil@4.0.9)(encoding@0.1.13)(graphql@16.8.1)(react-native-webview@13.12.5(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5))(react@18.3.1))(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5))(react@18.3.1)(utf-8-validate@6.0.5)
expo-image-loader@5.0.0(expo@52.0.37(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@expo/metro-runtime@4.0.1(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5)))(bufferutil@4.0.9)(encoding@0.1.13)(graphql@16.8.1)(react-native-webview@13.12.5(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5))(react@18.3.1))(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5))(react@18.3.1)(utf-8-validate@6.0.5)):
dependencies:
expo: 52.0.37(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@expo/metro-runtime@4.0.1(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5)))(bufferutil@4.0.9)(encoding@0.1.13)(graphql@16.8.1)(react-native-webview@13.12.5(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5))(react@18.3.1))(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5))(react@18.3.1)(utf-8-validate@6.0.5)
expo-image-picker@16.0.6(expo@52.0.37(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@expo/metro-runtime@4.0.1(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5)))(bufferutil@4.0.9)(encoding@0.1.13)(graphql@16.8.1)(react-native-webview@13.12.5(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5))(react@18.3.1))(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5))(react@18.3.1)(utf-8-validate@6.0.5)):
dependencies:
expo: 52.0.37(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@expo/metro-runtime@4.0.1(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5)))(bufferutil@4.0.9)(encoding@0.1.13)(graphql@16.8.1)(react-native-webview@13.12.5(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5))(react@18.3.1))(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5))(react@18.3.1)(utf-8-validate@6.0.5)
expo-image-loader: 5.0.0(expo@52.0.37(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@expo/metro-runtime@4.0.1(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5)))(bufferutil@4.0.9)(encoding@0.1.13)(graphql@16.8.1)(react-native-webview@13.12.5(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5))(react@18.3.1))(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5))(react@18.3.1)(utf-8-validate@6.0.5))
expo-image@2.0.6(expo@52.0.37(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@expo/metro-runtime@4.0.1(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5)))(bufferutil@4.0.9)(encoding@0.1.13)(graphql@16.8.1)(react-native-webview@13.12.5(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5))(react@18.3.1))(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5))(react@18.3.1)(utf-8-validate@6.0.5))(react-native-web@0.19.13(encoding@0.1.13)(react-dom@18.3.1(react@18.3.1))(react@18.3.1))(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5))(react@18.3.1):
dependencies:
expo: 52.0.37(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@expo/metro-runtime@4.0.1(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5)))(bufferutil@4.0.9)(encoding@0.1.13)(graphql@16.8.1)(react-native-webview@13.12.5(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5))(react@18.3.1))(react-native@0.77.0(@babel/core@7.26.9)(@babel/preset-env@7.26.9(@babel/core@7.26.9))(@types/react@18.3.12)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@6.0.5))(react@18.3.1)(utf-8-validate@6.0.5)