- Added @emoji-mart/data and @emoji-mart/react dependencies for enhanced emoji support. - Replaced static emoji characters with Emoji component in various UI components for consistency and improved rendering. - Updated generateDateTitle function to return an object with emoji and text for better structure. - Marked the task for removing emojis from the UI as complete in TODO.md.
127 lines
2.9 KiB
TypeScript
127 lines
2.9 KiB
TypeScript
import { getGravatarUrl, isGravatarUrl } from './gravatar'
|
|
|
|
export type AvatarType = 'custom' | 'gravatar' | 'default'
|
|
|
|
export interface AvatarConfig {
|
|
type: AvatarType
|
|
url?: string
|
|
email?: string
|
|
size?: number
|
|
}
|
|
|
|
/**
|
|
* Détermine l'URL finale de l'avatar selon la configuration
|
|
* @param config Configuration de l'avatar
|
|
* @returns URL finale de l'avatar ou null
|
|
*/
|
|
export function getAvatarUrl(config: AvatarConfig): string | null {
|
|
const { type, url, email, size = 200 } = config
|
|
|
|
switch (type) {
|
|
case 'custom':
|
|
if (!url) return null
|
|
return url
|
|
|
|
case 'gravatar':
|
|
if (!email) return null
|
|
return getGravatarUrl(email, { size })
|
|
|
|
case 'default':
|
|
default:
|
|
return null
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Détermine le type d'avatar à partir d'une URL existante
|
|
* @param url URL de l'avatar
|
|
* @param email Email de l'utilisateur (pour vérifier si c'est déjà un Gravatar)
|
|
* @returns Type d'avatar détecté
|
|
*/
|
|
export function detectAvatarType(url: string | null | undefined, email?: string): AvatarType {
|
|
if (!url) return 'default'
|
|
|
|
if (isGravatarUrl(url)) {
|
|
return 'gravatar'
|
|
}
|
|
|
|
// Si on a un email et que l'URL correspond à un Gravatar généré pour cet email
|
|
if (email && typeof url === 'string') {
|
|
const expectedGravatarUrl = getGravatarUrl(email)
|
|
if (url.includes(expectedGravatarUrl.split('?')[0].split('avatar/')[1])) {
|
|
return 'gravatar'
|
|
}
|
|
}
|
|
|
|
return 'custom'
|
|
}
|
|
|
|
/**
|
|
* Génère une configuration d'avatar optimisée
|
|
* @param url URL existante de l'avatar
|
|
* @param email Email de l'utilisateur
|
|
* @param size Taille souhaitée
|
|
* @returns Configuration optimisée pour l'avatar
|
|
*/
|
|
export function optimizeAvatarConfig(
|
|
url: string | null | undefined,
|
|
email: string | undefined,
|
|
size: number = 200
|
|
): AvatarConfig {
|
|
const type = detectAvatarType(url, email)
|
|
|
|
switch (type) {
|
|
case 'gravatar':
|
|
return {
|
|
type: 'gravatar',
|
|
email,
|
|
size
|
|
}
|
|
|
|
case 'custom':
|
|
return {
|
|
type: 'custom',
|
|
url: url!,
|
|
size
|
|
}
|
|
|
|
case 'default':
|
|
default:
|
|
return {
|
|
type: 'default',
|
|
size
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Valide une URL d'avatar personnalisée
|
|
* @param url URL à valider
|
|
* @returns true si valide, false sinon
|
|
*/
|
|
export function validateCustomAvatarUrl(url: string): boolean {
|
|
try {
|
|
const parsedUrl = new URL(url)
|
|
|
|
// Vérifier le protocole (seulement HTTPS pour la sécurité)
|
|
if (parsedUrl.protocol !== 'https:') {
|
|
return false
|
|
}
|
|
|
|
// Vérifier que c'est bien une URL d'image
|
|
const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.svg']
|
|
const hasImageExtension = imageExtensions.some(ext =>
|
|
parsedUrl.pathname.toLowerCase().endsWith(ext)
|
|
)
|
|
|
|
if (!hasImageExtension && parsedUrl.pathname.includes('.')) {
|
|
return false
|
|
}
|
|
|
|
return true
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|
|
|