refactor: add useCallback, add methods and split effects
All checks were successful
Open PR on Staging / audit_and_check (pull_request) Successful in 34s
All checks were successful
Open PR on Staging / audit_and_check (pull_request) Successful in 34s
This commit is contained in:
parent
995c7ce293
commit
331759de5c
@ -1,16 +1,15 @@
|
|||||||
import { logout as nostrLogout } from 'nostr-login'
|
import { logout as nostrLogout } from 'nostr-login'
|
||||||
import { clear } from '../utils/localStorage'
|
import { clear } from '../utils/localStorage'
|
||||||
import { useDispatch } from 'react-redux'
|
|
||||||
import { Dispatch } from '../store/store'
|
|
||||||
import { userLogOutAction } from '../store/actions'
|
import { userLogOutAction } from '../store/actions'
|
||||||
import { LoginMethod } from '../store/auth/types'
|
import { LoginMethod } from '../store/auth/types'
|
||||||
import { useAppSelector } from './store'
|
import { useAppDispatch, useAppSelector } from './store'
|
||||||
|
import { useCallback } from 'react'
|
||||||
|
|
||||||
export const useLogout = () => {
|
export const useLogout = () => {
|
||||||
const loginMethod = useAppSelector((state) => state.auth?.loginMethod)
|
const loginMethod = useAppSelector((state) => state.auth?.loginMethod)
|
||||||
const dispatch: Dispatch = useDispatch()
|
const dispatch = useAppDispatch()
|
||||||
|
|
||||||
const logout = () => {
|
const logout = useCallback(() => {
|
||||||
// Log out of the nostr-login
|
// Log out of the nostr-login
|
||||||
if (loginMethod === LoginMethod.nostrLogin) {
|
if (loginMethod === LoginMethod.nostrLogin) {
|
||||||
nostrLogout()
|
nostrLogout()
|
||||||
@ -21,7 +20,7 @@ export const useLogout = () => {
|
|||||||
|
|
||||||
// Clear the local storage states
|
// Clear the local storage states
|
||||||
clear()
|
clear()
|
||||||
}
|
}, [dispatch, loginMethod])
|
||||||
|
|
||||||
return logout
|
return logout
|
||||||
}
|
}
|
||||||
|
@ -74,7 +74,6 @@ export const MainLayout = () => {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleNostrAuth = (_: string, opts: NostrLoginAuthOptions) => {
|
const handleNostrAuth = (_: string, opts: NostrLoginAuthOptions) => {
|
||||||
console.log(opts.method)
|
|
||||||
if (opts.type === 'logout') {
|
if (opts.type === 'logout') {
|
||||||
logout()
|
logout()
|
||||||
} else {
|
} else {
|
||||||
@ -83,12 +82,22 @@ export const MainLayout = () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
initNostrLogin({
|
;(async () => {
|
||||||
darkMode: false,
|
try {
|
||||||
noBanner: true,
|
const options = {
|
||||||
onAuth: handleNostrAuth
|
methods: ['connect', 'extension', 'local'],
|
||||||
})
|
noBanner: true,
|
||||||
|
onAuth: handleNostrAuth
|
||||||
|
}
|
||||||
|
await initNostrLogin(options)
|
||||||
|
console.log('Nostr-Login initialized successfully')
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to initialize Nostr-Login', error)
|
||||||
|
}
|
||||||
|
})()
|
||||||
|
}, [dispatch, login, logout])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
const metadataController = MetadataController.getInstance()
|
const metadataController = MetadataController.getInstance()
|
||||||
|
|
||||||
const restoredState = loadState()
|
const restoredState = loadState()
|
||||||
@ -119,8 +128,7 @@ export const MainLayout = () => {
|
|||||||
} else {
|
} else {
|
||||||
setIsLoading(false)
|
setIsLoading(false)
|
||||||
}
|
}
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
}, [dispatch, logout])
|
||||||
}, [])
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (authState && authState.loggedIn && usersAppData) {
|
if (authState && authState.loggedIn && usersAppData) {
|
||||||
|
Loading…
Reference in New Issue
Block a user