import { useEffect, useState } from 'react'; import { httpGet } from '../../lib/http'; import { pageProgressMessage } from '../../stores/page'; import { getUrlParams } from '../../lib/browser'; import { useToast } from '../../hooks/use-toast'; import type { TeamMemberDocument } from '../TeamMembers/TeamMembersPage'; import type { UserProgress } from '../TeamProgress/TeamProgressPage'; import type { TeamActivityStreamDocument } from '../TeamActivity/TeamActivityPage'; import { ResourceProgress } from '../Activity/ResourceProgress'; import { ActivityStream } from '../Activity/ActivityStream'; import { MemberRoleBadge } from '../TeamMembers/RoleBadge'; import { TeamMemberEmptyPage } from './TeamMemberEmptyPage'; import { Pagination } from '../Pagination/Pagination'; import type { ResourceType } from '../../lib/resource-progress'; import { MemberProgressModal } from '../TeamProgress/MemberProgressModal'; import { useStore } from '@nanostores/react'; import { $currentTeam } from '../../stores/team'; import { MemberCustomProgressModal } from '../TeamProgress/MemberCustomProgressModal'; type GetTeamMemberProgressesResponse = TeamMemberDocument & { name: string; avatar: string; email: string; progresses: UserProgress[]; }; type GetTeamMemberActivityResponse = { data: TeamActivityStreamDocument[]; totalCount: number; totalPages: number; currPage: number; perPage: number; }; export function TeamMemberDetailsPage() { const { t: teamId, m: memberId } = getUrlParams() as { t: string; m: string }; const toast = useToast(); const currentTeam = useStore($currentTeam); const [memberProgress, setMemberProgress] = useState(null); const [memberActivity, setMemberActivity] = useState(null); const [currPage, setCurrPage] = useState(1); const [selectedResource, setSelectedResource] = useState<{ resourceId: string; resourceType: ResourceType; isCustomResource?: boolean; } | null>(null); const loadMemberProgress = async () => { const { response, error } = await httpGet( `${import.meta.env.PUBLIC_API_URL}/v1-get-team-member-progresses/${teamId}/${memberId}`, ); if (error || !response) { pageProgressMessage.set(''); toast.error(error?.message || 'Failed to load team member'); return; } setMemberProgress(response); }; const loadMemberActivity = async (currPage: number = 1) => { const { response, error } = await httpGet( `${import.meta.env.PUBLIC_API_URL}/v1-get-team-member-activity/${teamId}/${memberId}`, { currPage, }, ); if (error || !response) { pageProgressMessage.set(''); toast.error(error?.message || 'Failed to load team member activity'); return; } setMemberActivity(response); setCurrPage(response?.currPage || 1); }; useEffect(() => { if (!teamId) { return; } Promise.allSettled([loadMemberProgress(), loadMemberActivity()]).finally( () => { pageProgressMessage.set(''); }, ); }, [teamId]); if (!teamId || !memberId || !memberProgress || !memberActivity) { return null; } const avatarUrl = memberProgress?.avatar ? `${import.meta.env.PUBLIC_AVATAR_BASE_URL}/${memberProgress?.avatar}` : '/images/default-avatar.png'; const ProgressModal = selectedResource && !selectedResource.isCustomResource ? MemberProgressModal : MemberCustomProgressModal; return ( <> {selectedResource && ( setSelectedResource(null)} onShowMyProgress={() => { window.location.href = `/team/member?t=${teamId}&m=${currentTeam?.memberId}`; }} /> )}
{memberProgress?.name}

{memberProgress?.name}

{memberProgress?.email}

{memberProgress?.progresses && memberProgress?.progresses?.length > 0 ? ( <>

Progress Overview

{memberProgress?.progresses?.map((progress) => { const learningCount = progress.learning || 0; const doneCount = progress.done || 0; const totalCount = progress.total || 0; const skippedCount = progress.skipped || 0; return ( totalCount ? totalCount : doneCount} learningCount={ learningCount > totalCount ? totalCount : learningCount } totalCount={totalCount} skippedCount={skippedCount} resourceId={progress.resourceId} resourceType={'roadmap'} updatedAt={progress.updatedAt} title={progress.resourceTitle} roadmapSlug={progress.roadmapSlug} showActions={false} onResourceClick={() => { setSelectedResource({ resourceId: progress.resourceId, resourceType: progress.resourceType, isCustomResource: progress.isCustomResource, }); }} /> ); })}
) : ( )} {memberActivity?.data && memberActivity?.data?.length > 0 ? ( <> act.activity) || [] } onResourceClick={(resourceId, resourceType, isCustomResource) => { setSelectedResource({ resourceId, resourceType, isCustomResource, }); }} /> { pageProgressMessage.set('Loading Activity'); loadMemberActivity(page).finally(() => { pageProgressMessage.set(''); }); }} /> ) : null} ); }