Skip to content

Commit

Permalink
Add feature flag and clear all button for custom address labeler
Browse files Browse the repository at this point in the history
  • Loading branch information
sealer3 committed Apr 27, 2024
1 parent 8ec9e84 commit 707aa7a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 21 deletions.
9 changes: 9 additions & 0 deletions src/api/address-resolver/CustomLabelResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ export class CustomLabelFetcher {
return undefined;
}
}

public getAllAddresses(): string[] {
return Array.from(this.localStorageLabels.keys());
}

public clearAll() {
this.localStorageLabels.clear();
this.localStorageLabels.clear();
}
}

export class CustomLabelResolver extends BasicAddressResolver {
Expand Down
61 changes: 40 additions & 21 deletions src/execution/address/AddressSubtitle.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { faPencil, faTag, faTimes } from "@fortawesome/free-solid-svg-icons";
import {
faPencil,
faTag,
faTimes,
faTrash,
} from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { FC, useContext, useState } from "react";
import Blockies from "react-blockies";
Expand All @@ -10,7 +15,7 @@ import { useResolvedAddress } from "../../useResolvedAddresses";
import { RuntimeContext } from "../../useRuntime";
import { AddressAwareComponentProps } from "../types";
import AddressAttributes from "./AddressAttributes";
import EditableAddressTag from "./EditableAddressTag";
import EditableAddressTag, { clearAllLabels } from "./EditableAddressTag";

type AddressSubtitleProps = AddressAwareComponentProps & {
isENS: boolean | undefined;
Expand Down Expand Up @@ -64,25 +69,39 @@ const AddressSubtitle: FC<AddressSubtitleProps> = ({
<span className="pl-1 text-nowrap">{resolvedName}</span>
</div>
)}
<div className="flex flex-no-wrap space-x-1">
{editingAddressTag && (
<EditableAddressTag
address={address}
defaultTag={resolvedName}
editedCallback={(address: string) => setEditingAddressTag(false)}
/>
)}
<button
className={`flex-no-wrap flex items-center justify-center space-x-1 self-center text-gray-500 focus:outline-none transition-shadows h-7 w-7 rounded-full bg-gray-200 text-xs transition-colors hover:bg-gray-500 hover:text-gray-200 hover:shadow`}
title={editingAddressTag ? "Cancel changes" : "Edit address label"}
onClick={() => setEditingAddressTag(!editingAddressTag)}
>
<FontAwesomeIcon
icon={editingAddressTag ? faTimes : faPencil}
size="1x"
/>
</button>
</div>
{config?.WIP_customAddressLabels && (
<div className="flex flex-no-wrap space-x-1">
{editingAddressTag && (
<EditableAddressTag
address={address}
defaultTag={resolvedName}
editedCallback={(address: string) =>
setEditingAddressTag(false)
}
/>
)}
<button
className={`flex-no-wrap flex items-center justify-center space-x-1 self-center text-gray-500 focus:outline-none transition-shadows h-7 w-7 rounded-full bg-gray-200 text-xs transition-colors hover:bg-gray-500 hover:text-gray-200 hover:shadow`}
title={
editingAddressTag ? "Cancel changes" : "Edit address label"
}
onClick={() => setEditingAddressTag(!editingAddressTag)}
>
<FontAwesomeIcon
icon={editingAddressTag ? faTimes : faPencil}
size="1x"
/>
</button>
{/* For debugging only; we'll want to create an address label management page. */}
<button
className={`flex-no-wrap flex items-center justify-center space-x-1 self-center text-red-500 focus:outline-none transition-shadows h-7 w-7 rounded-full bg-red-200 text-xs transition-colors hover:bg-red-500 hover:text-gray-200 hover:shadow`}
title={"Delete all labels"}
onClick={clearAllLabels}
>
<FontAwesomeIcon icon={faTrash} size="1x" />
</button>
</div>
)}
</div>
</StandardSubtitle>
);
Expand Down
12 changes: 12 additions & 0 deletions src/execution/address/EditableAddressTag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ async function setAddressLabel(address: string, label: string | null) {
mutate(address, [customLabelResolver, trimmedLabel]);
}

export async function clearAllLabels() {
const customLabelFetcher = CustomLabelFetcher.getInstance();
const addresses: string[] = customLabelFetcher.getAllAddresses();
await customLabelFetcher.updateLabels(
addresses.reduce(
(obj: Record<string, string>, key: string) => ({ ...obj, [key]: "" }),
{},
),
);
addresses.forEach((address) => mutate(address));
}

const EditableAddressTag: FC<EditableAddressTagProps> = ({
address,
defaultTag,
Expand Down
6 changes: 6 additions & 0 deletions src/useConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ export type OtterscanConfig = {
*/
l1ExplorerURL?: string;
};

/**
* Temporary config option, until address labels are complete: Enables setting
* address labels which are kept in local storage.
*/
WIP_customAddressLabels?: boolean;
};

/**
Expand Down

0 comments on commit 707aa7a

Please sign in to comment.