Skip to content

Commit 8734e91

Browse files
authored
Merge pull request #38 from glazec/master
add local storage support
2 parents 7b77545 + cd395ef commit 8734e91

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

src/app/containers/Contracts.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,30 @@ export function useContracts() {
2020

2121
const shiftDown = () => {
2222
setSelectedIdx((prev) =>
23-
prev === null || prev === contracts.length - 1 ? prev : prev + 1,
23+
prev === null || prev === contracts.length - 1 ? prev : prev + 1
2424
);
2525
};
2626

27-
const addContract = (contract: Contract) =>
27+
const addContract = (contract: Contract) => {
28+
var newContracts;
2829
setContracts((prevContracts) => {
29-
const newContracts = [...prevContracts, contract];
30-
return newContracts.sort((a, b) => a.name.localeCompare(b.name));
30+
newContracts = [...prevContracts, contract].sort((a, b) =>
31+
a.name.localeCompare(b.name)
32+
);
33+
return newContracts;
3134
});
35+
localStorage.setItem("contracts", JSON.stringify(newContracts));
36+
};
3237

38+
const overwriteContract = (contracts: Contract[]) => {
39+
var newContracts;
40+
setContracts(() => {
41+
newContracts = [...contracts].sort((a, b) =>
42+
a.name.localeCompare(b.name)
43+
);
44+
return newContracts;
45+
});
46+
};
3347
const removeContractByIdx = (idx: number) => {
3448
setSelectedIdx(null);
3549
setContracts((prevContracts) => prevContracts.filter((x, i) => idx !== i));
@@ -84,7 +98,7 @@ export function useContracts() {
8498

8599
const removeByPath = (path: string) => {
86100
setContracts((prevContracts) =>
87-
prevContracts.filter((c) => c.path !== path),
101+
prevContracts.filter((c) => c.path !== path)
88102
);
89103
};
90104

@@ -94,6 +108,7 @@ export function useContracts() {
94108
removeContractByIdx,
95109
addByAbi,
96110
addByArtifact,
111+
overwriteContract,
97112
upsertByPath,
98113
removeByPath,
99114
selectedIdx,

src/app/features/main/Main.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from "react";
2+
import { useEffect } from "react";
23
import styled from "styled-components";
34
import { Fieldset, Panel } from "react95";
45

@@ -39,8 +40,17 @@ const FooterPanel = styled(Panel)`
3940
`;
4041

4142
const Main = () => {
42-
const { selectedContract: contract } = Contracts.useContainer();
43-
useQueryStringContract()
43+
const { selectedContract: contract, overwriteContract } =
44+
Contracts.useContainer();
45+
useQueryStringContract();
46+
if (localStorage.getItem("contracts")) {
47+
useEffect(() => {
48+
let result = localStorage.getItem("contracts");
49+
result = `{"data":${result}}`;
50+
let jsonResult = JSON.parse(result).data;
51+
overwriteContract(jsonResult);
52+
}, [contract]);
53+
}
4454

4555
return (
4656
<Container>

0 commit comments

Comments
 (0)