feat(mobile): conditionally render settings based on user authentication
- Add `anonymous` flag to navigation links - Implement dynamic filtering of navigation groups for anonymous users - Use `useMemo` to optimize navigation group rendering - Integrate user authentication state with settings visibility Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { useIsFocused } from "@react-navigation/native"
|
||||
import * as FileSystem from "expo-file-system"
|
||||
import type { FC, RefObject } from "react"
|
||||
import { Fragment, useContext, useEffect } from "react"
|
||||
import { Fragment, useContext, useEffect, useMemo } from "react"
|
||||
import type { ScrollView } from "react-native"
|
||||
import { Alert, View } from "react-native"
|
||||
import { useSafeAreaInsets } from "react-native-safe-area-context"
|
||||
@@ -26,6 +26,7 @@ import { Settings1CuteFiIcon } from "@/src/icons/settings_1_cute_fi"
|
||||
import { StarCuteFiIcon } from "@/src/icons/star_cute_fi"
|
||||
import { UserSettingCuteFiIcon } from "@/src/icons/user_setting_cute_fi"
|
||||
import { signOut } from "@/src/lib/auth"
|
||||
import { useWhoami } from "@/src/store/user/hooks"
|
||||
|
||||
import { useSettingsNavigation } from "./hooks"
|
||||
|
||||
@@ -38,6 +39,7 @@ interface GroupNavigationLink {
|
||||
) => void
|
||||
iconBackgroundColor: string
|
||||
|
||||
anonymous?: boolean
|
||||
todo?: boolean
|
||||
}
|
||||
const SettingGroupNavigationLinks: GroupNavigationLink[] = [
|
||||
@@ -57,6 +59,7 @@ const SettingGroupNavigationLinks: GroupNavigationLink[] = [
|
||||
},
|
||||
iconBackgroundColor: "#FBBF24",
|
||||
todo: true,
|
||||
anonymous: false,
|
||||
},
|
||||
{
|
||||
label: "Appearance",
|
||||
@@ -73,6 +76,7 @@ const SettingGroupNavigationLinks: GroupNavigationLink[] = [
|
||||
navigation.navigate("Data")
|
||||
},
|
||||
iconBackgroundColor: "#CBAD6D",
|
||||
anonymous: false,
|
||||
},
|
||||
{
|
||||
label: "Account",
|
||||
@@ -81,6 +85,7 @@ const SettingGroupNavigationLinks: GroupNavigationLink[] = [
|
||||
navigation.navigate("Account")
|
||||
},
|
||||
iconBackgroundColor: "#d08700",
|
||||
anonymous: false,
|
||||
},
|
||||
]
|
||||
|
||||
@@ -93,6 +98,7 @@ const DataGroupNavigationLinks: GroupNavigationLink[] = [
|
||||
},
|
||||
iconBackgroundColor: "#059669",
|
||||
todo: true,
|
||||
anonymous: false,
|
||||
},
|
||||
|
||||
{
|
||||
@@ -103,6 +109,7 @@ const DataGroupNavigationLinks: GroupNavigationLink[] = [
|
||||
},
|
||||
iconBackgroundColor: "#10B981",
|
||||
todo: true,
|
||||
anonymous: false,
|
||||
},
|
||||
{
|
||||
label: "Lists",
|
||||
@@ -112,6 +119,7 @@ const DataGroupNavigationLinks: GroupNavigationLink[] = [
|
||||
},
|
||||
iconBackgroundColor: "#34D399",
|
||||
// todo: true,
|
||||
anonymous: false,
|
||||
},
|
||||
]
|
||||
|
||||
@@ -155,6 +163,7 @@ const ActionGroupNavigationLinks: GroupNavigationLink[] = [
|
||||
])
|
||||
},
|
||||
iconBackgroundColor: "#F87181",
|
||||
anonymous: false,
|
||||
},
|
||||
]
|
||||
|
||||
@@ -187,7 +196,6 @@ const NavigationLinkGroup: FC<{
|
||||
)
|
||||
|
||||
const navigationGroups = [
|
||||
// UserGroupNavigationLinks,
|
||||
DataGroupNavigationLinks,
|
||||
SettingGroupNavigationLinks,
|
||||
PrivacyGroupNavigationLinks,
|
||||
@@ -207,12 +215,26 @@ export const SettingsList: FC<{ scrollRef: RefObject<ScrollView> }> = ({ scrollR
|
||||
|
||||
const insets = useSafeAreaInsets()
|
||||
const tabBarHeight = useBottomTabBarHeight()
|
||||
|
||||
const whoami = useWhoami()
|
||||
|
||||
const filteredNavigationGroups = useMemo(() => {
|
||||
if (whoami) return navigationGroups
|
||||
|
||||
return navigationGroups
|
||||
.map((group) => {
|
||||
const filteredGroup = group.filter((link) => link.anonymous !== false)
|
||||
if (filteredGroup.length === 0) return false
|
||||
return filteredGroup
|
||||
})
|
||||
.filter((group) => group !== false)
|
||||
}, [whoami])
|
||||
return (
|
||||
<View
|
||||
className="bg-system-grouped-background flex-1 py-4"
|
||||
style={{ paddingBottom: insets.bottom + tabBarHeight }}
|
||||
>
|
||||
{navigationGroups.map((group, index) => (
|
||||
{filteredNavigationGroups.map((group, index) => (
|
||||
<Fragment key={`nav-group-${index}`}>
|
||||
<NavigationLinkGroup
|
||||
key={`nav-group-${index}`}
|
||||
@@ -220,7 +242,7 @@ export const SettingsList: FC<{ scrollRef: RefObject<ScrollView> }> = ({ scrollR
|
||||
navigation={navigation}
|
||||
scrollRef={scrollRef}
|
||||
/>
|
||||
{index < navigationGroups.length - 1 && <View className="h-8" />}
|
||||
{index < filteredNavigationGroups.length - 1 && <View className="h-8" />}
|
||||
</Fragment>
|
||||
))}
|
||||
</View>
|
||||
|
||||
Reference in New Issue
Block a user