on master help sign in
Clear state on logout

- add resetStore function, in line with Zustand docs (https://docs.pmnd.rs/zustand/guides/how-to-reset-state)

commitdbd36845cde42d5a5f3ac6954874044d4e32b953 authorShawn Tobin <shawn.david.tobin@gmail.com> dateThu 5 Oct 2023, 15:24 (3 years ago) parent703ff86
2 files changed+16−5
src/components/L2/Dropdowns/AccountsDropdown.tsx+3−3 src/store/rollerStore.ts+13−2
src/components/L2/Dropdowns/AccountsDropdown.tsx+3−3
@@ -1,7 +1,6 @@
11import { useCallback, useMemo, useState } from 'react';
22import { Icon, Row, Box } from '@tlon/indigo-react';
33import { Just } from 'folktale/maybe';
4−
54import LayerIndicator from 'components/L2/LayerIndicator';
65import CopiableAddressWrap from 'components/copiable/CopiableAddressWrap';
76import { TooltipPosition } from 'components/WithTooltip';
@@ -26,7 +25,7 @@ const AccountsDropdown = ({ showMigrate = false }: AccountsDropdownProps) => {
2625 const walletInfo: any = useWallet();
2726 const { push, popTo, names, reset }: any = useHistory();
2827 const { setPointCursor }: any = usePointCursor();
29− const { point, pointList } = useRollerStore();
28+ const { point, pointList, resetStore } = useRollerStore();
3029
3130 const canBitcoin = Just.hasInstance(walletInfo.urbitWallet);
3231
@@ -65,7 +64,8 @@ const AccountsDropdown = ({ showMigrate = false }: AccountsDropdownProps) => {
6564
6665 const onLogout = () => {
6766 clearInvitesStorage();
68− reset();
67+ reset(); // router
68+ resetStore();
6969 };
7070
7171 const hasL1Points = Boolean(pointList.find(({ layer }) => layer === 1));
src/store/rollerStore.ts+13−2
@@ -20,7 +20,7 @@ export const EMPTY_POINT = new Point({
2020 address: '',
2121});
2222
23−export interface RollerStore {
23+export interface RollerState {
2424 loading: boolean;
2525 nextQuotaTime: number;
2626 pendingTransactions: PendingTransaction[];
@@ -31,6 +31,9 @@ export interface RollerStore {
3131 modalText?: string;
3232 recentlyCompleted: number;
3333 ethBalance: BN;
34+}
35+
36+export interface RollerActions {
3437 setLoading: (loading: boolean) => void;
3538 setModalText: (modalText: string) => void;
3639 setNextQuotaTime: (nextQuotaTime: number) => void;
@@ -41,9 +44,10 @@ export interface RollerStore {
4144 storePendingL1Txn: (txn: PendingL1Txn) => void;
4245 deletePendingL1Txn: (txn: PendingL1Txn) => void;
4346 setEthBalance: (ethBalance: BN) => void;
47+ resetStore: () => void;
4448}
4549
46−export const useRollerStore = create<RollerStore>(set => ({
50+const initialRollerState: RollerState = {
4751 loading: false,
4852 nextQuotaTime: new Date().getTime() + 24 * HOUR,
4953 pendingTransactions: [],
@@ -53,6 +57,10 @@ export const useRollerStore = create<RollerStore>(set => ({
5357 pendingL1ByPoint: {},
5458 recentlyCompleted: 0,
5559 ethBalance: toBN(0),
60+};
61+
62+export const useRollerStore = create<RollerState & RollerActions>(set => ({
63+ ...initialRollerState,
5664 setLoading: (loading: boolean) => set(() => ({ loading })),
5765 setNextQuotaTime: (nextQuotaTime: number) => set(() => ({ nextQuotaTime })),
5866 setPendingTransactions: (pendingTransactions: PendingTransaction[]) =>
@@ -92,4 +100,7 @@ export const useRollerStore = create<RollerStore>(set => ({
92100 return { pendingL1ByPoint: newPending };
93101 }),
94102 setEthBalance: (ethBalance: BN) => set(() => ({ ethBalance })),
103+ resetStore: () => {
104+ set(initialRollerState);
105+ },
95106}));