import { useEffect, useState, useCallback, useRef } 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, error: channelsError, refresh } = useChannels(); const { defaultSocialSetId } = useSocialSets(); const { plan } = usePlan(); const [disconnectingId, setDisconnectingId] = useState(null); const [subTab, setSubTabState] = useState("connect"); const hasUserInteracted = useRef(false); const hasAutoSwitched = useRef(false); const setSubTab = (tab: SubTab) => { hasUserInteracted.current = true; setSubTabState(tab); }; // On first render the channels query has not resolved, so the initial state // cannot know whether the user has profiles. Once loading finishes, land // returning users on Manage - unless they already clicked a tab themselves. useEffect(() => { if (!channelsLoading && !hasAutoSwitched.current) { hasAutoSwitched.current = true; if (channels.length > 0 && !hasUserInteracted.current) { setSubTabState("manage"); } } }, [channelsLoading, channels.length]); // A SUCCESSFUL connect switches to Manage to show the new profile; plain // channels-changed refetches (popup closed, errors) must not yank the user // off the Connect tab. const handleOAuthComplete = useCallback(() => { refresh(); setSubTabState("manage"); }, [refresh]); useEffect(() => { window.addEventListener("viraly:oauth-complete", handleOAuthComplete); return () => window.removeEventListener("viraly:oauth-complete", handleOAuthComplete); }, [handleOAuthComplete]); 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...
) : channelsError ? (

Could not reach Viraly

Your profiles could not be loaded. This is a connection problem, not an empty account.

) : connectedCount > 0 ? (
{channels.map((ch) => ( ))}
) : (

No profiles connected yet

Connect your first social profile to get started.

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