Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

fix placeholder avatar

Steven Tey79230c4886f3
diff --git a/apps/web/lib/analytics/get-events.ts b/apps/web/lib/analytics/get-events.ts
index 74ea174aae..d3404e10df 100644
--- a/apps/web/lib/analytics/get-events.ts
+++ b/apps/web/lib/analytics/get-events.ts
@@ -122,7 +122,7 @@ export const getEvents = async (params: EventsFilters) => {
id: evt.customer_id,
name: "Deleted Customer",
- avatar: `https://api.dicebear.com/7.x/micah/svg?seed=${evt.customer_id}`,
+ avatar: `https://api.dicebear.com/9.x/micah/svg?seed=${evt.customer_id}`,
},
...(evt.event === "sale"
? {
diff --git a/apps/web/lib/webhook/transform.ts b/apps/web/lib/webhook/transform.ts
index 00682d3cd2..0713b26a17 100644
--- a/apps/web/lib/webhook/transform.ts
+++ b/apps/web/lib/webhook/transform.ts
@@ -59,7 +59,7 @@ export const transformLeadEventData = (data: any) => {
email: lead.customerEmail,
avatar:
lead.customerAvatar ||
- `https://api.dicebear.com/7.x/micah/svg?seed=${lead.customerId}`,
+ `https://api.dicebear.com/9.x/micah/svg?seed=${lead.customerId}`,
},
click: {
...lead,
diff --git a/packages/ui/src/avatar.tsx b/packages/ui/src/avatar.tsx
index 0186649b0d..c8a6bfae9d 100644
--- a/packages/ui/src/avatar.tsx
+++ b/packages/ui/src/avatar.tsx
@@ -1,16 +1,33 @@
import { cn } from "@dub/utils";
-import { useUserAvatarUrl } from "./hooks";
+import { sha256 } from "js-sha256";
+import { useState } from "react";
+
+type User = {
+ id?: string | null | undefined;
+ name?: string | null | undefined;
+ email?: string | null | undefined;
+ image?: string | null | undefined;
+};
+
+export function getUserAvatarUrl(user?: User | null) {
+ if (user?.image) return user.image;
+
+ if (!user?.id) return "https://api.dicebear.com/9.x/micah/svg";
+
+ const dicebear = `https://api.dicebear.com/9.x/micah/svg?seed=${encodeURIComponent(user.id)}`;
+ // Gravatar default doesn't support SVG or query params, so we use PNG and encoded / params
+ const encodedDicebear = `https://api.dicebear.com/9.x/micah/png/${encodeURIComponent(`seed=${encodeURIComponent(user.id)}`)}`;
+
+ return user.email
+ ? `https://www.gravatar.com/avatar/${sha256(user.email)}?d=${encodeURIComponent(encodedDicebear)}`
+ : dicebear;
+}
export function Avatar({
user = {},
className,
}: {
- user?: {
- id?: string | null | undefined;
- name?: string | null | undefined;
- email?: string | null | undefined;
- image?: string | null | undefined;
- };
+ user?: User;
className?: string;
}) {
if (!user) {
@@ -24,7 +41,7 @@ export function Avatar({
);
}
- const url = useUserAvatarUrl(user);
+ const [url, setUrl] = useState(getUserAvatarUrl(user));
return (
<img
@@ -33,6 +50,13 @@ export function Avatar({
src={url}
className={cn("h-10 w-10 rounded-full border border-gray-300", className)}
draggable={false}
+ onError={() => {
+ setUrl(
+ `https://api.dicebear.com/9.x/micah/svg?seed=${encodeURIComponent(
+ user.id || "",
+ )}`,
+ );
+ }}
/>
);
}
diff --git a/packages/ui/src/hooks/index.ts b/packages/ui/src/hooks/index.ts
index 49487490fb..82924adba7 100644
--- a/packages/ui/src/hooks/index.ts
+++ b/packages/ui/src/hooks/index.ts
@@ -14,4 +14,3 @@ export * from "./use-router-stuff";
export * from "./use-scroll";
export * from "./use-scroll-progress";
export * from "./use-toast-with-undo";
-export * from "./use-user-avatar-url";
diff --git a/packages/ui/src/hooks/use-user-avatar-url.ts b/packages/ui/src/hooks/use-user-avatar-url.ts
deleted file mode 100644
index 25e1eb0e21..0000000000
--- a/packages/ui/src/hooks/use-user-avatar-url.ts
+++ /dev/null
@@ -1,25 +0,0 @@
-import { sha256 } from "js-sha256";
-import { useMemo } from "react";
-
-type User = {
- id?: string | null | undefined;
- email?: string | null | undefined;
- image?: string | null | undefined;
-};
-
-export function useUserAvatarUrl(user: User) {
- const url = useMemo(() => getUserAvatarUrl(user), [user]);
- return url;
-}
-
-export function getUserAvatarUrl(user?: User | null) {
- if (!user) return "https://api.dicebear.com/7.x/micah/svg";
- if (user.image) return user.image;
-
- // Gravatar default doesn't support SVG or query params, so we use PNG and encoded / params
- const dicebear = `https://api.dicebear.com/7.x/micah/png/${encodeURIComponent(`seed=${encodeURIComponent(user.email || user.id || "")}`)}`;
-
- return user.email
- ? `https://www.gravatar.com/avatar/${sha256(user.email)}?d=${encodeURIComponent(dicebear)}`
- : dicebear;
-}