import React, { useState, useEffect } from 'react' import RecordList from './RecordList.jsx' import ChatRecordList from './ChatRecordList.jsx' import ProfileRecordList from './ProfileRecordList.jsx' import LoadingSkeleton from './LoadingSkeleton.jsx' import { logger } from '../utils/logger.js' import { collections } from '../api/atproto.js' import { getApiConfig } from '../utils/pds.js' import { env } from '../config/env.js' export default function RecordTabs({ langRecords, commentRecords, userComments, chatRecords, chatHasMore, onLoadMoreChat, userChatRecords, userChatLoading, baseRecords, apiConfig, pageContext, user = null, agent = null, onRecordDeleted = null }) { // State for page-specific chat records const [pageSpecificChatRecords, setPageSpecificChatRecords] = useState([]) const [pageSpecificLoading, setPageSpecificLoading] = useState(false) // Check if current page has matching chat records (AI posts always have chat records) const isAiPost = !pageContext.isTopPage && Array.isArray(chatRecords) && chatRecords.some(chatPair => { const recordUrl = chatPair.question?.value?.post?.url if (!recordUrl) return false try { const recordRkey = new URL(recordUrl).pathname.split('/').pop()?.replace(/\.html$/, '') return recordRkey === pageContext.rkey } catch { return false } }) const [activeTab, setActiveTab] = useState(isAiPost ? 'collection' : 'profiles') // Fetch page-specific chat records for individual article pages useEffect(() => { if (!pageContext.isTopPage && pageContext.rkey) { const fetchPageSpecificChats = async () => { setPageSpecificLoading(true) try { const apiConfig = getApiConfig(`https://${env.pds}`) const { atproto } = await import('../api/atproto.js') const did = await atproto.getDid(env.pds, env.admin) const records = await collections.getChatForPost( apiConfig.pds, did, env.collection, pageContext.rkey ) setPageSpecificChatRecords(records) } catch (error) { setPageSpecificChatRecords([]) } finally { setPageSpecificLoading(false) } } fetchPageSpecificChats() } else { setPageSpecificChatRecords([]) } }, [pageContext.isTopPage, pageContext.rkey]) // Filter records based on page context const filterRecords = (records, isProfile = false) => { const recordsArray = Array.isArray(records) ? records : [] if (pageContext.isTopPage) { // Top page: show latest 3 records return recordsArray.slice(0, 3) } else { // Individual page: show records matching the URL const filtered = recordsArray.filter(record => { // Profile records should always be shown if (isProfile || record.value?.type === 'profile') { return true } const recordUrl = record.value?.post?.url if (!recordUrl) { return false } try { const recordRkey = new URL(recordUrl).pathname.split('/').pop()?.replace(/\.html$/, '') return recordRkey === pageContext.rkey } catch { return false } }) return filtered } } // Filter chat records (which are already processed into pairs) const filterChatRecords = (chatPairs) => { const chatArray = Array.isArray(chatPairs) ? chatPairs : [] if (pageContext.isTopPage) { // Top page: show latest 3 pairs return chatArray.slice(0, 3) } else { // Individual page: show pairs matching the URL (compare path only, ignore domain) const filtered = chatArray.filter(chatPair => { const recordUrl = chatPair.question?.value?.post?.url if (!recordUrl) { return false } try { // Extract path from URL and get the filename part const recordPath = new URL(recordUrl).pathname const recordRkey = recordPath.split('/').pop()?.replace(/\.html$/, '') // Compare with current page rkey return recordRkey === pageContext.rkey } catch (error) { return false } }) return filtered } } // Apply filters to all record types const filteredLangRecords = filterRecords(Array.isArray(langRecords) ? langRecords : []) const filteredCommentRecords = filterRecords(Array.isArray(commentRecords) ? commentRecords : []) const filteredUserComments = filterRecords(Array.isArray(userComments) ? userComments : []) const filteredChatRecords = filterChatRecords(Array.isArray(chatRecords) ? chatRecords : []) const filteredBaseRecords = filterRecords(Array.isArray(baseRecords) ? baseRecords : []) // Filter profile records from baseRecords const profileRecords = (Array.isArray(baseRecords) ? baseRecords : []).filter(record => record.value?.type === 'profile') const sortedProfileRecords = profileRecords.sort((a, b) => { if (a.value.profileType === 'admin' && b.value.profileType !== 'admin') return -1 if (a.value.profileType !== 'admin' && b.value.profileType === 'admin') return 1 return 0 }) const filteredProfileRecords = filterRecords(sortedProfileRecords, true) return (