railyard/apps/ritgram/web/app/[owner]/ProfileTabs.tsx
caelum0x 0ec2f66158 fix(ritgram): stop escaping post-content slashes, linkify URLs, fix profile Posts-tab 404
- postValidator: drop .escape() from createPost's content rule (was turning
  every literal "/" in a URL into /, so link detection and link previews
  silently never fired on any post containing a link). React already escapes
  rendered text; PostCard never uses dangerouslySetInnerHTML for post content.
- compose/edit live-preview panes DO use dangerouslySetInnerHTML for their
  markdown preview and relied on the backend .escape() as their only XSS
  guard -- added an explicit &/</> escape before any markdown transform so
  removing .escape() doesn't open that up.
- linkify.tsx: linkifyText only matched @mention/#hashtag; added a URL pass
  (same regex as extractUrls.ts) so bare https:// URLs render as a real
  <a target="_blank">, with mention/hashtag matching skipping any range a URL
  already claimed.
- Profile Posts tab called a route that doesn't exist
  (GET /users/:username/posts, 404 on every profile). Added real
  username/userId filtering to postController.getPosts (it previously ignored
  both query params entirely) and pointed both frontend callers
  (profileApi.ts's fetchUserPosts, ProfileTabs.tsx's dead-code twin) at
  GET /v1/posts?username=. Switched ProfileTabs' infinite-scroll fetcher from
  a cursor param the backend never returned to real page-based pagination.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 00:04:58 +03:00

321 lines
8.7 KiB
TypeScript

'use client'
import { useRef, useCallback, useEffect, useState } from 'react'
import { useInfiniteQuery, useQuery } from '@tanstack/react-query'
import api from '@/services/api'
import PostCard from '@/components/PostCard'
import GitHubProfileCard from '@/components/GitHubProfileCard'
import RepositorySocialCard from '@/components/RepositorySocialCard'
// ---- Types ------------------------------------------------------------------
interface Post {
id: string
content?: string
mediaUrl?: string
mediaUrls?: string[]
gifUrl?: string
mediaType?: string
likesCount: number
commentsCount: number
isLiked: boolean
createdAt: string
user: { id: string; username: string; avatar?: string }
}
interface PostsPage {
posts: Post[]
nextPage: number | null
}
interface UserRepo {
id: string
owner: string
name: string
fullName: string
description?: string
stars: number
forks: number
language?: string
topics?: string[]
isFollowed?: boolean
followersCount?: number
}
interface GitHubProfile {
login: string
name?: string
bio?: string
avatarUrl?: string
followers?: number
following?: number
publicRepos?: number
company?: string
location?: string
websiteUrl?: string
}
export type TabId = 'posts' | 'projects' | 'github'
// ---- API fetchers -----------------------------------------------------------
async function fetchUserPostsPage(username: string, page = 1): Promise<PostsPage> {
// There is no `/users/:username/posts` route — the real backend route is
// the plain posts list, filtered by `username` (`GET /v1/posts`), which
// paginates by page/limit rather than a cursor.
const { data } = await api.get('/v1/posts', { params: { username, page, limit: 20 } })
const hasMore = Boolean(data.pagination?.hasMore)
return { posts: data.posts ?? [], nextPage: hasMore ? page + 1 : null }
}
async function fetchUserRepositories(username: string): Promise<UserRepo[]> {
const { data } = await api.get(`/v1/users/${username}/repositories`)
return data.repositories ?? data ?? []
}
async function fetchGitHubProfile(username: string): Promise<GitHubProfile> {
const { data } = await api.get(`/v1/users/${username}/github-profile`)
return data
}
// ---- Tab content components -------------------------------------------------
function PostsTab({ username }: { username: string }) {
const sentinelRef = useRef<HTMLDivElement>(null)
const { data, fetchNextPage, hasNextPage, isFetching, isFetchingNextPage, status } =
useInfiniteQuery({
queryKey: ['user-posts', username],
queryFn: ({ pageParam }) => fetchUserPostsPage(username, pageParam as number),
initialPageParam: 1,
getNextPageParam: (last) => last.nextPage ?? undefined,
})
const posts = data?.pages.flatMap((p) => p.posts) ?? []
const onIntersect = useCallback(
(entries: IntersectionObserverEntry[]) => {
if (entries[0].isIntersecting && hasNextPage && !isFetching) fetchNextPage()
},
[fetchNextPage, hasNextPage, isFetching]
)
useEffect(() => {
const el = sentinelRef.current
if (!el) return
const observer = new IntersectionObserver(onIntersect, { rootMargin: '300px' })
observer.observe(el)
return () => observer.disconnect()
}, [onIntersect])
if (status === 'pending') {
return (
<div className="space-y-4">
{Array.from({ length: 3 }).map((_, i) => (
<div key={i} className="h-32 bg-muted animate-pulse rounded-lg" />
))}
</div>
)
}
if (status === 'error') {
return <p className="text-center text-destructive py-12">Failed to load posts.</p>
}
if (posts.length === 0) {
return <p className="text-center text-muted-foreground py-16">No posts yet.</p>
}
return (
<div className="space-y-4">
{posts.map((post) => (
<PostCard
key={post.id}
post={{
id: post.id,
content: post.content ?? '',
author: {
username: post.user.username,
displayName: post.user.username,
avatarUrl: post.user.avatar,
},
likes: post.likesCount,
comments: post.commentsCount,
reposts: 0,
createdAt: post.createdAt,
isLiked: post.isLiked,
}}
onLike={() => undefined}
/>
))}
<div ref={sentinelRef} className="h-1" />
{isFetchingNextPage && (
<div className="flex justify-center py-6">
<div className="w-6 h-6 border-2 border-primary border-t-transparent rounded-full animate-spin" />
</div>
)}
</div>
)
}
function ProjectsTab({ username }: { username: string }) {
const { data, status } = useQuery({
queryKey: ['user-repos', username],
queryFn: () => fetchUserRepositories(username),
})
if (status === 'pending') {
return (
<div className="space-y-3">
{Array.from({ length: 4 }).map((_, i) => (
<div key={i} className="h-20 bg-muted animate-pulse rounded-lg" />
))}
</div>
)
}
if (status === 'error') {
return (
<p className="text-center text-destructive py-12">Failed to load repositories.</p>
)
}
const repos = data ?? []
if (repos.length === 0) {
return <p className="text-center text-muted-foreground py-16">No repositories yet.</p>
}
return (
<div className="space-y-3">
{repos.map((repo) => (
<RepositorySocialCard
key={repo.id}
id={repo.id}
owner={repo.owner}
name={repo.name}
fullName={repo.fullName}
description={repo.description}
stars={repo.stars}
forks={repo.forks}
language={repo.language}
topics={repo.topics}
isFollowed={repo.isFollowed}
followersCount={repo.followersCount}
/>
))}
</div>
)
}
function GitHubTab({
username,
githubUsername,
}: {
username: string
githubUsername?: string
}) {
const { data, status } = useQuery({
queryKey: ['user-github', username],
queryFn: () => fetchGitHubProfile(username),
enabled: Boolean(githubUsername),
})
if (!githubUsername) {
return (
<div className="text-center py-16">
<p className="text-muted-foreground mb-2">No GitHub account linked.</p>
</div>
)
}
if (status === 'pending') {
return (
<div className="flex justify-center pt-8">
<GitHubProfileCard login={githubUsername} isLoading />
</div>
)
}
if (status === 'error') {
return (
<p className="text-center text-destructive py-12">Failed to load GitHub profile.</p>
)
}
return (
<div className="flex justify-center pt-8">
<GitHubProfileCard
login={data?.login ?? githubUsername}
name={data?.name}
bio={data?.bio}
avatarUrl={data?.avatarUrl}
followers={data?.followers}
following={data?.following}
publicRepos={data?.publicRepos}
company={data?.company}
location={data?.location}
websiteUrl={data?.websiteUrl}
/>
</div>
)
}
// ---- Main export ------------------------------------------------------------
interface ProfileTabsProps {
username: string
githubUsername?: string
initialTab?: TabId
}
const TABS: { id: TabId; label: string }[] = [
{ id: 'posts', label: 'Posts' },
{ id: 'projects', label: 'Projects' },
{ id: 'github', label: 'GitHub' },
]
export default function ProfileTabs({
username,
githubUsername,
initialTab = 'posts',
}: ProfileTabsProps) {
const [activeTab, setActiveTab] = useState<TabId>(initialTab)
return (
<>
{/* Tab bar */}
<div className="border-b border-border bg-card sticky top-0 z-10">
<div className="max-w-4xl mx-auto px-4">
<nav className="flex" aria-label="Profile sections" role="tablist">
{TABS.map((tab) => (
<button
key={tab.id}
role="tab"
aria-selected={activeTab === tab.id}
onClick={() => setActiveTab(tab.id)}
className={`px-6 py-3 text-sm font-medium border-b-2 transition-colors ${
activeTab === tab.id
? 'border-primary text-primary'
: 'border-transparent text-muted-foreground hover:text-foreground hover:border-border'
}`}
>
{tab.label}
</button>
))}
</nav>
</div>
</div>
{/* Tab content */}
<div className="max-w-4xl mx-auto px-4 py-6">
{activeTab === 'posts' && <PostsTab username={username} />}
{activeTab === 'projects' && <ProjectsTab username={username} />}
{activeTab === 'github' && (
<GitHubTab username={username} githubUsername={githubUsername} />
)}
</div>
</>
)
}