Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persist transfer history to local storage #37

Merged
merged 6 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"react-toastify": "^9.1.3",
"wagmi": "0.12.18",
"zod": "^3.21.4",
"zustand": "^4.3.8"
"zustand": "^4.3.9"
},
"devDependencies": {
"@trivago/prettier-plugin-sort-imports": "^4.1.1",
Expand Down
73 changes: 41 additions & 32 deletions src/features/store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { create } from 'zustand';
import { persist } from 'zustand/middleware';

import type { TransferContext, TransferStatus } from './transfer/types';

Expand All @@ -22,35 +23,43 @@ export interface AppState {
setIsSenderNftOwner: (isOwner: boolean | null) => void;
}

export const useStore = create<AppState>()((set) => ({
transfers: [],
addTransfer: (t) => {
set((state) => ({ transfers: [...state.transfers, t] }));
},
updateTransferStatus: (i, s, options) => {
set((state) => {
if (i >= state.transfers.length) return state;
const txs = [...state.transfers];
txs[i].status = s;
txs[i].msgId ||= options?.msgId;
txs[i].originTxHash ||= options?.originTxHash;
return {
transfers: txs,
};
});
},
balances: {
senderBalance: '0',
senderNftIds: null,
isSenderNftOwner: false,
},
setSenderBalance: (senderBalance) => {
set((state) => ({ balances: { ...state.balances, senderBalance } }));
},
setSenderNftIds: (senderNftIds) => {
set((state) => ({ balances: { ...state.balances, senderNftIds } }));
},
setIsSenderNftOwner: (isSenderNftOwner) => {
set((state) => ({ balances: { ...state.balances, isSenderNftOwner } }));
},
}));
export const useStore = create<AppState>()(
persist(
(set) => ({
transfers: [],
addTransfer: (t) => {
set((state) => ({ transfers: [...state.transfers, t] }));
},
updateTransferStatus: (i, s, options) => {
set((state) => {
if (i >= state.transfers.length) return state;
const txs = [...state.transfers];
txs[i].status = s;
txs[i].msgId ||= options?.msgId;
txs[i].originTxHash ||= options?.originTxHash;
return {
transfers: txs,
};
});
},
balances: {
senderBalance: '0',
senderNftIds: null,
isSenderNftOwner: false,
},
setSenderBalance: (senderBalance) => {
set((state) => ({ balances: { ...state.balances, senderBalance } }));
},
setSenderNftIds: (senderNftIds) => {
set((state) => ({ balances: { ...state.balances, senderNftIds } }));
},
setIsSenderNftOwner: (isSenderNftOwner) => {
set((state) => ({ balances: { ...state.balances, isSenderNftOwner } }));
},
}),
{
name: 'app-state',
partialize: (state) => ({ transfers: state.transfers }),
},
),
);
1 change: 1 addition & 0 deletions src/features/transfer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ export interface TransferContext {
params: TransferFormValues;
originTxHash?: string;
msgId?: string;
timestamp?: number;
}
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ __metadata:
typescript: ^5.0.4
wagmi: 0.12.18
zod: ^3.21.4
zustand: ^4.3.8
zustand: ^4.3.9
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -10580,9 +10580,9 @@ __metadata:
languageName: node
linkType: hard

"zustand@npm:^4.3.8":
version: 4.3.8
resolution: "zustand@npm:4.3.8"
"zustand@npm:^4.3.9":
version: 4.3.9
resolution: "zustand@npm:4.3.9"
dependencies:
use-sync-external-store: 1.2.0
peerDependencies:
Expand All @@ -10593,6 +10593,6 @@ __metadata:
optional: true
react:
optional: true
checksum: 24db6bf063ce1fc8b2ee238f13211a88f43236541a716e5f6f706f613c671a45332465f9ed06d694f8c353da3d24c53ea668e5712a86aceda9ad74f6c433e8c0
checksum: fc83d653913fa537c354ba8b95d3a4fdebe62d2ebd3d9f5aeff2edf062811c0f5af48e02ab4da32b666752fd4f3e78c2b44624e445254f48503595435d4a7d70
languageName: node
linkType: hard
Loading