Skip to content

Commit

Permalink
♻️ split aliases by user address
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin Burg committed Mar 28, 2024
1 parent cb5ec01 commit 7acd1ac
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
34 changes: 21 additions & 13 deletions context/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
Tzip16Module,
} from "@taquito/tzip16";
import BigNumber from "bignumber.js";
import Router from "next/router";
import { Context, createContext, Dispatch } from "react";
import { contractStorage } from "../types/app";
import { Trie } from "../utils/radixTrie";
Expand All @@ -30,6 +29,7 @@ type tezosState = {
accountInfo: AccountInfo | null;
contracts: { [address: string]: contractStorage };
aliases: { [address: string]: string };
aliasesByUser: { [address: string]: { [addr: string]: string } };
aliasTrie: Trie<string>;
hasBanner: boolean;
delegatorAddresses: string[] | undefined;
Expand All @@ -48,6 +48,7 @@ type tezosState = {
type storage = {
contracts: { [address: string]: contractStorage };
aliases: { [address: string]: string };
aliasesByUser: { [address: string]: { [addr: string]: string } };
importedWallets: {
[address: string]: { [contractAddr: string]: contractStorage };
};
Expand Down Expand Up @@ -90,6 +91,7 @@ let emptyState = (): tezosState => {
proposalRefresher: 0,
attemptedInitialLogin: false,
importedWallets: {},
aliasesByUser: {},
};
};

Expand Down Expand Up @@ -159,7 +161,6 @@ type action =
};

const saveState = (state: tezosState) => {
console.log("state.currentContract", state.currentContract);
const storage = JSON.parse(localStorage.getItem("app_state")!);
localStorage.setItem(
"app_state",
Expand All @@ -171,6 +172,9 @@ const saveState = (state: tezosState) => {
importedWallets: storage
? { ...storage.importedWallets, ...state.importedWallets }
: state.importedWallets,
aliasesByUser: storage
? { ...storage.aliasesByUser, ...state.aliasesByUser }
: state.aliasesByUser,
})
);
};
Expand Down Expand Up @@ -224,9 +228,7 @@ function reducer(state: tezosState, action: action): tezosState {
[action.payload.address]: action.payload.contract,
};

console.log("---- ", state, action);

const newState = {
const newState: tezosState = {
...state,
contracts: contracts,
aliases: aliases,
Expand All @@ -239,10 +241,12 @@ function reducer(state: tezosState, action: action): tezosState {
[action.payload.address]: action.payload.contract,
},
},
aliasesByUser: {
...state.aliasesByUser,
[state.address!]: aliases,
},
};

console.warn(newState);

saveState(newState);

return newState;
Expand Down Expand Up @@ -336,24 +340,27 @@ function reducer(state: tezosState, action: action): tezosState {
{}
)
: {};
// const currentContract = Object.keys(contracts).at(0) || null;
// if (currentContract) Router.push(`/${currentContract}/proposals`);
console.log("contracts", contracts);
const aliases =
storage?.aliasesByUser && storage.aliasesByUser[action.address]
? storage.aliasesByUser[action.address]
: state.aliases;

const currentContract = Object.keys(contracts).at(0) || null;
return {
...state,
balance: action.balance,
accountInfo: action.accountInfo,
address: action.address,
attemptedInitialLogin: true,
contracts,
// currentContract,
// importedWallets: {...state.importedWallets, [action.address] : {}}
currentContract,
aliases,
};
}
case "logout": {
let { connection } = emptyState();

const newState = {
const newState: tezosState = {
...state,
beaconWallet: null,
balance: null,
Expand All @@ -363,6 +370,7 @@ function reducer(state: tezosState, action: action): tezosState {
p2pClient: null,
contracts: {},
currentContract: null,
aliases: {},
};

saveState(newState);
Expand Down
2 changes: 0 additions & 2 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ export default function App({ Component, pageProps }: AppProps) {

if ((path === "/" || path === "") && contracts.length > 0) {
const contract = contracts[0];
console.log("🚀 ~ useEffect ~ contract:", contract);

router.replace(`/${contract}/proposals`);
return;
Expand Down Expand Up @@ -156,7 +155,6 @@ export default function App({ Component, pageProps }: AppProps) {
state.currentStorage,
state.connection,
]);
console.log("state", state);
useEffect(() => {
(async () => {
if (state!.beaconWallet === null) {
Expand Down

0 comments on commit 7acd1ac

Please sign in to comment.