Skip to content

Commit 4b4dc9d

Browse files
committed
Refactor Avatar component to remove deprecation warning
1 parent 07b2df4 commit 4b4dc9d

2 files changed

Lines changed: 22 additions & 38 deletions

File tree

‎src/components/Avatar/avatars.ts‎

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
* the import.meta.glob operation only runs once during the module initialization.
77
* @example
88
* ```typescript
9-
* import { AvatarManager } from '@components/Avatar/avatars'
9+
* import { AvatarManager, normalizeNameToFilename } from '@components/Avatar/avatars'
1010
*
1111
* const avatar = AvatarManager.getInstance().getAvatar('kevin-brown')
1212
* const allAvatars = AvatarManager.getInstance().getAll()
13+
* const filename = normalizeNameToFilename('Chris Southam') // 'chris-southam'
1314
* ```
1415
*/
1516

@@ -22,6 +23,24 @@ type ImageMetadata = {
2223

2324
type AvatarMap = Record<string, ImageMetadata>
2425

26+
/**
27+
* Normalize a person's name to a filename format
28+
* @param name - The person's name (e.g., "Chris Southam")
29+
* @returns The normalized filename (e.g., "chris-southam")
30+
* @example
31+
* ```typescript
32+
* normalizeNameToFilename('Chris Southam') // 'chris-southam'
33+
* normalizeNameToFilename('Kevin Brown') // 'kevin-brown'
34+
* ```
35+
*/
36+
export function normalizeNameToFilename(name: string): string {
37+
return name
38+
.toLowerCase()
39+
.trim()
40+
.replace(/\s+/g, '-') // Replace spaces with hyphens
41+
.replace(/[^a-z0-9-]/g, '') // Remove non-alphanumeric chars except hyphens
42+
}
43+
2544
class AvatarManagerClass {
2645
private static instance: AvatarManagerClass
2746
private readonly avatarMap: Readonly<AvatarMap>
@@ -111,26 +130,3 @@ class AvatarManagerClass {
111130
// Export singleton instance
112131
export const AvatarManager = AvatarManagerClass
113132

114-
// Backward-compatible exports for existing code
115-
const instance = AvatarManager.getInstance()
116-
117-
/**
118-
* @deprecated Use AvatarManager.getInstance().getAll() instead
119-
*/
120-
export const avatarMap = instance.getAll()
121-
122-
/**
123-
* Get avatar image by filename
124-
* @deprecated Use AvatarManager.getInstance().getAvatar() instead
125-
*/
126-
export function getAvatarImage(filename: string): ImageMetadata | undefined {
127-
return instance.getAvatar(filename)
128-
}
129-
130-
/**
131-
* Get all available avatar filenames
132-
* @deprecated Use AvatarManager.getInstance().getAvailableAvatars() instead
133-
*/
134-
export function getAvailableAvatars(): readonly string[] {
135-
return instance.getAvailableAvatars()
136-
}

‎src/components/Avatar/index.astro‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
1515
import { Image } from 'astro:assets'
16-
import { getAvatarImage } from './avatars.ts'
16+
import { AvatarManager, normalizeNameToFilename } from './avatars.ts'
1717
1818
export interface Props {
1919
/** The person's name (will be normalized for filename lookup) */
@@ -24,22 +24,10 @@ export interface Props {
2424
2525
const { name, class: className = '' } = Astro.props
2626
27-
/**
28-
* Normalize a person's name to a filename format
29-
* "Chris Southam" -> "chris-southam"
30-
*/
31-
const normalizeNameToFilename = (personName: string): string => {
32-
return personName
33-
.toLowerCase()
34-
.trim()
35-
.replace(/\s+/g, '-') // Replace spaces with hyphens
36-
.replace(/[^a-z0-9-]/g, '') // Remove non-alphanumeric chars except hyphens
37-
}
38-
3927
const normalizedFilename = normalizeNameToFilename(name)
4028
4129
// Get the avatar image if it exists
42-
const avatarImage = getAvatarImage(normalizedFilename)
30+
const avatarImage = AvatarManager.getInstance().getAvatar(normalizedFilename)
4331
4432
// If no avatar image and no name for fallback, render nothing
4533
if (!avatarImage && !name) {

0 commit comments

Comments
 (0)