nostros/frontend/Contexts/UserContext.tsx

181 lines
5.0 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-28 20:22:04 +00:00
import { getUser } 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-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
reloadUser: () => void
logout: () => void
2023-01-28 20:22:04 +00:00
name?: string
setName: (value: string) => void
picture?: string
setPicture: (value: string) => void
about?: string
setAbout: (value: string) => void
lnurl?: string
setLnurl: (value: string) => void
nip05?: string
setNip05: (value: string) => void
validNip05?: boolean
2023-01-14 20:35:35 +00:00
}
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: () => {},
reloadUser: () => {},
logout: () => {},
2023-01-28 20:22:04 +00:00
setName: () => {},
setPicture: () => {},
setAbout: () => {},
setLnurl: () => {},
setNip05: () => {},
2023-01-14 20:35:35 +00:00
}
export const UserContextProvider = ({ children }: UserContextProviderProps): JSX.Element => {
const { database, loadingDb, init } = useContext(AppContext)
2023-02-08 16:09:13 +00:00
const { relayPool, relayPoolReady } = 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>()
2023-01-28 20:22:04 +00:00
const [name, setName] = useState<string>()
const [picture, setPicture] = useState<string>()
const [about, setAbout] = useState<string>()
const [lnurl, setLnurl] = useState<string>()
const [nip05, setNip05] = useState<string>()
const [validNip05, setValidNip05] = useState<boolean>()
2023-01-14 20:35:35 +00:00
const reloadUser: () => void = () => {
if (database && publicKey) {
getUser(publicKey, database).then((result) => {
2023-01-17 13:21:32 +00:00
if (result) {
2023-01-28 20:22:04 +00:00
setName(result.name)
setPicture(result.picture)
setAbout(result.about)
setLnurl(result.lnurl)
setNip05(result.nip05)
setValidNip05(result.valid_nip05)
2023-01-17 13:21:32 +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)
2023-01-28 20:22:04 +00:00
setName(undefined)
setPicture(undefined)
setAbout(undefined)
setLnurl(undefined)
setNip05(undefined)
setValidNip05(undefined)
2023-01-14 20:35:35 +00:00
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' })
})
})
})
}
}
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(() => {
2023-02-08 16:09:13 +00:00
if (userState === 'ready' && publicKey && relayPoolReady) {
2023-01-18 21:54:28 +00:00
navigate('Feed')
}
2023-02-08 16:09:13 +00:00
}, [userState, publicKey, relayPoolReady])
2023-01-18 21:54:28 +00:00
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,
publicKey,
setPublicKey,
privateKey,
setPrivateKey,
reloadUser,
2023-01-16 12:09:18 +00:00
logout,
2023-01-28 20:22:04 +00:00
name,
setName,
picture,
setPicture,
about,
setAbout,
lnurl,
setLnurl,
nip05,
setNip05,
validNip05,
2023-01-14 20:35:35 +00:00
}}
>
{children}
</UserContext.Provider>
)
}
export const UserContext = React.createContext(initialUserContext)