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

[AVATAR] Reload on error #2892

Open
juliusmarminge opened this issue May 10, 2024 · 2 comments
Open

[AVATAR] Reload on error #2892

juliusmarminge opened this issue May 10, 2024 · 2 comments

Comments

@juliusmarminge
Copy link

juliusmarminge commented May 10, 2024

Feature request

Overview

I'd like to retry loading the image on error. I tried something using the onLoadingStatusChanged but didn't manage to get it working.

For context, this is my current hacky playground:

const AvatarImage = React.forwardRef<
  React.ElementRef<typeof AvatarPrimitive.Image>,
  React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Image>
>(({ className, ...props }, ref) => {
  const [retries, setRetries] = React.useState(0);
  return (
    <AvatarPrimitive.Image
      onLoadingStatusChange={(status) => {
        console.log("status", { status, retries, ref });
        if (status === "error" && retries < 3) {
          setTimeout(() => {
            console.log("retrying", { retries, ref });
            setRetries((retries) => retries + 1);
          }, 200);
        }
      }}
      ref={ref}
      className={twMerge("aspect-square h-full w-full", className)}
      {...props}
    />
  );
});

but the retrying is only logged once, and the ref is always null:

CleanShot 2024-05-10 at 12 04 08

Is this possible and I'm just dumb and can't figure it out? Could this be a built in prop perhaps?

<AvatarPrimitive.Image
  // Retry loading the image 3 times with 100ms intervals
  retryTimes={3}
  retryInterval={100}
/>

Examples in other libraries

N/A

Who does this impact? Who is this for?

The usecase is for when updating a profile picture, sometimes it takes a short while for the image to have the correct metadata on the server to be rendered properly by the browser. This causes the image to error immediately after being uploaded. I'd like to just retry loading the image a couple times (like retry it 3 times in 100ms intervals).

Additional context

@juliusmarminge
Copy link
Author

juliusmarminge commented May 10, 2024

oh wait i am stupid... that callback doesn't fire multiple times since the status doesn't change again after it's error...

yea solved it in user-code

@juliusmarminge juliusmarminge closed this as not planned Won't fix, can't repro, duplicate, stale May 10, 2024
@juliusmarminge
Copy link
Author

Actually it didn't work since you unmount the image on error

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

No branches or pull requests

1 participant