Skip to content

Commit 69c61d9

Browse files
authored
Merge pull request #416 from extolkom/feat/405-creator-profile-gradient-fallback
feat: add deterministic gradient fallback for creator avatars
2 parents 90abcf3 + 25147f7 commit 69c61d9

2 files changed

Lines changed: 27 additions & 5 deletions

File tree

src/components/common/__tests__/CreatorInitialsAvatar.test.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { render, screen } from '@testing-library/react';
22
import { describe, expect, it } from 'vitest';
33
import CreatorInitialsAvatar from '@/components/common/CreatorInitialsAvatar';
4-
import { getFallbackAvatarColors } from '@/utils/avatarColor.util';
4+
import {
5+
getCreatorGradientFallback,
6+
getFallbackAvatarColors,
7+
} from '@/utils/avatarColor.util';
58

69
describe('CreatorInitialsAvatar', () => {
710
it('uses deterministic fallback colors for the same creator id', () => {
@@ -14,6 +17,16 @@ describe('CreatorInitialsAvatar', () => {
1417
expect(first.textColor).toBe('rgba(255, 255, 255, 0.95)');
1518
});
1619

20+
it('uses the fallback gradient helper deterministically for handles or ids', () => {
21+
const first = getCreatorGradientFallback('creator-123');
22+
const second = getCreatorGradientFallback('creator-123');
23+
const third = getCreatorGradientFallback('creator-456');
24+
25+
expect(first).toEqual(second);
26+
expect(first).not.toBe(third);
27+
expect(first).toContain('linear-gradient');
28+
});
29+
1730
it('renders initials fallback with hashed background when image is missing', () => {
1831
render(<CreatorInitialsAvatar name="Alex Rivers" creatorId="creator-123" />);
1932

src/utils/avatarColor.util.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,23 @@ const stringHash = (value: string) => {
66
return hash;
77
};
88

9-
export const getFallbackAvatarColors = (creatorId?: string | number | null) => {
10-
const normalizedId = String(creatorId ?? '').trim();
11-
const hash = stringHash(normalizedId || 'fallback-avatar');
9+
export const getCreatorGradientFallback = (
10+
identifier?: string | number | null
11+
) => {
12+
const normalizedIdentifier = String(identifier ?? 'creator-fallback').trim() || 'creator-fallback';
13+
const hash = stringHash(normalizedIdentifier);
1214
const baseHue = hash % 360;
1315
const accentHue = (baseHue + 28) % 360;
1416

17+
// Use darker mid-lightness HSL values so white text remains readable over
18+
// the gradient. The chosen lightness range keeps contrast high enough for
19+
// WCAG AA against white overlay text.
20+
return `linear-gradient(135deg, hsl(${baseHue} 65% 28%), hsl(${accentHue} 72% 36%))`;
21+
};
22+
23+
export const getFallbackAvatarColors = (creatorId?: string | number | null) => {
1524
return {
16-
background: `linear-gradient(135deg, hsl(${baseHue} 65% 28%), hsl(${accentHue} 72% 36%))`,
25+
background: getCreatorGradientFallback(creatorId),
1726
textColor: 'rgba(255, 255, 255, 0.95)',
1827
};
1928
};

0 commit comments

Comments
 (0)