diff --git a/app.json b/app.json index e5b7fd8..87f5fa6 100644 --- a/app.json +++ b/app.json @@ -7,14 +7,16 @@ "icon": "./assets/images/noBgScaleManagerLogo.png", "scheme": "myapp", "userInterfaceStyle": "automatic", - "newArchEnabled": true, "ios": { "supportsTablet": true, "bundleIdentifier": "com.scaleManager.scaleManager", "appleTeamId": "PB5QFHF445", "buildNumber": "2", "infoPlist": { - "ITSAppUsesNonExemptEncryption": false + "ITSAppUsesNonExemptEncryption": false, + "NSAppTransportSecurity": { + "NSAllowsArbitraryLoads": true + } } }, "android": { @@ -42,7 +44,8 @@ ], "expo-font", "expo-web-browser", - "expo-status-bar" + "expo-status-bar", + "expo-sharing" ], "experiments": { "typedRoutes": true diff --git a/app/(tabs)/_layout.tsx b/app/(tabs)/_layout.tsx index ac78fb8..74bcf07 100644 --- a/app/(tabs)/_layout.tsx +++ b/app/(tabs)/_layout.tsx @@ -1,41 +1,18 @@ -import { TopTabs } from "expo-router/js-top-tabs"; +import { Tabs } from "expo-router"; import { MaterialCommunityIcons } from "@expo/vector-icons"; import { getServerConfig } from "../utils/getServer"; -import { useEffect, useState, type ComponentProps } from "react"; -import { StyleSheet, Text, type ColorValue } from "react-native"; +import { useEffect, useMemo, useState, type ComponentProps, type ReactNode } from "react"; import { isV026OrHigher } from "../utils/headscaleVersion"; import { useTheme } from "@/theme"; import { spacing } from "@/theme/spacing"; +import { TabSwipeHost } from "@/app/components/TabSwipeHost"; function tabIcon(name: ComponentProps["name"]) { - return ({ color }: { color: string }) => ( - - ); -} - -function TabLabel({ - color, - children, -}: { - color: ColorValue; - children: string; -}) { - const { theme } = useTheme(); - - return ( - - {children} - - ); + function TabBarIcon({ color }: { color: string }) { + return ; + } + TabBarIcon.displayName = `TabBarIcon(${name})`; + return TabBarIcon; } export default function TabLayout() { @@ -43,53 +20,60 @@ export default function TabLayout() { const { theme } = useTheme(); useEffect(() => { + let cancelled = false; async function checkVersion() { const serverConf = await getServerConfig(); - if (isV026OrHigher(serverConf?.version)) { + if (!cancelled && isV026OrHigher(serverConf?.version)) { setHideRoutes(true); } } checkVersion(); + return () => { + cancelled = true; + }; }, []); + const tabOrder = useMemo( + () => + hideRoutes + ? ["index", "users", "devices", "preauthkeys", "apikeys", "acl"] + : ["index", "users", "devices", "preauthkeys", "apikeys", "routes", "acl"], + [hideRoutes], + ); + + const screenLayout = useMemo( + () => + function SwipeScreenLayout({ children }: { children: ReactNode }) { + return {children}; + }, + [tabOrder], + ); + return ( - {children}, - sceneStyle: { backgroundColor: theme.colors.background }, }} > - - - - - - - + - + - - + ); } diff --git a/app/(tabs)/acl.tsx b/app/(tabs)/acl.tsx index db1cb08..9844497 100644 --- a/app/(tabs)/acl.tsx +++ b/app/(tabs)/acl.tsx @@ -159,15 +159,15 @@ export default function ACLScreen() { {policy ? ( - - + + {policy} - + ) : ( diff --git a/app/(tabs)/devices.tsx b/app/(tabs)/devices.tsx index c1abdaf..00113f9 100644 --- a/app/(tabs)/devices.tsx +++ b/app/(tabs)/devices.tsx @@ -8,12 +8,21 @@ import { TextInput, } from "react-native"; import { SafeAreaView } from "react-native-safe-area-context"; +import { useLocalSearchParams } from "expo-router"; import { MaterialIcons } from "@expo/vector-icons"; import { useDevices } from "@/app/funcs/devices"; import { RegisterDeviceModal } from "@/app/components/RegisterDeviceModal"; import { ScreenLoading } from "@/app/components/ScreenLoading"; import { useTheme } from "@/theme"; +type DeviceFilter = "all" | "online" | "offline"; + +function parseDeviceFilter(value: string | string[] | undefined): DeviceFilter { + const raw = Array.isArray(value) ? value[0] : value; + if (raw === "online" || raw === "offline" || raw === "all") return raw; + return "all"; +} + export default function DevicesScreen() { const { devices, @@ -40,11 +49,20 @@ export default function DevicesScreen() { } = useDevices(); const { theme } = useTheme(); const { colors } = theme; + const { filter: filterParam } = useLocalSearchParams<{ filter?: string }>(); const [searchQuery, setSearchQuery] = useState(""); - const [filterOnline, setFilterOnline] = useState<"all" | "online" | "offline">("all"); + const [manualFilter, setManualFilter] = useState(null); + const [seenFilterParam, setSeenFilterParam] = useState(filterParam); const [sortBy, setSortBy] = useState<"name" | "lastSeen" | "user">("name"); + // Prefer route params from Home taps; clear manual override when they change. + if (filterParam !== seenFilterParam) { + setSeenFilterParam(filterParam); + setManualFilter(null); + } + const filterOnline = manualFilter ?? parseDeviceFilter(filterParam); + const filteredAndSortedDevices = sortDevices( devices .filter(device => { @@ -123,28 +141,26 @@ export default function DevicesScreen() { {/* Filter and Sort Controls */} - {/* Status Filter */} - - - {[ - { key: "all", label: "All", count: devices.length }, - { key: "online", label: "Online", count: devices.filter(d => d.online).length }, - { key: "offline", label: "Offline", count: devices.filter(d => !d.online).length }, - ].map((filter) => ( - setFilterOnline(filter.key as any)} - activeOpacity={0.8} - className="px-3 mr-2 py-2 rounded-lg" - style={{ backgroundColor: filterOnline === filter.key ? colors.primary : colors.surfaceMuted }} - > - - {filter.label} ({filter.count}) - - - ))} - - + {/* Status Filter — plain row (not horizontal ScrollView) so tab swipes aren't stolen mid-gesture */} + + {[ + { key: "all", label: "All", count: devices.length }, + { key: "online", label: "Online", count: devices.filter(d => d.online).length }, + { key: "offline", label: "Offline", count: devices.filter(d => !d.online).length }, + ].map((filter) => ( + setManualFilter(filter.key as DeviceFilter)} + activeOpacity={0.8} + className="px-3 py-2 rounded-lg" + style={{ backgroundColor: filterOnline === filter.key ? colors.primary : colors.surfaceMuted }} + > + + {filter.label} ({filter.count}) + + + ))} + {/* Sort Button */} { diff --git a/app/(tabs)/index.tsx b/app/(tabs)/index.tsx index 81a27a9..48dd7c6 100644 --- a/app/(tabs)/index.tsx +++ b/app/(tabs)/index.tsx @@ -301,14 +301,45 @@ export default function IndexScreen() { {( [ - { label: "Users", value: usersCount, icon: "account-group" as const, tint: colors.primaryMuted }, - { label: "Online", value: onlineDevices.length, icon: "wifi" as const, tint: colors.success }, - { label: "Offline", value: offlineDevices.length, icon: "wifi-off" as const, tint: colors.error }, - { label: "Total", value: devices.length, icon: "server" as const, tint: colors.primaryMuted }, + { + label: "Users", + value: usersCount, + icon: "account-group" as const, + tint: colors.primaryMuted, + onPress: () => router.push("/(tabs)/users"), + }, + { + label: "Online", + value: onlineDevices.length, + icon: "wifi" as const, + tint: colors.success, + onPress: () => + router.push({ pathname: "/(tabs)/devices", params: { filter: "online" } }), + }, + { + label: "Offline", + value: offlineDevices.length, + icon: "wifi-off" as const, + tint: colors.error, + onPress: () => + router.push({ pathname: "/(tabs)/devices", params: { filter: "offline" } }), + }, + { + label: "Total", + value: devices.length, + icon: "server" as const, + tint: colors.primaryMuted, + onPress: () => + router.push({ pathname: "/(tabs)/devices", params: { filter: "all" } }), + }, ] ).map((stat) => ( - )} - + ))} @@ -342,8 +373,17 @@ export default function IndexScreen() { Recent Activity {topActiveDevices.map((device, index) => ( - + router.push({ + pathname: `/customScreens/${device.id}` as any, + params: { device: JSON.stringify(device) }, + }) + } + accessibilityRole="button" + accessibilityLabel={`Open device ${device.givenName || device.name}`} style={{ flexDirection: "row", alignItems: "center", @@ -372,7 +412,7 @@ export default function IndexScreen() { {new Date(device.lastSeen).toLocaleString()} - + ))} )} diff --git a/app/_layout.tsx b/app/_layout.tsx index 43c1756..25af0c2 100644 --- a/app/_layout.tsx +++ b/app/_layout.tsx @@ -1,5 +1,7 @@ +import "react-native-gesture-handler"; import { Slot } from "expo-router"; import { StatusBar } from "expo-status-bar"; +import { GestureHandlerRootView } from "react-native-gesture-handler"; import { SafeAreaProvider } from "react-native-safe-area-context"; import { ThemeProvider, useTheme } from "@/theme"; import { installFrontendLogger } from "./utils/frontendLog"; @@ -77,10 +79,12 @@ function ThemedChrome() { export default function RootLayout() { return ( - - - - - + + + + + + + ); } diff --git a/app/api/login.ts b/app/api/login.ts index ae2d18a..d5f1991 100644 --- a/app/api/login.ts +++ b/app/api/login.ts @@ -23,6 +23,7 @@ export async function testAPIKey( export async function testAPIKeyDetailed( server: string, apiKey: string, + options?: { signal?: AbortSignal }, ): Promise { const key = normalizeApiKey(apiKey); @@ -58,6 +59,7 @@ export async function testAPIKeyDetailed( // (which returns 404) doesn't get misreported as an invalid API key. const response = await fetchWithFallback(server, key, "/api/v1/apikey", { method: "GET", + signal: options?.signal, }); if (response.ok) { @@ -92,6 +94,12 @@ export async function testAPIKeyDetailed( message: `Server returned ${response.status}. Check URL and API key.`, }; } catch (error) { + if ( + options?.signal?.aborted || + (error instanceof Error && error.name === "AbortError") + ) { + return { ok: false, message: "Connection check cancelled." }; + } console.error("Fetch error:", error); return { ok: false, diff --git a/app/components/ScreenLoading.tsx b/app/components/ScreenLoading.tsx index 11975a5..af14568 100644 --- a/app/components/ScreenLoading.tsx +++ b/app/components/ScreenLoading.tsx @@ -1,4 +1,4 @@ -import { useEffect } from "react"; +import { useEffect, type ReactNode } from "react"; import { Text, View } from "react-native"; import Animated, { useAnimatedStyle, @@ -34,7 +34,13 @@ function Bar({ width }: { width: `${number}%` }) { ); } -export function ScreenLoading({ label }: { label: string }) { +export function ScreenLoading({ + label, + action, +}: { + label: string; + action?: ReactNode; +}) { const { theme } = useTheme(); const { colors, radii, spacing } = theme; @@ -59,6 +65,7 @@ export function ScreenLoading({ label }: { label: string }) { {label} + {action ? {action} : null} ); } diff --git a/app/components/TabSwipeHost.tsx b/app/components/TabSwipeHost.tsx new file mode 100644 index 0000000..16f318c --- /dev/null +++ b/app/components/TabSwipeHost.tsx @@ -0,0 +1,70 @@ +import { type ReactNode, useCallback, useMemo } from "react"; +import { View } from "react-native"; +import { Gesture, GestureDetector } from "react-native-gesture-handler"; +import { runOnJS } from "react-native-reanimated"; +import { router, usePathname } from "expo-router"; + +type TabSwipeHostProps = { + children: ReactNode; + tabOrder: string[]; +}; + +function tabFromPathname(pathname: string, tabOrder: string[]): string { + const cleaned = pathname.replace(/\/$/, "") || "/"; + const leaf = cleaned.split("/").filter(Boolean).pop() ?? "index"; + if (leaf === "tabs" || cleaned === "/" || cleaned === "/(tabs)") { + return "index"; + } + return tabOrder.includes(leaf) ? leaf : "index"; +} + +function hrefForTab(name: string) { + return name === "index" ? "/(tabs)" : `/(tabs)/${name}`; +} + +/** + * Discrete horizontal flings between bottom tabs. + * Unlike PagerView, this never leaves the UI stuck between two pages. + */ +export function TabSwipeHost({ children, tabOrder }: TabSwipeHostProps) { + const pathname = usePathname(); + const current = tabFromPathname(pathname, tabOrder); + + const goRelative = useCallback( + (delta: number) => { + const index = tabOrder.indexOf(current); + if (index < 0) return; + const next = tabOrder[index + delta]; + if (!next) return; + router.navigate(hrefForTab(next)); + }, + [current, tabOrder], + ); + + const gesture = useMemo( + () => + Gesture.Pan() + // Prefer horizontal swipes; let vertical ScrollViews keep control otherwise. + .activeOffsetX([-24, 24]) + .failOffsetY([-16, 16]) + .onEnd((event) => { + const wentNext = + event.translationX < -56 || event.velocityX < -650; + const wentPrev = + event.translationX > 56 || event.velocityX > 650; + + if (wentNext && !wentPrev) { + runOnJS(goRelative)(1); + } else if (wentPrev && !wentNext) { + runOnJS(goRelative)(-1); + } + }), + [goRelative], + ); + + return ( + + {children} + + ); +} diff --git a/app/funcs/index.ts b/app/funcs/index.ts index bab8072..4a859eb 100644 --- a/app/funcs/index.ts +++ b/app/funcs/index.ts @@ -1,4 +1,4 @@ -import { useCallback, useState } from "react"; +import { useCallback, useRef, useState } from "react"; import AsyncStorage from "@react-native-async-storage/async-storage"; import { useRouter } from "expo-router"; import Toast from "react-native-toast-message"; @@ -19,17 +19,34 @@ export function useLogin() { const [showInfo, setShowInfo] = useState(null); const [loading, setLoading] = useState(true); const httpRisk = useHttpRiskAck(server); + const savedLoginAbortRef = useRef(null); // Toggle info display - close if same item clicked, open if different const toggleInfo = (field: string) => { setShowInfo(showInfo === field ? null : field); }; + const abortSavedLoginCheck = useCallback(() => { + savedLoginAbortRef.current?.abort(); + savedLoginAbortRef.current = null; + }, []); + + const cancelSavedLoginCheck = useCallback(() => { + abortSavedLoginCheck(); + setLoading(false); + }, [abortSavedLoginCheck]); + const checkForPreviousKey = useCallback(async () => { + savedLoginAbortRef.current?.abort(); + const controller = new AbortController(); + savedLoginAbortRef.current = controller; + setLoading(true); const selectedName = await AsyncStorage.getItem("selectedServer"); const serversJson = await AsyncStorage.getItem("servers"); + if (controller.signal.aborted) return; + if (!serversJson) { setLoading(false); return; @@ -58,7 +75,12 @@ export function useLogin() { : servers; for (const candidate of ordered) { - const result = await testAPIKeyDetailed(candidate.server, candidate.apiKey); + if (controller.signal.aborted) return; + + const result = await testAPIKeyDetailed(candidate.server, candidate.apiKey, { + signal: controller.signal, + }); + if (controller.signal.aborted) return; if (!result.ok) continue; const fellBack = Boolean(selectedName) && candidate.name !== selectedName; @@ -79,7 +101,9 @@ export function useLogin() { return; } - setLoading(false); + if (!controller.signal.aborted) { + setLoading(false); + } }, [router]); const handleLogin = async () => { @@ -183,6 +207,8 @@ export function useLogin() { toggleInfo, loading, checkForPreviousKey, + cancelSavedLoginCheck, + abortSavedLoginCheck, handleLogin, httpRisk, }; diff --git a/app/index.tsx b/app/index.tsx index 1ae7bb1..ebf4a2f 100644 --- a/app/index.tsx +++ b/app/index.tsx @@ -134,13 +134,19 @@ export default function LoginScreen() { toggleInfo, loading, checkForPreviousKey, + cancelSavedLoginCheck, + abortSavedLoginCheck, handleLogin, httpRisk, } = useLogin(); useEffect(() => { checkForPreviousKey(); - }, [checkForPreviousKey]); + return () => { + // Abort only — avoid setState after unmount/navigation. + abortSavedLoginCheck(); + }; + }, [checkForPreviousKey, abortSavedLoginCheck]); return ( @@ -149,7 +155,21 @@ export default function LoginScreen() { behavior={Platform.OS === "ios" ? "padding" : undefined} > {loading ? ( - + + + Cancel + + + } + /> ) : (