'use client'; import { useState, useMemo } from 'react'; import Link from 'next/link'; import { AitmChangeStory } from '@/utils/aitmChangeStory'; import { parseAitmChangeStoryBlock } from '@/utils/aitmChangeStory'; import EmptyState from '@/components/EmptyState'; interface RepoCodeChangesTabProps { owner: string; name: string; activities: any[]; posts: any[]; } /** Extracts AITM checkpoint-linked stories from posts. */ function extractCheckpointPosts(posts: any[]): { post: any; story: AitmChangeStory; cleanContent: string }[] { const results: { post: any; story: AitmChangeStory; cleanContent: string }[] = []; for (const post of posts) { const { story, cleanContent } = parseAitmChangeStoryBlock(post.content || ''); if (story) { results.push({ post, story, cleanContent }); } } return results; } /** File change type badge */ function ChangeTypeBadge({ type }: { type: 'added' | 'removed' | 'modified' }) { const styles = { added: 'bg-green-500/10 text-green-500 border-green-500/20', removed: 'bg-red-500/10 text-red-500 border-red-500/20', modified: 'bg-yellow-500/10 text-yellow-500 border-yellow-500/20', }; const labels = { added: 'Added', removed: 'Removed', modified: 'Modified' }; return ( {labels[type]} ); } /** A single checkpoint-linked change story card */ function CheckpointCard({ story, cleanContent, post, owner, name, }: { story: AitmChangeStory; cleanContent: string; post: any; owner: string; name: string; }) { const [expanded, setExpanded] = useState(false); const aitmUrl = story.aitmProjectId ? `https://timemachine.gitgram.app/project/${story.aitmProjectId}/checkpoint/${story.checkpointId}` : null; return (
{/* Header */}

{story.title || 'AITM Change Story'}

checkpoint {story.checkpointId.slice(0, 8)} {story.fromCheckpointId && ( <> from {story.fromCheckpointId.slice(0, 8)} )}
{post.user?.username && ( by @{post.user.username} )} {post.createdAt && ( {new Date(post.createdAt).toLocaleDateString()} )}
{/* Diff summary */}

{story.diffAdded}

Added

{story.diffRemoved}

Removed

{story.diffModified}

Modified

{/* Changed files */} {story.changedFiles.length > 0 && (
{expanded && (
{story.changedFiles.map((file, idx) => (
{file}
))}
)}
)} {/* Description and CTA */}
{cleanContent && (

{cleanContent}

)}
{aitmUrl && ( View full diff on AITM )} Create Change Story
); } export default function RepoCodeChangesTab({ owner, name, activities, posts }: RepoCodeChangesTabProps) { const checkpointPosts = useMemo(() => extractCheckpointPosts(posts), [posts]); return (
{/* Checkpoint-linked posts */} {checkpointPosts.length > 0 && (

Checkpoint-Linked Changes

{checkpointPosts.length}
{checkpointPosts.map(({ post, story, cleanContent }) => ( ))}
)} {/* Activity timeline */}

Activity Timeline

New Change Story
{activities.length === 0 && checkpointPosts.length === 0 ? ( ) : activities.length === 0 ? null : (
{/* Timeline line */}
{activities.map((activity, idx) => (
{/* Timeline dot */}
{/* Content */}
{(activity.eventType || 'event').replace(/_/g, ' ')} {new Date(activity.occurredAt || activity.createdAt).toLocaleString()}

{activity.summary || activity.title || activity.description || 'Repository activity captured.'}

{activity.commitSha && (

{activity.commitSha.slice(0, 7)}

)}
))}
)}
); } function ActivityIcon({ eventType }: { eventType?: string }) { const type = (eventType || '').toLowerCase(); if (type.includes('push') || type.includes('commit')) { return ( ); } if (type.includes('pull') || type.includes('pr')) { return ( ); } if (type.includes('issue')) { return ( ); } if (type.includes('release') || type.includes('tag')) { return ( ); } return ( ); }