Skip to content

Commit

Permalink
fix: syntax error in demo GA script & hydration warning (#1333)
Browse files Browse the repository at this point in the history
* fix: quote GA ID & fix UserAvatar hydration warning

* chore: remove "use client"
  • Loading branch information
jakehobbs authored Feb 7, 2025
1 parent f486487 commit df1d99a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/ui-demo/src/components/shared/GoogleAnalytics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const GoogleAnalytics: FC = () => (
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', ${GA_ID});`,
gtag('config', '${GA_ID}');`,
}}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,30 @@ const SUB_COLORS = ["#37BCFA", "#6D37FA"];

const UserAvatar = ({ address, primaryColor }: UserAvatarProps) => {
const avatarColors = useMemo(() => {
return shuffleColors([primaryColor, ...SUB_COLORS]);
}, [primaryColor]);
return shuffleColorsDeterministically(
[primaryColor, ...SUB_COLORS],
address
);
}, [address, primaryColor]);

return (
<Avatar
size={40}
name={address}
variant="marble"
colors={avatarColors.length > 0 ? avatarColors : [primaryColor]}
/>
<Avatar size={40} name={address} variant="marble" colors={avatarColors} />
);
};

const shuffleColors = (colors: string[]) => {
return colors.sort(() => Math.random() - 0.5);
// Always returns the same colors for the same seed, so
// that rendering on the server and client will match.
const shuffleColorsDeterministically = (colors: string[], seed: string) => {
if (colors.length === 1) {
return colors;
}
const hash = Array.from(seed).reduce(
(acc, char) => acc + char.charCodeAt(0),
0
);
return [...colors].sort(
(a, b) => (hash % a.charCodeAt(0)) - (hash % b.charCodeAt(0))
);
};

export { UserAvatar };

0 comments on commit df1d99a

Please sign in to comment.