"use client"; import { AlertCircleIcon, CheckCircle2Icon, LoaderCircleIcon, PlugIcon, UnplugIcon, } from "lucide-react"; import { toast } from "sonner"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Item, ItemActions, ItemContent, ItemDescription, ItemMedia, ItemTitle, } from "@/components/ui/item"; import { useChannelConnections, useChannelProviders, useConnectChannelProvider, useDisconnectChannelConnection, } from "@/core/channels/hooks"; import { closeConnectWindow, openConnectUrl, prepareConnectWindow, } from "@/core/channels/open-connect-url"; import type { ChannelConnection, ChannelProvider } from "@/core/channels/types"; import { useI18n } from "@/core/i18n/hooks"; import { cn } from "@/lib/utils"; import { ChannelProviderIcon } from "../channels/channel-provider-icon"; import { SettingsSection } from "./settings-section"; function getProviderDescription( provider: ChannelProvider, descriptions: Record, ): string { return descriptions[provider.provider] ?? provider.display_name; } function getConnectionLabel(connection: ChannelConnection): string | null { const account = connection.external_account_name; const workspace = connection.workspace_name; if (account && workspace) { return `${account} ยท ${workspace}`; } return account ?? workspace ?? connection.external_account_id ?? null; } function getStatusLabel( provider: ChannelProvider, connection: ChannelConnection | undefined, t: ReturnType["t"], ): string { if (!provider.enabled) { return t.channels.disabled; } if (!provider.configured) { return t.channels.unconfigured; } if (provider.unavailable_reason) { return t.channels.unavailableShort; } const status = connection?.status ?? provider.connection_status; if (status === "connected") { return t.channels.connected; } if (status === "pending") { return t.channels.pending; } if (status === "revoked") { return t.channels.revoked; } return t.channels.notConnected; } function getProviderDisabledReason( provider: ChannelProvider, t: ReturnType["t"], ): string | undefined { if (!provider.enabled) { return t.channels.disabled; } if (!provider.configured) { return t.channels.unconfigured; } return provider.unavailable_reason ?? undefined; } function ChannelProviderItem({ provider, connection, }: { provider: ChannelProvider; connection?: ChannelConnection; }) { const { t } = useI18n(); const connectMutation = useConnectChannelProvider(); const disconnectMutation = useDisconnectChannelConnection(); const isConnected = connection?.status === "connected"; const canConnect = (provider.connectable ?? (provider.enabled && provider.configured)) && !isConnected; const isConnecting = connectMutation.isPending && connectMutation.variables === provider.provider; const isDisconnecting = disconnectMutation.isPending && disconnectMutation.variables === connection?.id; const connectionLabel = connection ? getConnectionLabel(connection) : null; const statusLabel = getStatusLabel(provider, connection, t); const unavailableReason = getProviderDisabledReason(provider, t); return ( {provider.display_name} {isConnected ? : } {statusLabel} {getProviderDescription(provider, t.channels.descriptions)} {connectionLabel ? ` ${t.channels.connectedAs(connectionLabel)}` : ""} {!isConnected && provider.unavailable_reason ? ` ${provider.unavailable_reason}` : ""} {isConnected && connection ? ( ) : ( )} ); } export function ChannelsSettingsPage() { const { t } = useI18n(); const { enabled, providers, isLoading: providersLoading, error: providersError, } = useChannelProviders(); const { connections, isLoading: connectionsLoading, error: connectionsError, } = useChannelConnections(); const isLoading = providersLoading || connectionsLoading; const error = providersError ?? connectionsError; const connectionByProvider = new Map(); for (const connection of connections) { const existing = connectionByProvider.get(connection.provider); if (!existing || connection.status === "connected") { connectionByProvider.set(connection.provider, connection); } } return ( {isLoading ? (
{t.common.loading}
) : error ? (
{t.channels.unavailable}
) : !enabled ? (
{t.settings.channels.disabled}
) : (
{providers.map((provider) => ( ))}
)}
); }