nostros/frontend/Contexts/UserContext.tsx

186 lines
5.2 KiB
TypeScript
Raw Normal View History

2023-01-14 20:35:35 +00:00
import React, { useContext, useEffect, useState } from 'react'
import SInfo from 'react-native-sensitive-info'
import { RelayPoolContext } from './RelayPoolContext'
import { AppContext } from './AppContext'
2023-01-16 12:09:18 +00:00
import {
getContactsCount,
getFollowersCount,
getUser,
User,
} from '../Functions/DatabaseFunctions/Users'
2023-01-23 14:32:51 +00:00
import { getPublicKey } from 'nostr-tools'
2023-01-14 20:35:35 +00:00
import { dropTables } from '../Functions/DatabaseFunctions'
2023-01-16 20:06:12 +00:00
import { navigate } from '../lib/Navigation'
2023-01-21 20:32:17 +00:00
import { nsecEncode } from 'nostr-tools/nip19'
import { getNpub } from '../lib/nostr/Nip19'
2023-01-26 06:28:07 +00:00
import Clipboard from '@react-native-clipboard/clipboard'
import { validNip21 } from '../Functions/NativeFunctions'
2023-01-14 20:35:35 +00:00
export interface UserContextProps {
2023-01-18 21:54:28 +00:00
userState: 'loading' | 'access' | 'ready'
setUserState: (userState: 'loading' | 'access' | 'ready') => void
2023-01-14 20:35:35 +00:00
nPub?: string
nSec?: string
publicKey?: string
setPublicKey: (privateKey: string | undefined) => void
privateKey?: string
setPrivateKey: (privateKey: string | undefined) => void
setUser: (user: User) => void
2023-01-16 12:09:18 +00:00
user?: User
contactsCount: number
followersCount: number
setContantsCount: (count: number) => void
setFollowersCount: (count: number) => void
2023-01-14 20:35:35 +00:00
reloadUser: () => void
logout: () => void
}
export interface UserContextProviderProps {
children: React.ReactNode
}
export const initialUserContext: UserContextProps = {
2023-01-18 21:54:28 +00:00
userState: 'loading',
setUserState: () => {},
2023-01-14 20:35:35 +00:00
setPublicKey: () => {},
setPrivateKey: () => {},
setUser: () => {},
reloadUser: () => {},
logout: () => {},
2023-01-16 12:09:18 +00:00
setContantsCount: () => {},
setFollowersCount: () => {},
2023-01-14 20:35:35 +00:00
contactsCount: 0,
2023-01-16 12:09:18 +00:00
followersCount: 0,
2023-01-14 20:35:35 +00:00
}
export const UserContextProvider = ({ children }: UserContextProviderProps): JSX.Element => {
const { database, loadingDb, init } = useContext(AppContext)
2023-01-25 15:38:18 +00:00
const { relayPool, lastEventId } = useContext(RelayPoolContext)
2023-01-18 21:54:28 +00:00
const [userState, setUserState] = useState<'loading' | 'access' | 'ready'>('loading')
2023-01-14 20:35:35 +00:00
const [publicKey, setPublicKey] = useState<string>()
const [nPub, setNpub] = useState<string>()
const [nSec, setNsec] = useState<string>()
const [privateKey, setPrivateKey] = useState<string>()
const [user, setUser] = React.useState<User>()
2023-01-26 06:28:07 +00:00
const [clipboardLoads, setClipboardLoads] = React.useState<string[]>([])
2023-01-14 20:35:35 +00:00
const [contactsCount, setContantsCount] = React.useState<number>(0)
const [followersCount, setFollowersCount] = React.useState<number>(0)
const reloadUser: () => void = () => {
if (database && publicKey) {
getUser(publicKey, database).then((result) => {
2023-01-17 13:21:32 +00:00
if (result) {
setUser(result)
} else {
setUser({
id: publicKey,
})
}
2023-01-26 06:28:07 +00:00
checkClipboard()
2023-01-14 20:35:35 +00:00
})
getContactsCount(database).then(setContantsCount)
getFollowersCount(database).then(setFollowersCount)
}
}
2023-01-26 06:28:07 +00:00
const checkClipboard: () => void = () => {
Clipboard.getString().then((clipboardContent) => {
if (validNip21(clipboardContent) && !clipboardLoads.includes(clipboardContent)) {
setClipboardLoads((prev) => [...prev, clipboardContent])
2023-01-26 07:13:04 +00:00
console.log(clipboardContent)
2023-01-26 06:28:07 +00:00
}
})
}
2023-01-14 20:35:35 +00:00
const logout: () => void = () => {
if (database) {
relayPool?.unsubscribeAll()
setPrivateKey(undefined)
setPublicKey(undefined)
setNpub(undefined)
setNsec(undefined)
setUser(undefined)
dropTables(database).then(() => {
SInfo.deleteItem('privateKey', {}).then(() => {
SInfo.deleteItem('publicKey', {}).then(() => {
init()
2023-01-18 21:54:28 +00:00
setUserState('access')
2023-01-14 20:35:35 +00:00
navigate('Home', { screen: 'ProfileConnect' })
})
})
})
}
}
2023-01-25 15:38:18 +00:00
useEffect(() => {
if (!user) reloadUser()
}, [lastEventId])
2023-01-14 20:35:35 +00:00
useEffect(() => {
if (privateKey && privateKey !== '') {
SInfo.setItem('privateKey', privateKey, {})
setNsec(nsecEncode(privateKey))
2023-01-23 14:32:51 +00:00
setPublicKey(getPublicKey(privateKey))
2023-01-14 20:35:35 +00:00
}
}, [privateKey])
useEffect(() => {
if (publicKey && publicKey !== '') {
SInfo.setItem('publicKey', publicKey, {})
2023-01-21 20:32:17 +00:00
setNpub(getNpub(publicKey))
2023-01-14 20:35:35 +00:00
reloadUser()
}
}, [publicKey])
2023-01-18 21:54:28 +00:00
useEffect(() => {
if (userState === 'ready' && publicKey) {
navigate('Feed')
}
}, [userState, publicKey])
2023-01-14 20:35:35 +00:00
useEffect(() => {
2023-01-16 12:09:18 +00:00
if (!loadingDb) {
2023-01-14 20:35:35 +00:00
SInfo.getItem('privateKey', {}).then((privateResult) => {
if (privateResult && privateResult !== '') {
setPrivateKey(privateResult)
2023-01-18 21:54:28 +00:00
}
})
SInfo.getItem('publicKey', {}).then((publicResult) => {
if (publicResult && publicResult !== '') {
setPublicKey(publicResult)
setUserState('ready')
2023-01-14 20:35:35 +00:00
} else {
2023-01-18 21:54:28 +00:00
setUserState('access')
2023-01-14 20:35:35 +00:00
}
})
}
}, [loadingDb])
return (
<UserContext.Provider
value={{
2023-01-18 21:54:28 +00:00
userState,
setUserState,
2023-01-14 20:35:35 +00:00
nSec,
nPub,
setUser,
publicKey,
setPublicKey,
privateKey,
setPrivateKey,
user,
contactsCount,
followersCount,
reloadUser,
2023-01-16 12:09:18 +00:00
logout,
setContantsCount,
setFollowersCount,
2023-01-14 20:35:35 +00:00
}}
>
{children}
</UserContext.Provider>
)
}
export const UserContext = React.createContext(initialUserContext)