Skip to content

Commit

Permalink
Refactored non-react controllers/helpers to use `EXCEPTION_CASE_VANIL…
Browse files Browse the repository at this point in the history
…LA_getCommunityById`
  • Loading branch information
mzparacha committed Aug 8, 2024
1 parent 435cfcd commit f1fd705
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 11 deletions.
51 changes: 43 additions & 8 deletions packages/commonwealth/client/scripts/controllers/app/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { Magic } from 'magic-sdk';

import axios from 'axios';
import app from 'state';
import { EXCEPTION_CASE_VANILLA_getCommunityById } from 'state/api/communities/getCommuityById';
import { fetchProfilesByAddress } from 'state/api/profiles/fetchProfilesByAddress';
import {
onUpdateEmailError,
Expand All @@ -35,7 +36,7 @@ import { userStore } from 'state/ui/user';
import Account from '../../models/Account';
import AddressInfo from '../../models/AddressInfo';
import type BlockInfo from '../../models/BlockInfo';
import type ChainInfo from '../../models/ChainInfo';
import ChainInfo from '../../models/ChainInfo';

function storeActiveAccount(account: Account) {
const user = userStore.getState();
Expand Down Expand Up @@ -282,7 +283,18 @@ export async function createUserWithAddress(
});

const id = response.data.result.id;
const chainInfo = app.config.chains.getById(chain);

// HACK: 8762 -- find a way to call getCommunityById trpc in non-react files
// when u do, update `EXCEPTION_CASE_VANILLA_getCommunityById` name and make the
// call from that function
const communityInfo = await EXCEPTION_CASE_VANILLA_getCommunityById(
chain || '',
true,
);
const chainInfo = ChainInfo.fromJSON({
...(communityInfo as any),
});

const account = new Account({
addressId: id,
address,
Expand Down Expand Up @@ -412,8 +424,19 @@ export async function handleSocialLoginCallback({
}): Promise<{ address: string; isAddressNew: boolean }> {
// desiredChain may be empty if social login was initialized from
// a page without a chain, in which case we default to an eth login
// @ts-expect-error StrictNullChecks
const desiredChain = app.chain?.meta || app.config.chains.getById(chain);
let desiredChain = app.chain?.meta;
if (!desiredChain && chain) {
// HACK: 8762 -- find a way to call getCommunityById trpc in non-react files
// when u do, update `EXCEPTION_CASE_VANILLA_getCommunityById` name and make the
// call from that function
const communityInfo = await EXCEPTION_CASE_VANILLA_getCommunityById(
chain || '',
true,
);
desiredChain = ChainInfo.fromJSON({
...(communityInfo as any),
});
}
const isCosmos = desiredChain?.base === ChainBase.CosmosSDK;
const magic = await constructMagic(isCosmos, desiredChain?.id);
const isEmail = walletSsoSource === WalletSsoSource.Email;
Expand Down Expand Up @@ -548,10 +571,22 @@ export async function handleSocialLoginCallback({
// This is code from before desiredChain was implemented, and
// may not be necessary anymore:
if (app.chain) {
const c =
userStore.getState().activeCommunity ||
app.config.chains.getById(app.activeChainId());
await updateActiveAddresses({ chain: c });
let chainInfo = userStore.getState().activeCommunity;

// HACK: 8762 -- find a way to call getCommunityById trpc in non-react files
// when u do, update `EXCEPTION_CASE_VANILLA_getCommunityById` name and make the
// call from that function
if (!chainInfo && chain) {
const communityInfo = await EXCEPTION_CASE_VANILLA_getCommunityById(
chain || '',
true,
);
chainInfo = ChainInfo.fromJSON({
...(communityInfo as any),
});
}

chainInfo && (await updateActiveAddresses({ chain: chainInfo }));
}

const { Profiles: profiles, email: ssoEmail } = response.data.result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import app from 'state';
import type Web3 from 'web3';

import { SIWESigner } from '@canvas-js/chain-ethereum';
import { EXCEPTION_CASE_VANILLA_getCommunityById } from 'state/api/communities/getCommuityById';
import { userStore } from 'state/ui/user';
import { hexToNumber } from 'web3-utils';
import BlockInfo from '../../../models/BlockInfo';
Expand Down Expand Up @@ -99,8 +100,26 @@ class WalletConnectWebWalletController implements IWebWallet<string> {
this._enabling = true;
// try {
// Create WalletConnect Provider
this._chainInfo =
app.chain?.meta || app.config.chains.getById(this.defaultNetwork);
this._chainInfo = app?.chain?.meta;

// HACK: 8762 -- find a way to call getCommunityById trpc in non-react files
// when u do, update `EXCEPTION_CASE_VANILLA_getCommunityById` name and make the
// call from that function
{
if (!this._chainInfo && app.activeChainId()) {
const communityInfo = await EXCEPTION_CASE_VANILLA_getCommunityById(
app.activeChainId() || '',
true,
);

const chainInfo = ChainInfo.fromJSON({
...(communityInfo as any),
});

this._chainInfo = chainInfo;
}
}

const chainId = this._chainInfo.node?.ethChainId || 1;
const EthereumProvider = (await import('@walletconnect/ethereum-provider'))
.default;
Expand Down
12 changes: 11 additions & 1 deletion packages/commonwealth/client/scripts/helpers/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { updateActiveAddresses } from 'controllers/app/login';
import { DEFAULT_CHAIN } from 'helpers/constants';
import app, { ApiStatus } from 'state';
import ChainInfo from '../models/ChainInfo';
import { EXCEPTION_CASE_VANILLA_getCommunityById } from '../state/api/communities/getCommuityById';
import { userStore } from '../state/ui/user';

export const deinitChainOrCommunity = async () => {
Expand Down Expand Up @@ -39,7 +40,16 @@ export const loadCommunityChainInfo = async (
if (activeCommunity) {
tempChain = activeCommunity;
} else {
tempChain = app.config.chains.getById(DEFAULT_CHAIN);
// HACK: 8762 -- find a way to call getCommunityById trpc in non-react files
// when u do, update `EXCEPTION_CASE_VANILLA_getCommunityById` name and make the
// call from that function
const communityInfo = await EXCEPTION_CASE_VANILLA_getCommunityById(
DEFAULT_CHAIN,
true,
);
tempChain = ChainInfo.fromJSON({
...(communityInfo as any),
});
}

if (!tempChain) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Community } from '@hicommonwealth/schemas';
import axios from 'axios';
import { trpc } from 'utils/trpcClient';
import { z } from 'zod';
import app from '../../index';
import { queryClient } from '../config';

const COMMUNITIY_STALE_TIME = 60 * 3_000; // 3 mins
Expand Down Expand Up @@ -43,6 +45,19 @@ export const invalidateAllQueriesForCommunity = async (communityId: string) => {
}
};

export const EXCEPTION_CASE_VANILLA_getCommunityById = async (
communityId: string,
includeNodeInfo = false,
): Promise<z.infer<typeof Community>> => {
// HACK: 8762 -- find a way to call getCommunityById trpc in non-react files
// when u do, update `EXCEPTION_CASE_VANILLA_getCommunityById` name and make the
// call from that function
const response = await axios.get(`
${app.serverUrl()}/v1/community.getCommunity?batch=1&input=%7B%220%22%3A%7B%22id%22%3A%22${communityId}%22%2C%22include_node_info%22%3A${includeNodeInfo}%7D%7D

Check warning on line 56 in packages/commonwealth/client/scripts/state/api/communities/getCommuityById.ts

View workflow job for this annotation

GitHub Actions / Code Quality Recommendations (20)

This line has a length of 163. Maximum allowed is 120
`);
return response?.data[0]?.result.data;
};

const useGetCommunityByIdQuery = ({
id,
includeNodeInfo = false,
Expand Down

0 comments on commit f1fd705

Please sign in to comment.