Skip to content

Commit

Permalink
Migrate to Lens v3
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoginth committed Dec 13, 2024
1 parent 8c67449 commit 1bf1add
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 38 deletions.
54 changes: 16 additions & 38 deletions apps/web/src/components/Shared/SwitchAccounts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import getAvatar from "@hey/helpers/getAvatar";
import {
type Account,
useAccountsAvailableQuery,
useAuthenticateMutation,
useChallengeMutation
useSwitchAccountMutation
} from "@hey/indexer";
import { ErrorMessage, H4, Image, Spinner } from "@hey/ui";
import cn from "@hey/ui/cn";
Expand All @@ -17,15 +16,15 @@ import { useState } from "react";
import toast from "react-hot-toast";
import { useAccountStore } from "src/store/persisted/useAccountStore";
import { signIn, signOut } from "src/store/persisted/useAuthStore";
import { useAccount, useSignMessage } from "wagmi";
import { useAccount } from "wagmi";
import WalletSelector from "./Auth/WalletSelector";
import Loader from "./Loader";

const SwitchAccounts: FC = () => {
const { reload } = useRouter();
const { currentAccount } = useAccountStore();
const [isLoading, setIsLoading] = useState(false);
const [loggingInProfileId, setLoggingInProfileId] = useState<null | string>(
const [loggingInAccountId, setLoggingInAccountId] = useState<null | string>(
null
);
const { address } = useAccount();
Expand All @@ -35,52 +34,31 @@ const SwitchAccounts: FC = () => {
errorToast(error);
};

const { signMessageAsync } = useSignMessage({ mutation: { onError } });

const { data, error, loading } = useAccountsAvailableQuery({
variables: {
lastLoggedInAccountRequest: { address },
accountsAvailableRequest: { managedBy: address }
}
});
const [loadChallenge] = useChallengeMutation();
const [authenticate] = useAuthenticateMutation();
const [switchAccount] = useSwitchAccountMutation();

if (loading) {
return <Loader className="my-5" message="Loading Profiles" />;
return <Loader className="my-5" message="Loading Accounts" />;
}

const accountsAvailable = data?.accountsAvailable.items || [];

const handleSwitchProfile = async (account: string) => {
const handleSwitchAccount = async (account: string) => {
try {
setLoggingInProfileId(account);
setLoggingInAccountId(account);
setIsLoading(true);
// Get challenge
const challenge = await loadChallenge({
variables: {
request: { accountOwner: { account, owner: address } }
}
});

if (!challenge?.data?.challenge?.text) {
return toast.error(Errors.SomethingWentWrong);
}

// Get signature
const signature = await signMessageAsync({
message: challenge?.data?.challenge?.text
});

// Auth profile and set cookies
const auth = await authenticate({
variables: { request: { id: challenge.data.challenge.id, signature } }
});
const auth = await switchAccount({ variables: { request: { account } } });

if (auth.data?.authenticate.__typename === "AuthenticationTokens") {
const accessToken = auth.data?.authenticate.accessToken;
const refreshToken = auth.data?.authenticate.refreshToken;
const idToken = auth.data?.authenticate.idToken;
if (auth.data?.switchAccount.__typename === "AuthenticationTokens") {
const accessToken = auth.data?.switchAccount.accessToken;
const refreshToken = auth.data?.switchAccount.refreshToken;
const idToken = auth.data?.switchAccount.idToken;
signOut();
signIn({ accessToken, idToken, refreshToken });
return reload();
Expand Down Expand Up @@ -112,15 +90,15 @@ const SwitchAccounts: FC = () => {
<ErrorMessage
className="m-2"
error={error}
title="Failed to load profiles"
title="Failed to load accounts"
/>
{accountsAvailable.map((accountAvailable, index) => (
<button
className="flex w-full cursor-pointer items-center justify-between space-x-2 rounded-lg py-3 pr-4 pl-3 text-gray-700 hover:bg-gray-100 dark:text-gray-200 dark:hover:bg-gray-800"
key={accountAvailable?.account.address}
onClick={async () => {
const selectedProfile = accountsAvailable[index].account as Account;
await handleSwitchProfile(selectedProfile.address);
const selectedAccount = accountsAvailable[index].account as Account;
await handleSwitchAccount(selectedAccount.address);
}}
type="button"
>
Expand All @@ -146,7 +124,7 @@ const SwitchAccounts: FC = () => {
</div>
</span>
{isLoading &&
accountAvailable.account.address === loggingInProfileId ? (
accountAvailable.account.address === loggingInAccountId ? (
<Spinner size="xs" />
) : currentAccount?.address === accountAvailable.account.address ? (
<CheckCircleIcon className="size-5 text-green-500" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
mutation SwitchAccount($request: SwitchAccountRequest!) {
switchAccount(request: $request) {
... on AuthenticationTokens {
accessToken
refreshToken
idToken
}
}
}
34 changes: 34 additions & 0 deletions packages/indexer/generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6426,6 +6426,13 @@ export type RefreshMutationVariables = Exact<{

export type RefreshMutation = { __typename?: 'Mutation', refresh: { __typename?: 'AuthenticationTokens', accessToken: any, refreshToken: any, idToken: any } | { __typename?: 'ForbiddenError', reason: string } };

export type SwitchAccountMutationVariables = Exact<{
request: SwitchAccountRequest;
}>;


export type SwitchAccountMutation = { __typename?: 'Mutation', switchAccount: { __typename?: 'AuthenticationTokens', accessToken: any, refreshToken: any, idToken: any } | { __typename?: 'ForbiddenError' } };

export type CreateGroupMutationVariables = Exact<{
request: CreateGroupRequest;
}>;
Expand Down Expand Up @@ -7579,6 +7586,33 @@ export function useRefreshMutation(baseOptions?: Apollo.MutationHookOptions<Refr
export type RefreshMutationHookResult = ReturnType<typeof useRefreshMutation>;
export type RefreshMutationResult = Apollo.MutationResult<RefreshMutation>;
export type RefreshMutationOptions = Apollo.BaseMutationOptions<RefreshMutation, RefreshMutationVariables>;
export const SwitchAccountDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"SwitchAccount"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"request"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"SwitchAccountRequest"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"switchAccount"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"request"},"value":{"kind":"Variable","name":{"kind":"Name","value":"request"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"InlineFragment","typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"AuthenticationTokens"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"accessToken"}},{"kind":"Field","name":{"kind":"Name","value":"refreshToken"}},{"kind":"Field","name":{"kind":"Name","value":"idToken"}}]}}]}}]}}]} as unknown as DocumentNode;
export type SwitchAccountMutationFn = Apollo.MutationFunction<SwitchAccountMutation, SwitchAccountMutationVariables>;

/**
* __useSwitchAccountMutation__
*
* To run a mutation, you first call `useSwitchAccountMutation` within a React component and pass it any options that fit your needs.
* When your component renders, `useSwitchAccountMutation` returns a tuple that includes:
* - A mutate function that you can call at any time to execute the mutation
* - An object with fields that represent the current status of the mutation's execution
*
* @param baseOptions options that will be passed into the mutation, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options-2;
*
* @example
* const [switchAccountMutation, { data, loading, error }] = useSwitchAccountMutation({
* variables: {
* request: // value for 'request'
* },
* });
*/
export function useSwitchAccountMutation(baseOptions?: Apollo.MutationHookOptions<SwitchAccountMutation, SwitchAccountMutationVariables>) {
const options = {...defaultOptions, ...baseOptions}
return Apollo.useMutation<SwitchAccountMutation, SwitchAccountMutationVariables>(SwitchAccountDocument, options);
}
export type SwitchAccountMutationHookResult = ReturnType<typeof useSwitchAccountMutation>;
export type SwitchAccountMutationResult = Apollo.MutationResult<SwitchAccountMutation>;
export type SwitchAccountMutationOptions = Apollo.BaseMutationOptions<SwitchAccountMutation, SwitchAccountMutationVariables>;
export const CreateGroupDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"CreateGroup"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"request"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"CreateGroupRequest"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"createGroup"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"request"},"value":{"kind":"Variable","name":{"kind":"Name","value":"request"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"InlineFragment","typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"CreateGroupResponse"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"hash"}}]}},{"kind":"InlineFragment","typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"SelfFundedTransactionRequest"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"reason"}}]}},{"kind":"InlineFragment","typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"TransactionWillFail"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"reason"}}]}}]}}]}}]} as unknown as DocumentNode;
export type CreateGroupMutationFn = Apollo.MutationFunction<CreateGroupMutation, CreateGroupMutationVariables>;

Expand Down

0 comments on commit 1bf1add

Please sign in to comment.