import { useState } from 'react';
import { getRelativeTimeString } from '../../lib/date';
import type { TeamStreamActivity } from './TeamActivityPage';
import { ChevronsDown, ChevronsUp } from 'lucide-react';
import { ActivityTopicTitles } from '../Activity/ActivityTopicTitles';
import { cn } from '../../lib/classname';
import { useStore } from '@nanostores/react';
import { $currentTeam } from '../../stores/team';
type TeamActivityItemProps = {
onTopicClick?: (activity: TeamStreamActivity) => void;
teamId: string;
user: {
activities: TeamStreamActivity[];
_id: string;
name: string;
avatar?: string | undefined;
username?: string | undefined;
memberId?: string;
};
};
export function TeamActivityItem(props: TeamActivityItemProps) {
const { user, onTopicClick, teamId } = props;
const { activities } = user;
const currentTeam = useStore($currentTeam);
const [showAll, setShowAll] = useState(false);
const resourceLink = (activity: TeamStreamActivity) => {
const {
resourceId,
resourceTitle,
resourceType,
isCustomResource,
resourceSlug,
} = activity;
const resourceUrl =
resourceType === 'question'
? `/questions/${resourceId}`
: resourceType === 'best-practice'
? `/best-practices/${resourceId}`
: isCustomResource && resourceType === 'roadmap'
? `/r/${resourceSlug}`
: `/${resourceId}`;
return (
{resourceTitle}
);
};
const timeAgo = (date: string | Date) => (
{getRelativeTimeString(new Date(date).toISOString(), true)}
);
const userAvatar = user.avatar
? `${import.meta.env.PUBLIC_AVATAR_BASE_URL}/${user.avatar}`
: '/images/default-avatar.png';
const isPersonalProgressOnly =
currentTeam?.personalProgressOnly &&
currentTeam.role === 'member' &&
user.memberId !== currentTeam.memberId;
const username = (
{
if (isPersonalProgressOnly) {
e.preventDefault();
}
}}
aria-disabled={isPersonalProgressOnly}
>
{user?.name || 'Unknown'}
);
if (activities.length === 1) {
const activity = activities[0];
const { actionType, topicTitles } = activity;
const topicCount = topicTitles?.length || 0;
return (
{username} started {topicCount} topic{topicCount > 1 ? 's' : ''} in {resourceLink(activity)} {timeAgo(activity.updatedAt)}
{username} completed {topicCount} topic{topicCount > 1 ? 's' : ''} in {resourceLink(activity)} {timeAgo(activity.updatedAt)}
{username} answered {topicCount} question{topicCount > 1 ? 's' : ''} in {resourceLink(activity)} {timeAgo(activity.updatedAt)}
Started {topicCount} topic {topicCount > 1 ? 's' : ''} in {resourceLink(activity)} {timeAgo(activity.updatedAt)}
Completed {topicCount} topic {topicCount > 1 ? 's' : ''} in {resourceLink(activity)} {timeAgo(activity.updatedAt)}
Answered {topicCount} question {topicCount > 1 ? 's' : ''} in {resourceLink(activity)} {timeAgo(activity.updatedAt)}