- Virtualize mobile lists and defer queue loading - Split playback state from actions to reduce rerenders - Skip invalid or unnecessary Convex queries Reviewed-on: #18
105 lines
2.7 KiB
TypeScript
105 lines
2.7 KiB
TypeScript
import {
|
|
createContext,
|
|
useCallback,
|
|
useContext,
|
|
useEffect,
|
|
useMemo,
|
|
useState,
|
|
type ReactNode,
|
|
} from "react"
|
|
import AsyncStorage from "@react-native-async-storage/async-storage"
|
|
|
|
import {
|
|
secureGetItem,
|
|
secureRemoveItem,
|
|
secureSetItem,
|
|
} from "@/lib/auth-storage"
|
|
import {
|
|
clearServerConfig,
|
|
loadServerConfig,
|
|
parseServerHostname,
|
|
saveServerConfig,
|
|
LEGACY_SERVER_CONFIG_KEY,
|
|
type ServerConfig,
|
|
} from "@/lib/server-config"
|
|
|
|
type ServerConfigContextValue = {
|
|
config: ServerConfig | null
|
|
isLoading: boolean
|
|
setServerHostname: (hostname: string) => Promise<boolean>
|
|
clearServer: () => Promise<void>
|
|
}
|
|
|
|
const ServerConfigContext = createContext<ServerConfigContextValue | null>(null)
|
|
|
|
export function ServerConfigProvider({ children }: { children: ReactNode }) {
|
|
const [config, setConfig] = useState<ServerConfig | null>(null)
|
|
const [isLoading, setIsLoading] = useState(true)
|
|
|
|
useEffect(() => {
|
|
let cancelled = false
|
|
void (async () => {
|
|
let loaded: ServerConfig | null = null
|
|
try {
|
|
loaded = await loadServerConfig(secureGetItem)
|
|
if (!loaded) {
|
|
const legacy = await AsyncStorage.getItem(LEGACY_SERVER_CONFIG_KEY)
|
|
if (legacy) {
|
|
try {
|
|
const parsed = JSON.parse(legacy) as ServerConfig
|
|
if (parsed.hostname && parsed.serverUrl && parsed.convexUrl) {
|
|
await saveServerConfig(secureSetItem, parsed)
|
|
await AsyncStorage.removeItem(LEGACY_SERVER_CONFIG_KEY)
|
|
loaded = parsed
|
|
}
|
|
} catch {
|
|
// Ignore invalid or inaccessible legacy data.
|
|
}
|
|
}
|
|
}
|
|
} catch {
|
|
// Storage failures should not leave startup blocked indefinitely.
|
|
}
|
|
if (!cancelled) {
|
|
setConfig(loaded)
|
|
setIsLoading(false)
|
|
}
|
|
})()
|
|
return () => {
|
|
cancelled = true
|
|
}
|
|
}, [])
|
|
|
|
const setServerHostname = useCallback(async (hostname: string) => {
|
|
const parsed = parseServerHostname(hostname)
|
|
if (!parsed) return false
|
|
await saveServerConfig(secureSetItem, parsed)
|
|
setConfig(parsed)
|
|
return true
|
|
}, [])
|
|
|
|
const clearServer = useCallback(async () => {
|
|
await clearServerConfig(secureRemoveItem)
|
|
setConfig(null)
|
|
}, [])
|
|
|
|
const value = useMemo(
|
|
() => ({ config, isLoading, setServerHostname, clearServer }),
|
|
[config, isLoading, setServerHostname, clearServer]
|
|
)
|
|
|
|
return (
|
|
<ServerConfigContext.Provider value={value}>
|
|
{children}
|
|
</ServerConfigContext.Provider>
|
|
)
|
|
}
|
|
|
|
export function useServerConfig() {
|
|
const context = useContext(ServerConfigContext)
|
|
if (!context) {
|
|
throw new Error("useServerConfig must be used within ServerConfigProvider")
|
|
}
|
|
return context
|
|
}
|