import { useEffect, useState, useCallback } from "react"; import { useChannels } from "../hooks/useChannels"; import { useSocialSets } from "../hooks/useSocialSets"; import { usePlan } from "../hooks/usePlan"; import { wpApi } from "../api/wp-api"; import { toast } from "react-toastify"; import clsx from "clsx"; import ChannelCard from "../components/ChannelCard"; import QuotaBar from "../components/QuotaBar"; import PlatformGrid from "../components/PlatformGrid"; type SubTab = "connect" | "manage"; export default function SocialProfiles() { const { channels, isLoading: channelsLoading, refresh } = useChannels(); const { defaultSocialSetId } = useSocialSets(); const { plan } = usePlan(); const [disconnectingId, setDisconnectingId] = useState(null); const [subTab, setSubTab] = useState(channels.length > 0 ? "manage" : "connect"); // When channels load for the first time, default to manage if they have profiles useEffect(() => { if (!channelsLoading && channels.length > 0 && subTab === "connect") { // Only auto-switch on initial load, not after user interaction } }, [channelsLoading]); // Listen for channel changes from OAuth popup — switch to Manage tab to show new profile const handleChannelsChanged = useCallback(() => { refresh(); setSubTab("manage"); }, [refresh]); useEffect(() => { window.addEventListener("viraly:channels-changed", handleChannelsChanged); return () => window.removeEventListener("viraly:channels-changed", handleChannelsChanged); }, [handleChannelsChanged]); const handleDisconnect = async (id: string, name: string) => { if (!confirm(`Disconnect "${name}"?\n\nThis will remove the social account from Viraly. You can reconnect it at any time.`)) { return; } setDisconnectingId(id); try { await wpApi.del(`channels/${id}`); toast.success(`${name} disconnected successfully.`); refresh(); } catch { toast.error("Failed to disconnect. Please try again."); } finally { setDisconnectingId(null); } }; const atLimit = plan ? plan.channelsUsed >= plan.channelsLimit : false; const connectedCount = channels.length; return (
{/* Header */}

Social Profiles

Connect and manage your social media accounts.

{/* Sub-tabs */}
{/* Tab content */}
{/* Quota bar — visible on both tabs */} {plan &&
} {subTab === "manage" && ( <> {channelsLoading ? (
Loading profiles...
) : connectedCount > 0 ? (
{channels.map((ch) => ( ))}
) : (

No profiles connected yet

Connect your first social profile to get started.

)} )} {subTab === "connect" && ( 0} /> )}
); }