Merge remote-tracking branch 'origin/dev' into refactor/course-creation-tags

This commit is contained in:
Flavio Mastrangelo 2025-02-25 20:22:26 +01:00
commit 60ae7706bd
2 changed files with 56 additions and 48 deletions

View file

@ -16,7 +16,7 @@ We prioritize issues depending on the most requested features from our users, pl
[🚢 LearnHouse General Roadmap](https://www.learnhouse.app/roadmap) [🚢 LearnHouse General Roadmap](https://www.learnhouse.app/roadmap)
[👨‍💻 Detailed Roadmap](https://github.com/orgs/learnhouse/projects/4/views/1) [👨‍💻 Detailed Roadmap](https://github.com/orgs/learnhouse/projects/4)
## Overview ## Overview

View file

@ -1,4 +1,4 @@
import React, { useEffect } from 'react' import React from 'react'
import { getUriWithOrg } from '@services/config/config' import { getUriWithOrg } from '@services/config/config'
import { useParams } from 'next/navigation' import { useParams } from 'next/navigation'
import { getUserAvatarMediaDirectory } from '@services/media/media' import { getUserAvatarMediaDirectory } from '@services/media/media'
@ -8,12 +8,7 @@ type UserAvatarProps = {
width?: number width?: number
avatar_url?: string avatar_url?: string
use_with_session?: boolean use_with_session?: boolean
rounded?: rounded?: 'rounded-md' | 'rounded-xl' | 'rounded-lg' | 'rounded-full' | 'rounded'
| 'rounded-md'
| 'rounded-xl'
| 'rounded-lg'
| 'rounded-full'
| 'rounded'
border?: 'border-2' | 'border-4' | 'border-8' border?: 'border-2' | 'border-4' | 'border-8'
borderColor?: string borderColor?: string
predefined_avatar?: 'ai' | 'empty' predefined_avatar?: 'ai' | 'empty'
@ -23,59 +18,72 @@ function UserAvatar(props: UserAvatarProps) {
const session = useLHSession() as any const session = useLHSession() as any
const params = useParams() as any const params = useParams() as any
function checkUrlProtocol(url: string): boolean { const isExternalUrl = (url: string): boolean => {
return url.startsWith('https://') || url.startsWith('http://'); return url.startsWith('http://') || url.startsWith('https://')
} }
const predefinedAvatarFunc = () => { const extractExternalUrl = (url: string): string | null => {
if (props.predefined_avatar === 'ai') { // Check if the URL contains an embedded external URL
return getUriWithOrg(params.orgslug, '/ai_avatar.png') const matches = url.match(/avatars\/(https?:\/\/[^/]+.*$)/)
} if (matches && matches[1]) {
if (props.predefined_avatar === 'empty') { return matches[1]
return getUriWithOrg(params.orgslug, '/empty_avatar.png')
} }
return null return null
} }
const predefinedAvatar = predefinedAvatarFunc() const getAvatarUrl = (): string => {
const emptyAvatar = getUriWithOrg(params.orgslug, '/empty_avatar.png') as any // If predefined avatar is specified
const uploadedAvatar = (session.status == 'authenticated') && (checkUrlProtocol(session?.data?.user?.avatar_image)) ? session?.data?.user?.avatar_image : getUserAvatarMediaDirectory(
session?.data?.user?.user_uuid,
session?.data?.user?.avatar_image
)
const useAvatar = () => {
if (props.predefined_avatar) { if (props.predefined_avatar) {
return predefinedAvatar const avatarType = props.predefined_avatar === 'ai' ? 'ai_avatar.png' : 'empty_avatar.png'
} else { return getUriWithOrg(params.orgslug, `/${avatarType}`)
}
// If avatar_url prop is provided
if (props.avatar_url) { if (props.avatar_url) {
// Check if it's a malformed URL (external URL processed through getUserAvatarMediaDirectory)
const extractedUrl = extractExternalUrl(props.avatar_url)
if (extractedUrl) {
return extractedUrl
}
// If it's a direct external URL
if (isExternalUrl(props.avatar_url)) {
return props.avatar_url return props.avatar_url
} else { }
// Otherwise use as is
return props.avatar_url
}
// If user has an avatar in session
if (session?.data?.user?.avatar_image) { if (session?.data?.user?.avatar_image) {
return uploadedAvatar const avatarUrl = session.data.user.avatar_image
} else { // If it's an external URL (e.g., from Google, Facebook, etc.), use it directly
return emptyAvatar if (isExternalUrl(avatarUrl)) {
} return avatarUrl
}
} }
// Otherwise, get the local avatar URL
return getUserAvatarMediaDirectory(session.data.user.user_uuid, avatarUrl)
} }
useEffect(() => { // Fallback to empty avatar
return getUriWithOrg(params.orgslug, '/empty_avatar.png')
}
}, [session.status])
return ( return (
<img <img
alt="User Avatar" alt="User Avatar"
width={props.width ? props.width : 50} width={props.width ?? 50}
height={props.width ? props.width : 50} height={props.width ?? 50}
src={useAvatar()} src={getAvatarUrl()}
className={`${props.avatar_url && session?.data?.user?.avatar_image ? '' : 'bg-gray-700' className={`
} ${props.border ? 'border ' + props.border : ''} ${props.borderColor ? props.borderColor : 'border-white' ${props.avatar_url && session?.data?.user?.avatar_image ? '' : 'bg-gray-700'}
} shadow-xl aspect-square w-[${props.width ? props.width : 50}px] h-[${props.width ? props.width : 50 ${props.border ? `border ${props.border}` : ''}
}px] ${props.rounded ? props.rounded : 'rounded-xl'}`} ${props.borderColor ?? 'border-white'}
shadow-xl
aspect-square
w-[${props.width ?? 50}px]
h-[${props.width ?? 50}px]
${props.rounded ?? 'rounded-xl'}
`}
/> />
) )
} }