Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

11.4 KB
Raw Blame
import { useState, useEffect, useCallback } from 'react';
import * as api from '../../../services/api';
import BrailleSpinner from '../../BrailleSpinner';
import LoadFailedState from '../../ui/LoadFailedState';
import ListLoadError from '../../ui/ListLoadError';
import { FormField } from '../../ui/FormField';
import { timeAgo, formatDateTime } from '../../../utils/formatters';
import useLatestRequest from '../../../hooks/useLatestRequest.js';

export default function PublishedTab({ agentId }) {
  const [accounts, setAccounts] = useState([]);
  const [selectedAccountId, setSelectedAccountId] = useState('');
  const [published, setPublished] = useState({ posts: [], comments: [] });
  const [publishedLoading, setPublishedLoading] = useState(false);
  const [publishedError, setPublishedError] = useState(false);
  const [publishedDays, setPublishedDays] = useState(7);
  const [loading, setLoading] = useState(true);
  const beginPublishedLoad = useLatestRequest();
  const [accountsError, setAccountsError] = useState(false);

  const fetchAccounts = useCallback(async () => {
    setLoading(true);
    setAccountsError(false);
    await api.getPlatformAccounts(agentId, null, { silent: true })
      .then((data) => {
        const active = data.filter(a => a.status === 'active');
        setAccounts(active);
        // Auto-select first account
        if (active.length > 0) {
          setSelectedAccountId(prev => prev || active[0].id);
        }
      })
      .catch(() => setAccountsError(true))
      .finally(() => setLoading(false));
  }, [agentId]);

  useEffect(() => {
    fetchAccounts();
  }, [fetchAccounts]);

  const loadPublished = useCallback(async () => {
    const isCurrent = beginPublishedLoad();
    if (!agentId || !selectedAccountId) {
      setPublished({ posts: [], comments: [] });
      setPublishedLoading(false);
      return;
    }
    setPublishedLoading(true);
    setPublishedError(false);
    try {
      const data = await api.getAgentPublished({ agentId, accountId: selectedAccountId, days: publishedDays }, { silent: true });
      if (!isCurrent()) return;
      setPublished(data);
    } catch {
      if (!isCurrent()) return;
      setPublishedError(true);
    }
    setPublishedLoading(false);
  }, [agentId, selectedAccountId, publishedDays, beginPublishedLoad]);

  useEffect(() => {
    loadPublished();
  }, [loadPublished]);

  if (loading) {
    return <div className="p-4"><BrailleSpinner text="Loading" /></div>;
  }

  if (accountsError) {
    return (
      <div className="p-4">
        <ListLoadError what="platform accounts" onRetry={fetchAccounts} />
      </div>
    );
  }

  return (
    <div className="p-4">
      {/* Header */}
      <div className="flex flex-wrap items-end gap-4 mb-6 p-4 bg-port-card border border-port-border rounded-lg">
        <FormField label="Account">
          <select
            value={selectedAccountId}
            onChange={(e) => setSelectedAccountId(e.target.value)}
            className="px-3 py-2 bg-port-bg border border-port-border rounded text-white min-w-[200px]"
          >
            <option value="">Select account...</option>
            {accounts.map(account => (
              <option key={account.id} value={account.id}>
                {account.credentials.username}
              </option>
            ))}
          </select>
        </FormField>
        <div className="flex items-center gap-2 ml-auto">
          <select
            aria-label="Time range"
            value={publishedDays}
            onChange={(e) => setPublishedDays(parseInt(e.target.value, 10))}
            className="px-2 py-1 text-sm bg-port-bg border border-port-border rounded text-white"
          >
            <option value={7}>7 days</option>
            <option value={14}>14 days</option>
            <option value={30}>30 days</option>
          </select>
          <button
            onClick={loadPublished}
            disabled={publishedLoading}
            className="px-3 py-1 text-sm bg-port-accent/20 text-port-accent rounded hover:bg-port-accent/30 disabled:opacity-50"
          >
            {publishedLoading ? <BrailleSpinner text="Loading" /> : 'Refresh'}
          </button>
        </div>
      </div>

      {!selectedAccountId && (
        <div className="text-center py-12 text-gray-400">
          <p className="text-lg mb-2">Select an account to view published content</p>
        </div>
      )}

      {selectedAccountId && (
        <div className="bg-port-card border border-port-border rounded-lg p-4">
          {publishedError ? <LoadFailedState title="Could not load published content." onRetry={loadPublished} /> : <>
          <div className="flex items-center gap-3 mb-4">
            <h3 className="font-semibold text-white">Published Content</h3>
            {published.posts.length > 0 && (
              <span className="text-xs px-2 py-0.5 rounded bg-port-accent/20 text-port-accent">
                {published.posts.length} post{published.posts.length !== 1 ? 's' : ''}
              </span>
            )}
            {published.comments.length > 0 && (
              <span className="text-xs px-2 py-0.5 rounded bg-port-success/20 text-port-success">
                {published.comments.length} comment{published.comments.length !== 1 ? 's' : ''}
              </span>
            )}
          </div>

          {publishedLoading && published.posts.length === 0 && published.comments.length === 0 && (
            <p className="text-sm text-gray-500 text-center py-4">Loading published content...</p>
          )}

          {/* Posts Table */}
          {published.posts.length > 0 && (
            <div className="mb-4">
              <h4 className="text-sm font-medium text-gray-400 mb-2">Posts</h4>
              <div className="overflow-x-auto">
                <table className="w-full text-sm">
                  <thead>
                    <tr className="text-left text-gray-500 border-b border-port-border">
                      <th className="pb-2 pr-4">Title</th>
                      <th className="pb-2 pr-4">Submolt</th>
                      <th className="pb-2 pr-4 text-right">Score</th>
                      <th className="pb-2 pr-4 text-right">Comments</th>
                      <th className="pb-2 pr-4">Published</th>
                      <th className="pb-2 w-8"></th>
                    </tr>
                  </thead>
                  <tbody>
                    {published.posts.map(post => (
                      <tr key={post.postId} className="border-b border-port-border/50 hover:bg-port-bg/50">
                        <td className="py-2 pr-4 max-w-[250px]">
                          <span className="text-white truncate block" title={post.title}>
                            {post.title}
                          </span>
                        </td>
                        <td className="py-2 pr-4">
                          <span className="text-port-accent text-xs">/{post.submolt}</span>
                        </td>
                        <td className="py-2 pr-4 text-right">
                          {post.score !== null ? (
                            <span className={post.score > 0 ? 'text-port-success' : 'text-gray-500'}>
                              {post.score}
                            </span>
                          ) : (
                            <span className="text-gray-600">-</span>
                          )}
                        </td>
                        <td className="py-2 pr-4 text-right text-gray-400">
                          {post.commentCount ?? '-'}
                        </td>
                        <td className="py-2 pr-4 text-gray-500 whitespace-nowrap" title={formatDateTime(post.publishedAt)}>
                          {timeAgo(post.publishedAt)}
                        </td>
                        <td className="py-2">
                          {post.url ? (
                            <a
                              href={post.url}
                              target="_blank"
                              rel="noopener noreferrer"
                              className="text-port-accent hover:text-port-accent/80"
                              title="Open on Moltbook"
                            >
                              ↗
                            </a>
                          ) : (
                            <span className="text-gray-600">-</span>
                          )}
                        </td>
                      </tr>
                    ))}
                  </tbody>
                </table>
              </div>
            </div>
          )}

          {/* Comments Table */}
          {published.comments.length > 0 && (
            <div>
              <h4 className="text-sm font-medium text-gray-400 mb-2">Comments</h4>
              <div className="overflow-x-auto">
                <table className="w-full text-sm">
                  <thead>
                    <tr className="text-left text-gray-500 border-b border-port-border">
                      <th className="pb-2 pr-4">Post</th>
                      <th className="pb-2 pr-4">Type</th>
                      <th className="pb-2 pr-4">Published</th>
                      <th className="pb-2 w-8"></th>
                    </tr>
                  </thead>
                  <tbody>
                    {published.comments.map((comment, index) => (
                      <tr key={comment.commentId || index} className="border-b border-port-border/50 hover:bg-port-bg/50">
                        <td className="py-2 pr-4 max-w-[350px]">
                          <span className="text-white truncate block" title={comment.postTitle}>
                            {comment.postTitle}
                          </span>
                        </td>
                        <td className="py-2 pr-4">
                          <span className={`text-xs px-1.5 py-0.5 rounded ${
                            comment.isReply
                              ? 'bg-port-accent-2/20 text-port-accent-2'
                              : 'bg-port-accent/20 text-port-accent'
                          }`}>
                            {comment.isReply ? 'reply' : 'comment'}
                          </span>
                        </td>
                        <td className="py-2 pr-4 text-gray-500 whitespace-nowrap" title={formatDateTime(comment.publishedAt)}>
                          {timeAgo(comment.publishedAt)}
                        </td>
                        <td className="py-2">
                          {comment.url ? (
                            <a
                              href={comment.url}
                              target="_blank"
                              rel="noopener noreferrer"
                              className="text-port-accent hover:text-port-accent/80"
                              title="Open on Moltbook"
                            >
                              ↗
                            </a>
                          ) : (
                            <span className="text-gray-600">-</span>
                          )}
                        </td>
                      </tr>
                    ))}
                  </tbody>
                </table>
              </div>
            </div>
          )}

          {!publishedLoading && published.posts.length === 0 && published.comments.length === 0 && (
            <p className="text-sm text-gray-500 text-center py-4">
              No published content in the last {publishedDays} days
            </p>
          )}
          </>}
        </div>
      )}
    </div>
  );
}