-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add a logout function #595
Changes from all commits
65ace80
82e4ccd
453e21c
23904e4
d0cacd3
eb52faf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -7,7 +7,7 @@ import type { | |||||
Account | ||||||
} from '@w3ui/core' | ||||||
|
||||||
import React, { createContext, useState, useContext, useEffect } from 'react' | ||||||
import React, { createContext, useState, useContext, useEffect, ReactNode } from 'react' | ||||||
import { createClient } from '@w3ui/core' | ||||||
|
||||||
export { ContextState, ContextActions } | ||||||
|
@@ -23,15 +23,19 @@ export const ContextDefaultValue: ContextValue = [ | |||||
accounts: [], | ||||||
spaces: [] | ||||||
}, | ||||||
{} | ||||||
{ | ||||||
logout: async () => { | ||||||
throw new Error('missing logout function') | ||||||
} | ||||||
} | ||||||
] | ||||||
|
||||||
export const Context = createContext<ContextValue>( | ||||||
ContextDefaultValue | ||||||
) | ||||||
|
||||||
export interface ProviderProps extends ServiceConfig { | ||||||
children?: JSX.Element | ||||||
children?: ReactNode | ||||||
} | ||||||
|
||||||
/** | ||||||
|
@@ -41,7 +45,7 @@ export function Provider ({ | |||||
children, | ||||||
servicePrincipal, | ||||||
connection | ||||||
}: ProviderProps): JSX.Element { | ||||||
}: ProviderProps): ReactNode { | ||||||
const [client, setClient] = useState<Client>() | ||||||
const [events, setEvents] = useState<EventTarget>() | ||||||
const [accounts, setAccounts] = useState<Account[]>([]) | ||||||
|
@@ -61,22 +65,32 @@ export function Provider ({ | |||||
} | ||||||
}, [client, events]) | ||||||
|
||||||
const getClient = async (): Promise<Client> => { | ||||||
if (client == null) { | ||||||
const { client, events } = await createClient({ servicePrincipal, connection }) | ||||||
setClient(client) | ||||||
setEvents(events) | ||||||
setAccounts(Object.values(client.accounts())) | ||||||
setSpaces(client.spaces()) | ||||||
return client | ||||||
} | ||||||
return client | ||||||
const setupClient = async (): Promise<void> => { | ||||||
const { client, events } = await createClient({ servicePrincipal, connection }) | ||||||
setClient(client) | ||||||
setEvents(events) | ||||||
setAccounts(Object.values(client.accounts())) | ||||||
setSpaces(client.spaces()) | ||||||
} | ||||||
|
||||||
const logout = async (): Promise<void> => { | ||||||
// it's possible that setupClient hasn't been run yet - run createClient here | ||||||
// to get a reliable handle on the latest store | ||||||
const { store } = await createClient({ servicePrincipal, connection }) | ||||||
await store.reset() | ||||||
// set state back to defaults | ||||||
setClient(undefined) | ||||||
setEvents(undefined) | ||||||
setAccounts([]) | ||||||
setSpaces([]) | ||||||
// set state up again | ||||||
await setupClient() | ||||||
} | ||||||
|
||||||
useEffect(() => { void getClient() }, []) // load client - once. | ||||||
useEffect(() => { void setupClient() }, []) // load client - once. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmmmm, not sure about this - on at the very least needs an update to the comment to explain why we're doing something so unusual, and I'm pretty sure this would trigger the linter you suggested above (though tbh I'm not a huge fan of that linter since it is not always correct and triggers on some valid use-cases) |
||||||
|
||||||
return ( | ||||||
<Context.Provider value={[{ client, accounts, spaces }, {}]}> | ||||||
<Context.Provider value={[{ client, accounts, spaces }, { logout }]}> | ||||||
{children} | ||||||
</Context.Provider> | ||||||
) | ||||||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if props.connection changes, wouldn't we want this to run again? so that a new client gets created and passed to
setClient
?maybe
setupClient
assignment should useReact.useCallback
with appropriate dependencies, and thisuseEffect
can depend on the variables it depends on e.g.setupClient
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also maybe we should have the lint rule for react hook deps? https://github.com/facebook/react/blob/main/packages/eslint-plugin-react-hooks/README.md
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, I think you're right! I think that's orthogonal to the purpose of this PR though - happy to file an issue and even work on it today, but want to get this PR merged and shipped to fix the regression in production