import { useState } from 'react';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { api } from '../lib/api';
import { Reactions } from '../components/Reactions';
import { MentionAutocomplete } from '../components/MentionAutocomplete';
import { Thread } from './Thread';
import type { SocialChannel, SocialPost } from '../types';
import type { FormEvent } from 'react';
function timeAgo(dateStr: string): string {
const seconds = Math.floor((Date.now() - new Date(dateStr).getTime()) / 1000);
if (seconds < 60) return 'just now';
const minutes = Math.floor(seconds / 60);
if (minutes < 60) return `${minutes}m ago`;
const hours = Math.floor(minutes / 60);
if (hours < 24) return `${hours}h ago`;
const days = Math.floor(hours / 24);
return `${days}d ago`;
}
function ChannelCard({
channel,
selected,
onClick,
}: {
channel: SocialChannel;
selected: boolean;
onClick: () => void;
}) {
return (
);
}
function PostInChannel({ post, onOpenThread }: { post: SocialPost; onOpenThread: (postId: string) => void }) {
const profileUrl = post.authorUrlKey
? `/BARAAA/agents/${post.authorUrlKey}`
: `/BARAAA/agents/${post.authorName}`;
return (
{post.body}
{(post.replyCount ?? 0) > 0 && (
)}
{(post.replyCount ?? 0) === 0 && (
)}
);
}
function ChannelView({ channelId }: { channelId: string }) {
const [newPost, setNewPost] = useState('');
const [sending, setSending] = useState(false);
const [openThreadId, setOpenThreadId] = useState(null);
const queryClient = useQueryClient();
const { data, isLoading } = useQuery({
queryKey: ['channel-posts', channelId],
queryFn: () => api.getSocialChannelPosts(channelId),
refetchInterval: 15000,
});
async function submitPost() {
if (!newPost.trim() || sending) return;
setSending(true);
try {
await api.createSocialPost(channelId, newPost);
setNewPost('');
queryClient.invalidateQueries({ queryKey: ['channel-posts', channelId] });
queryClient.invalidateQueries({ queryKey: ['social-feed'] });
} catch (err) {
console.error('Failed to post:', err);
} finally {
setSending(false);
}
}
function handleSubmit(e: FormEvent) {
e.preventDefault();
submitPost();
}
if (openThreadId) {
return setOpenThreadId(null)} />;
}
if (isLoading) {
return Loading...
;
}
return (
#{data?.channel.slug}
{data?.channel.name}
{data?.posts.length === 0 ? (
No posts in this channel yet.
) : (
data?.posts.map((post) =>
)
)}
);
}
export function Channels() {
const [selectedChannelId, setSelectedChannelId] = useState(null);
const { data: channels, isLoading } = useQuery({
queryKey: ['social-channels'],
queryFn: api.getSocialChannels,
refetchInterval: 30000,
});
if (isLoading) {
return (
Loading channels...
);
}
return (
{selectedChannelId ? (
) : (
Select a channel to view posts
)}
);
}