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

fix: error page for non-MM users #1696

Merged

Conversation

Jennievon
Copy link
Contributor

Why this change is needed

Please provide a description and a link to the underlying ticket

https://github.com/ten-protocol/ten-internal/issues/2594

What changes were made as part of this PR

Please provide a high level list of the changes made

Error message for non-MM users: main-12b4344bfd88e8af.js:1 TypeError: Cannot read properties of undefined (reading 'on')

PR checks pre-merging

Please indicate below by ticking the checkbox that you have read and performed the required
PR checks

  • PR checks reviewed and performed

@Jennievon Jennievon requested a review from zkokelj December 12, 2023 12:59
@Jennievon Jennievon self-assigned this Dec 12, 2023
Copy link

coderabbitai bot commented Dec 12, 2023

Walkthrough

The updates involve enhancing the user experience for connecting a wallet in a web application. The changes include a new import of an ethereum utility, which is used to conditionally render text on a ConnectWalletButton depending on whether the MetaMask extension is detected. The ConnectWalletButton now accepts a text prop to customize its display. Additionally, a utility function to prompt the user to download MetaMask if it's not installed has been added.

Changes

File Path Change Summary
.../frontend/pages/personal/index.tsx Updated EmptyState component props to change button text and action based on ethereum presence.
.../frontend/src/components/.../connect-wallet.tsx Added text prop to ConnectWalletButton and updated onClick logic.
.../frontend/src/components/providers/wallet-provider.tsx Replaced window.ethereum with imported ethereum object and added existence check.
.../frontend/src/lib/utils.ts Added ethereum export for conditional assignment and downloadMetaMask function to open download link.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on X ?


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • You can reply to a review comment from CodeRabbit.
  • You can also chat with CodeRabbit bot directly around the specific lines of code or files in the PR by tagging @coderabbitai in a new comment.
  • You can tag @coderabbitai in a PR comment and ask questions about the PR and the codebase.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 6

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 457777e and a2a7c8a.
Files selected for processing (4)
  • tools/obscuroscan_v3/frontend/pages/personal/index.tsx (2 hunks)
  • tools/obscuroscan_v3/frontend/src/components/modules/common/connect-wallet.tsx (2 hunks)
  • tools/obscuroscan_v3/frontend/src/components/providers/wallet-provider.tsx (3 hunks)
  • tools/obscuroscan_v3/frontend/src/lib/utils.ts (1 hunks)
Additional comments: 13
tools/obscuroscan_v3/frontend/pages/personal/index.tsx (4)
  • 8-8: The import of ethereum from @/src/lib/utils is correctly added to support the conditional rendering logic in the EmptyState component's action prop.

  • 24-24: The update to the EmptyState component's title prop from "Connect your wallet" to "Connect Wallet" is a straightforward text change that improves consistency and clarity.

  • 26-34: The conditional rendering logic within the EmptyState component's action prop is correctly implemented. It uses the presence of the ethereum object to determine the text displayed on the ConnectWalletButton, providing a clear message to the user based on whether MetaMask is installed.

  • 27-33: Verify that the ConnectWalletButton component has been updated to accept and properly handle the new text prop.

tools/obscuroscan_v3/frontend/src/components/modules/common/connect-wallet.tsx (5)
  • 6-7: The addition of the text prop to the ConnectWalletButton component is consistent with the PR's objective to provide clearer messaging for users without MetaMask.

  • 15-21: The updated onClick handler logic now correctly handles the presence or absence of ethereum, providing a fallback action to download MetaMask when it's not present. This change should prevent the TypeError non-MetaMask users were experiencing.

  • 36-36: The conditional rendering of the button text using the text prop or a default value is a good user experience improvement, making it clear to users what action is expected of them.

  • 6-6: Importing the ethereum object from utils.ts is a good practice for maintainability and testability, as it centralizes the access to ethereum and abstracts away the direct dependency on the global window object.

  • 22-22: Verify the need for the suppressHydrationWarning prop on the Button component to ensure it's being used intentionally and not masking a potential issue with server/client markup mismatch.

tools/obscuroscan_v3/frontend/src/components/providers/wallet-provider.tsx (4)
  • 9-9: The import of ethereum from utils is a good practice to centralize the access to the ethereum object and handle its absence in a single place.

  • 33-34: The conditional check for ethereum before creating a new Web3Provider is a good addition to prevent errors for non-MetaMask users.

  • 67-70: The early return in the useEffect hook if ethereum is not present is a good defensive programming practice.

  • 79-83: Setting up and removing the accountsChanged event listener is correctly handled with the presence check for ethereum.

Comment on lines +14 to +15
export const { ethereum } =
typeof window !== "undefined" ? window : ({} as any);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ethereum export is incorrectly assigned the entire window object instead of window.ethereum.

- export const { ethereum } =
-   typeof window !== "undefined" ? window : ({} as any);
+ export const ethereum =
+   typeof window !== "undefined" ? window.ethereum : ({} as any);

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
export const { ethereum } =
typeof window !== "undefined" ? window : ({} as any);
export const ethereum =
typeof window !== "undefined" ? window.ethereum : ({} as any);

Comment on lines +17 to +18
export const downloadMetaMask = () => {
window ? window.open("https://metamask.io/download", "_blank") : null;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The downloadMetaMask function should check for the existence of window.open before attempting to call it.

- window ? window.open("https://metamask.io/download", "_blank") : null;
+ if (window && typeof window.open === 'function') {
+   window.open("https://metamask.io/download", "_blank");
+ }

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
export const downloadMetaMask = () => {
window ? window.open("https://metamask.io/download", "_blank") : null;
export const downloadMetaMask = () => {
if (window && typeof window.open === 'function') {
window.open("https://metamask.io/download", "_blank");
}

@@ -3,15 +3,23 @@ import { Button } from "@/src/components/ui/button";
import { Link2Icon, LinkBreak2Icon } from "@radix-ui/react-icons";
import React from "react";
import TruncatedAddress from "./truncated-address";
const ConnectWalletButton = () => {
import { downloadMetaMask, ethereum } from "@/src/lib/utils";
const ConnectWalletButton = ({ text }: { text?: string }) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding documentation or comments explaining the purpose and expected values for the text prop to ensure clarity for other developers.

@@ -10,3 +10,10 @@
const date = new Date(Number(unixTimestampSeconds) * 1000);
return formatDistanceToNow(date, { addSuffix: true });
}

export const { ethereum } =
typeof window !== "undefined" ? window : ({} as any);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fallback type for ethereum when window is not defined is any, which is not type-safe. Consider defining a more specific type or interface for the ethereum object.

-   typeof window !== "undefined" ? window : ({} as any);
+   typeof window !== "undefined" ? window.ethereum : ({} as EthereumType);

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
typeof window !== "undefined" ? window : ({} as any);
typeof window !== "undefined" ? window.ethereum : ({} as EthereumType);

typeof window !== "undefined" ? window : ({} as any);

export const downloadMetaMask = () => {
window ? window.open("https://metamask.io/download", "_blank") : null;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The downloadMetaMask function uses a ternary operator unnecessarily. Simplify this to an if-statement for better readability.

- window ? window.open("https://metamask.io/download", "_blank") : null;
+ if (window) {
+   window.open("https://metamask.io/download", "_blank");
+ }

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
window ? window.open("https://metamask.io/download", "_blank") : null;
if (window) {
window.open("https://metamask.io/download", "_blank");
}

(window as any).ethereum
);
if (ethereum) {
const ethProvider = new ethers.providers.Web3Provider(ethereum);
setProvider(ethProvider);

try {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider providing a more informative error message or guidance in the else block of the connectWallet function for users without MetaMask.

 showToast(
   ToastType.DESTRUCTIVE,
-  "No ethereum object found. Please install MetaMask!"
+  "MetaMask is not detected. Please install MetaMask to connect your wallet, or visit [link to guide] for more information."
 );

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
try {
try {

Copy link
Contributor

@zkokelj zkokelj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@Jennievon Jennievon merged commit 7c2a4df into main Dec 12, 2023
1 of 2 checks passed
@Jennievon Jennievon deleted the jennifer/2594-tenscan-throws-error-for-users-without-mm branch December 12, 2023 13:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants