-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: SessionClient#logout method. React useAccount, useAccountsAvail…
…able and, useLogout hooks
- Loading branch information
1 parent
82cb52b
commit 3b801c7
Showing
17 changed files
with
268 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './useAccount'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { type Account, AccountQuery, type AccountRequest } from '@lens-protocol/graphql'; | ||
import { | ||
type ReadResult, | ||
type Suspendable, | ||
type SuspendableResult, | ||
type SuspenseResult, | ||
useSuspendableQuery, | ||
} from '../helpers'; | ||
|
||
export type UseAccountArgs = AccountRequest; | ||
|
||
/** | ||
* Fetch a single Account. | ||
* | ||
* This signature supports React Suspense: | ||
* | ||
* ```tsx | ||
* const { data } = useAccount({ managedBy: evmAddress('0x…'), suspense: true }); | ||
* ``` | ||
*/ | ||
export function useAccount(args: UseAccountArgs & Suspendable): SuspenseResult<Account | null>; | ||
|
||
/** | ||
* Fetch a single Account. | ||
* | ||
* ```tsx | ||
* const { data } = useAccount({ managedBy: evmAddress('0x…') }); | ||
* ``` | ||
*/ | ||
export function useAccount(args: UseAccountArgs): ReadResult<Account | null>; | ||
|
||
export function useAccount({ | ||
suspense = false, | ||
...request | ||
}: UseAccountArgs & { suspense?: boolean }): SuspendableResult<Account | null> { | ||
return useSuspendableQuery({ | ||
document: AccountQuery, | ||
variables: { request }, | ||
suspense, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './useAccountsAvailable'; | ||
export * from './useAuthenticatedUser'; | ||
export * from './useLogin'; | ||
export * from './useLogout'; | ||
export * from './useSessionClient'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,52 @@ | ||
import { AccountsAvailableQuery, type AccountsAvailableRequest } from '@lens-protocol/graphql'; | ||
import { useQuery } from 'urql'; | ||
import { | ||
type AccountAvailable, | ||
AccountsAvailableQuery, | ||
type AccountsAvailableRequest, | ||
type Paginated, | ||
} from '@lens-protocol/graphql'; | ||
import { | ||
type ReadResult, | ||
type Suspendable, | ||
type SuspendableResult, | ||
type SuspenseResult, | ||
useSuspendableQuery, | ||
} from '../helpers'; | ||
|
||
export type UseAccountsAvailableArgs = AccountsAvailableRequest; | ||
|
||
/** | ||
* Fetch the accounts available for a given address. | ||
* | ||
* This signature supports React Suspense: | ||
* | ||
* ```tsx | ||
* const { data } = useAccountsAvailable({ managedBy: evmAddress('0x…'), suspense: true }); | ||
* ``` | ||
*/ | ||
export function useAccountsAvailable( | ||
args: UseAccountsAvailableArgs & Suspendable, | ||
): SuspenseResult<Paginated<AccountAvailable>>; | ||
|
||
/** | ||
* Fetch the accounts available for a given address. | ||
* | ||
* ```tsx | ||
* const { data } = useAccountsAvailable({ managedBy: evmAddress('0x…') }); | ||
* ``` | ||
*/ | ||
export function useAccountsAvailable(request: AccountsAvailableRequest) { | ||
return useQuery({ | ||
query: AccountsAvailableQuery, | ||
export function useAccountsAvailable( | ||
args: UseAccountsAvailableArgs, | ||
): ReadResult<Paginated<AccountAvailable>>; | ||
|
||
export function useAccountsAvailable({ | ||
suspense = false, | ||
...request | ||
}: UseAccountsAvailableArgs & { suspense?: boolean }): SuspendableResult< | ||
Paginated<AccountAvailable> | ||
> { | ||
return useSuspendableQuery({ | ||
document: AccountsAvailableQuery, | ||
variables: { request }, | ||
suspense: suspense, | ||
}); | ||
} |
3 changes: 2 additions & 1 deletion
3
packages/react/src/authentication/useAuthenticatedUser.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type { UnauthenticatedError, UnexpectedError } from '@lens-protocol/client'; | ||
import { invariant } from '@lens-protocol/types'; | ||
|
||
import { type UseAsyncTask, useAsyncTask } from '../helpers'; | ||
import { useSessionClient } from './useSessionClient'; | ||
|
||
export type LogoutError = UnauthenticatedError | UnexpectedError; | ||
|
||
/** | ||
* Log out of Lens. | ||
* | ||
* ```tsx | ||
* const { execute, error } = useLogout(); | ||
* ``` | ||
*/ | ||
export function useLogout(): UseAsyncTask<void, void, LogoutError> { | ||
const { data: sessionClient } = useSessionClient(); | ||
|
||
return useAsyncTask(() => { | ||
invariant( | ||
sessionClient, | ||
'It appears that you are not logged in. Please log in before attempting to log out.', | ||
); | ||
|
||
return sessionClient.logout(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,3 @@ | ||
export * from './reads'; | ||
export * from './results'; | ||
export * from './tasks'; | ||
|
||
/** | ||
* A read hook result. | ||
* | ||
* It's a discriminated union of the possible results of a read operation: | ||
* - Rely on the `loading` value to determine if the `data` or `error` can be evaluated. | ||
* - If `error` is `undefined`, then `data` value will be available. | ||
*/ | ||
export type ReadResult<T, E = never> = | ||
| { | ||
data: undefined; | ||
error: undefined; | ||
loading: true; | ||
} | ||
| { | ||
data: T; | ||
error: undefined; | ||
loading: false; | ||
} | ||
| { | ||
data: undefined; | ||
error: E; | ||
loading: false; | ||
}; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const ReadResult = { | ||
Initial: <T, E = never>(): ReadResult<T, E> => ({ | ||
data: undefined, | ||
error: undefined, | ||
loading: true, | ||
}), | ||
Success: <T, E = never>(data: T): ReadResult<T, E> => ({ | ||
data, | ||
error: undefined, | ||
loading: false, | ||
}), | ||
Failure: <T, E = never>(error: E): ReadResult<T, E> => ({ | ||
data: undefined, | ||
error, | ||
loading: false, | ||
}), | ||
}; | ||
|
||
/** | ||
* A read hook result that supports React Suspense | ||
*/ | ||
export type SuspenseResult<T> = { data: T }; |
Oops, something went wrong.