-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from dwhiffing/toggle-contracts
Toggle contracts
- Loading branch information
Showing
10 changed files
with
124 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Pencil, Circle, CircleCheck } from "lucide-react"; | ||
import { CHAINS } from "@/constants"; | ||
import { IContract } from "@/types"; | ||
import { shortenAddress } from "../utils/shortenAddress"; | ||
import { useContracts } from "@/utils/useContracts"; | ||
|
||
export const ContractItem = (props: { | ||
contract: IContract; | ||
onEdit?: (key: number) => void; | ||
}) => { | ||
const { disabledKeys, setDisabledKeys } = useContracts(); | ||
const isDisabled = disabledKeys.includes(props.contract.key); | ||
const DisabledIcon = isDisabled ? Circle : CircleCheck; | ||
return ( | ||
<div className="border p-3 rounded-md"> | ||
<div | ||
className={`flex items-center gap-2 ${isDisabled ? "opacity-50" : ""}`} | ||
> | ||
<div className="flex flex-col flex-1"> | ||
<span> | ||
{props.contract.name}{" "} | ||
<small className="text-muted-foreground"> | ||
({CHAINS[props.contract.chainId]?.name}) | ||
</small> | ||
</span> | ||
<small className="font-xs font-mono text-muted-foreground"> | ||
{shortenAddress(props.contract.address)} | ||
</small> | ||
{props.contract.description && ( | ||
<small className="font-xs text-muted-foreground mt-1"> | ||
{props.contract.description} | ||
</small> | ||
)} | ||
</div> | ||
|
||
{props.onEdit && ( | ||
<Pencil | ||
onClick={() => props.onEdit?.(props.contract.key)} | ||
className="h-4 w-4 cursor-pointer" | ||
/> | ||
)} | ||
|
||
<DisabledIcon | ||
className="h-4 w-4 cursor-pointer" | ||
onClick={() => | ||
isDisabled | ||
? setDisabledKeys( | ||
disabledKeys.filter((k) => k !== props.contract.key), | ||
) | ||
: setDisabledKeys([...disabledKeys, props.contract.key]) | ||
} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
export default function useLocalStorage<T>( | ||
key: string, | ||
defaultValue: T, | ||
): [T, (value: T) => void] { | ||
const [value, setValue] = useState(defaultValue); | ||
|
||
useEffect(() => { | ||
const item = localStorage.getItem(key); | ||
|
||
if (!item) { | ||
localStorage.setItem(key, JSON.stringify(defaultValue)); | ||
} | ||
|
||
setValue(item ? JSON.parse(item) : defaultValue); | ||
|
||
function handler(e: StorageEvent) { | ||
if (e.key !== key) return; | ||
|
||
const lsi = localStorage.getItem(key); | ||
setValue(JSON.parse(lsi ?? "")); | ||
} | ||
|
||
window.addEventListener("storage", handler); | ||
|
||
return () => { | ||
window.removeEventListener("storage", handler); | ||
}; | ||
}, []); | ||
|
||
const setValueWrap = (value: T) => { | ||
try { | ||
setValue(value); | ||
|
||
localStorage.setItem(key, JSON.stringify(value)); | ||
if (typeof window !== "undefined") { | ||
window.dispatchEvent(new StorageEvent("storage", { key })); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}; | ||
|
||
return [value, setValueWrap]; | ||
} |