Skip to content

Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.

26.6 KB
Raw Blame
<script setup lang="ts">
import { Link, usePage } from '@inertiajs/vue3';
import {
    IconCalendarEvent,
    IconTrendingUp,
    IconChevronDown,
    IconGripVertical,
    IconPlus,
    IconSearch,
    IconSettings,
} from '@tabler/icons-vue';
import { computed, ref } from 'vue';

import ChannelAvatar from '@/components/ChannelAvatar.vue';
import PlatformBrandIcon from '@/components/PlatformBrandIcon.vue';
import {
    HoverCard,
    HoverCardContent,
    HoverCardTrigger,
} from '@/components/ui/hover-card';
import { Kbd, KbdGroup } from '@/components/ui/kbd';
import {
    SidebarGroup,
    SidebarGroupAction,
    SidebarGroupLabel,
    SidebarMenuBadge,
    SidebarMenuButton,
    SidebarMenuItem,
    SidebarMenuSub,
    SidebarMenuSubButton,
    SidebarMenuSubItem,
    useSidebar,
} from '@/components/ui/sidebar';
import {
    Tooltip,
    TooltipContent,
    TooltipProvider,
    TooltipTrigger,
} from '@/components/ui/tooltip';
import { useActiveUrl } from '@/composables/useActiveUrl';
import { orderChannels, useChannelOrder } from '@/composables/useChannelOrder';
import { useCommandPalette } from '@/composables/useCommandPalette';
import { useConnectChannelDialog } from '@/composables/useConnectChannelDialog';
import { openPostComposer } from '@/composables/useGlobalPostComposer';
import { useNetworkConnect } from '@/composables/useNetworkConnect';
import { getPlatformLabel } from '@/composables/usePlatformLogo';
import { usePublishView } from '@/composables/usePublishView';
import { useWorkspaceAbilities } from '@/composables/useWorkspaceAbilities';
import { insights } from '@/routes/app/channels';
import { channels as channelsSettings } from '@/routes/app/workspace';
import { channelName, type SidebarChannel } from '@/types/channel';
import type { AvailablePlatform } from '@/types/social-account';
import { isConnectionLost } from '@/types/social-account';

const page = usePage();
const channels = computed<SidebarChannel[]>(() =>
    orderChannels((page.props.channels as SidebarChannel[]) ?? []),
);
const { canManageAccounts, canCreatePost } = useWorkspaceAbilities();
const { vSortableChannel, vSortableChannelList, onHandleKeydown, dragPreview } =
    useChannelOrder({
        list: 'sidebar',
        order: () => channels.value.map((channel) => channel.id),
    });
const reorderable = computed(
    () => canManageAccounts.value && channels.value.length > 1,
);
const draggedChannel = computed<SidebarChannel | null>(
    () =>
        channels.value.find(
            (channel) => channel.id === dragPreview.value?.channelId,
        ) ?? null,
);

type ChannelListEntry =
    | { key: string; channel: SidebarChannel }
    | { key: 'placeholder'; channel: null };

const listEntries = computed<ChannelListEntry[]>(() => {
    const entries: ChannelListEntry[] = channels.value.map((channel) => ({
        key: channel.id,
        channel,
    }));
    const preview = dragPreview.value;

    if (!preview) {
        return entries;
    }

    const anchor = entries.filter(
        (entry) => entry.channel?.id !== preview.channelId,
    )[preview.index];
    const position = anchor ? entries.indexOf(anchor) : entries.length;

    entries.splice(position, 0, { key: 'placeholder', channel: null });

    return entries;
});
const { urlIsActive } = useActiveUrl();
const { state: sidebarState, isMobile: sidebarIsMobile } = useSidebar();
const isCollapsedRail = computed(
    () => sidebarState.value === 'collapsed' && !sidebarIsMobile.value,
);
const channelCardItemClass =
    'flex min-h-8 items-center gap-2 rounded-md py-2 ps-2 pe-3 text-sm leading-4 font-medium outline-hidden transition-colors hover:bg-accent hover:text-accent-foreground focus-visible:bg-accent [&>svg]:size-4 [&>svg]:shrink-0 [&>svg]:text-muted-foreground';
const { open: openConnectDialog } = useConnectChannelDialog();
const { open: openCommandPalette } = useCommandPalette();
const isMacPlatform = /Mac|iPhone|iPad/.test(navigator.platform);
const headerActionClass =
    'static size-6 text-muted-foreground hover:bg-sidebar-action-hover hover:text-muted-foreground active:translate-y-[0.5px] transition-[background-color] duration-150 ease-out motion-reduce:transition-none [&>svg]:size-4';
const { startConnect } = useNetworkConnect(
    () => (page.props.connectablePlatforms as AvailablePlatform[]) ?? [],
);

const SUGGESTED_PLATFORMS = ['instagram', 'tiktok', 'linkedin'];

const suggestedPlatforms = computed(() => {
    const connectable = new Set(
        ((page.props.connectablePlatforms as AvailablePlatform[]) ?? []).map(
            (platform) => platform.value,
        ),
    );

    return SUGGESTED_PLATFORMS.filter((platform) => connectable.has(platform));
});

const { channelPublishUrl, isChannelPublishPage } = usePublishView();

const isCurrentChannel = (channel: SidebarChannel): boolean =>
    page.url.startsWith(`/channels/${channel.id}/`);

const toggledChannels = ref<Record<string, boolean>>({});

const isExpanded = (channel: SidebarChannel): boolean =>
    toggledChannels.value[channel.id] ?? isCurrentChannel(channel);

const toggleChannel = (channel: SidebarChannel): void => {
    toggledChannels.value = {
        ...toggledChannels.value,
        [channel.id]: !isExpanded(channel),
    };
};

const channelActionClass =
    'top-1.5 rounded-lg text-sidebar-foreground hover:bg-sidebar-action-hover active:translate-y-[0.5px] transition-[opacity,background-color,color,border-color] duration-[200ms,150ms,150ms,150ms] ease-[ease-in-out,ease-out,ease-out,ease-out] [&>svg]:text-muted-foreground md:opacity-0 md:group-hover/channel:opacity-100 md:group-has-focus-visible/channel:opacity-100 motion-reduce:transition-none';

const reconnect = (channel: SidebarChannel): void => {
    if (canManageAccounts.value) {
        startConnect(channel.platform, channel.id);
    }
};
</script>

<template>
    <SidebarGroup
        class="group/channels min-h-0 flex-1 px-4 pt-4 pb-0 group-data-[collapsible=icon]:px-2.5"
        data-testid="sidebar-channels"
    >
        <div class="relative">
            <SidebarGroupLabel
                class="group-data-[collapsible=icon]:hidden"
                data-testid="sidebar-channels-label"
            >
                <template v-if="channels.length > 0"
                    >{{ $t('sidebar.channels') }} · {{ channels.length }}</template
                >
                <template v-else-if="canManageAccounts">{{
                    $t('sidebar.connect_channels')
                }}</template>
                <template v-else>{{ $t('sidebar.channels') }}</template>
            </SidebarGroupLabel>
            <div
                :class="[
                    'absolute top-1 right-0 flex items-center gap-1 transition-opacity duration-(--motion-duration-sidebar-toggle) ease-(--motion-easing-sidebar-toggle) group-data-[collapsible=icon]:hidden',
                    channels.length > 0
                        ? 'focus-within:opacity-100 md:opacity-0 md:group-hover:opacity-100'
                        : '',
                ]"
                data-testid="sidebar-channels-actions"
            >
                <Tooltip>
                    <TooltipTrigger as-child>
                        <SidebarGroupAction
                            :class="['rounded-lg', headerActionClass]"
                            :aria-label="$t('sidebar.search_channels')"
                            :aria-keyshortcuts="isMacPlatform ? 'Meta+K' : 'Control+K'"
                            data-testid="sidebar-channels-search"
                            @click="openCommandPalette()"
                        >
                            <IconSearch />
                        </SidebarGroupAction>
                    </TooltipTrigger>
                    <TooltipContent
                        side="top"
                        class="flex items-center gap-2"
                        data-testid="sidebar-channels-search-tooltip"
                    >
                        {{ $t('sidebar.search_channels') }}
                        <KbdGroup data-testid="sidebar-channels-search-shortcut">
                            <Kbd>{{ isMacPlatform ? '⌘' : 'Ctrl' }}</Kbd>
                            <Kbd>K</Kbd>
                        </KbdGroup>
                    </TooltipContent>
                </Tooltip>
                <Tooltip v-if="canManageAccounts">
                    <TooltipTrigger as-child>
                        <SidebarGroupAction
                            as-child
                            :class="['rounded-md', headerActionClass]"
                        >
                            <Link
                                :href="channelsSettings.url()"
                                :aria-label="$t('channels.manage')"
                                data-testid="sidebar-channels-settings"
                            >
                                <IconSettings />
                            </Link>
                        </SidebarGroupAction>
                    </TooltipTrigger>
                    <TooltipContent
                        side="top"
                        data-testid="sidebar-channels-settings-tooltip"
                    >
                        {{ $t('channels.manage') }}
                    </TooltipContent>
                </Tooltip>
                <Tooltip v-if="canManageAccounts">
                    <TooltipTrigger as-child>
                        <SidebarGroupAction
                            :class="['rounded-lg', headerActionClass]"
                            :aria-label="$t('channels.connect')"
                            data-testid="sidebar-channels-connect"
                            @click="openConnectDialog()"
                        >
                            <IconPlus />
                        </SidebarGroupAction>
                    </TooltipTrigger>
                    <TooltipContent
                        side="top"
                        data-testid="sidebar-channels-connect-tooltip"
                    >
                        {{ $t('channels.connect') }}
                    </TooltipContent>
                </Tooltip>
            </div>
        </div>
        <div
            aria-hidden="true"
            class="mb-4 hidden h-px bg-sidebar-border group-data-[collapsible=icon]:block"
        />

        <TransitionGroup
            v-sortable-channel-list="reorderable"
            tag="ul"
            move-class="transition-transform duration-200 ease-out motion-reduce:transition-none"
            data-slot="sidebar-menu"
            data-sidebar="menu"
            class="relative -mx-4 mt-1 flex min-h-0 w-auto min-w-0 flex-col gap-2 overflow-y-auto px-4 pb-4 group-data-[collapsible=icon]:-mx-2.5 group-data-[collapsible=icon]:px-2.5"
            data-testid="sidebar-channels-list"
        >
            <SidebarMenuItem
                v-if="channels.length === 0"
                key="empty"
            >
                <div
                    v-if="canManageAccounts"
                    class="flex items-center gap-2 px-2 pt-1 pb-2"
                >
                    <Tooltip
                        v-for="platform in suggestedPlatforms"
                        :key="platform"
                    >
                        <TooltipTrigger as-child>
                            <button
                                type="button"
                                class="flex size-8 items-center justify-center rounded-[10px] border border-border-strong text-foreground transition-control group-data-[collapsible=icon]:hidden hover:bg-sidebar-action-hover focus-visible:ring-2 focus-visible:ring-ring focus-visible:outline-none"
                                :aria-label="
                                    $t('channels.details.connect', {
                                        network: getPlatformLabel(platform),
                                    })
                                "
                                :data-testid="`sidebar-channels-empty-connect-${platform}`"
                                @click="startConnect(platform)"
                            >
                                <PlatformBrandIcon :platform="platform" />
                            </button>
                        </TooltipTrigger>
                        <TooltipContent side="top">
                            {{
                                $t('channels.details.connect', {
                                    network: getPlatformLabel(platform),
                                })
                            }}
                        </TooltipContent>
                    </Tooltip>
                    <Tooltip>
                        <TooltipTrigger as-child>
                            <button
                                type="button"
                                class="flex size-8 items-center justify-center rounded-[10px] border border-dashed border-border-strong text-muted-foreground transition-control hover:border-solid hover:bg-sidebar-action-hover hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring focus-visible:outline-none"
                                :aria-label="$t('channels.connect')"
                                data-testid="sidebar-channels-empty"
                                @click="openConnectDialog()"
                            >
                                <IconPlus class="size-4" />
                            </button>
                        </TooltipTrigger>
                        <TooltipContent side="top">
                            {{ $t('channels.connect') }}
                        </TooltipContent>
                    </Tooltip>
                </div>
                <p
                    v-else
                    class="px-2 py-1.5 text-xs text-muted-foreground"
                    data-testid="sidebar-channels-empty"
                >
                    {{ $t('channels.empty') }}
                </p>
            </SidebarMenuItem>

            <template
                v-for="{ key, channel } in listEntries"
                :key="key"
            >
                <li
                    v-if="channel === null"
                    aria-hidden="true"
                    class="shrink-0 cursor-grabbing rounded-lg"
                    :class="
                        draggedChannel
                            ? null
                            : 'border border-dashed border-sidebar-border bg-sidebar-accent/60'
                    "
                    :style="{ height: `${dragPreview?.height ?? 0}px` }"
                    data-testid="sidebar-channel-placeholder"
                >
                    <div
                        v-if="draggedChannel"
                        class="flex h-9 items-center gap-2 rounded-lg bg-background ps-2 pe-2 shadow-md ring-1 ring-sidebar-border group-data-[collapsible=icon]:justify-center group-data-[collapsible=icon]:p-0.5"
                        data-testid="sidebar-channel-placeholder-preview"
                    >
                        <ChannelAvatar
                            :platform="draggedChannel.platform"
                            :src="draggedChannel.avatar_url"
                            :verified="draggedChannel.verified_badge"
                            :name="channelName(draggedChannel)"
                            :size="28"
                            ring="sidebar"
                            :reserve-space="!isCollapsedRail"
                        />
                        <span
                            class="truncate text-sm group-data-[collapsible=icon]:hidden"
                            >{{ channelName(draggedChannel) }}</span
                        >
                    </div>
                </li>
                <SidebarMenuItem
                    v-else
                    v-sortable-channel="reorderable ? channel.id : null"
                    class="group/channel"
                    :class="{ hidden: dragPreview?.channelId === channel.id }"
                    :data-testid="`sidebar-channel-row-${channel.id}`"
                >
                    <button
                        v-if="reorderable"
                        type="button"
                        class="absolute top-2 -start-3.5 z-10 flex h-5 w-3.5 cursor-grab items-center justify-center rounded-sm text-muted-foreground opacity-0 outline-hidden transition-opacity group-hover/channel:opacity-100 group-data-[collapsible=icon]:hidden focus-visible:opacity-100 focus-visible:ring-2 focus-visible:ring-sidebar-ring active:cursor-grabbing max-md:hidden"
                        :aria-label="$t('channels.reorder.handle', { name: channelName(channel) })"
                        :title="$t('channels.reorder.handle', { name: channelName(channel) })"
                        :data-channel-handle="`sidebar:${channel.id}`"
                        :data-testid="`sidebar-channel-handle-${channel.id}`"
                        @keydown="onHandleKeydown($event, channel.id)"
                    >
                        <IconGripVertical class="size-3.5" />
                    </button>
                    <HoverCard
                        :open-delay="100"
                        :close-delay="150"
                        :open="isCollapsedRail ? undefined : false"
                    >
                        <HoverCardTrigger as-child>
                            <SidebarMenuButton
                                as-child
                                :is-active="isCurrentChannel(channel)"
                                class="h-9 gap-2 py-1 ps-2 pe-8 max-md:pe-16 group-hover/channel:pe-16 group-has-focus-visible/channel:pe-16 data-[active=true]:bg-transparent data-[active=true]:hover:bg-sidebar-accent [&:has(~[data-sidebar=group-action]:hover)]:bg-sidebar-accent group-data-[collapsible=icon]:overflow-visible group-data-[collapsible=icon]:p-0.5! group-data-[collapsible=icon]:data-[active=true]:bg-sidebar-accent"
                            >
                                <Link
                                    :href="channelPublishUrl(channel.id)"
                                    :data-testid="`sidebar-channel-${channel.id}`"
                                >
                                    <ChannelAvatar
                                        :platform="channel.platform"
                                        :src="channel.avatar_url"
                                        :verified="channel.verified_badge"
                                        :name="channelName(channel)"
                                        :size="28"
                                        ring="sidebar"
                                        :reserve-space="!isCollapsedRail"
                                        :status="canManageAccounts ? null : channel.status"
                                        :account-id="channel.id"
                                    />
                                    <span
                                        class="truncate text-sm group-data-[collapsible=icon]:hidden"
                                        :data-testid="`sidebar-channel-name-${channel.id}`"
                                        >{{ channelName(channel) }}</span
                                    >
                                </Link>
                            </SidebarMenuButton>
                        </HoverCardTrigger>
                        <HoverCardContent
                            side="right"
                            align="start"
                            :side-offset="10"
                            class="w-auto min-w-[140px] rounded-xl border-0 px-2 py-3 shadow-md dark:border dark:border-border"
                            :data-testid="`sidebar-channel-${channel.id}-card`"
                        >
                            <p
                                class="truncate px-2 py-1.5 text-xs text-muted-foreground"
                                :data-testid="`sidebar-channel-${channel.id}-card-name`"
                            >
                                {{ channelName(channel) }}
                            </p>
                            <p
                                v-if="isConnectionLost(channel) && !canManageAccounts"
                                class="px-2 pb-1.5 text-xs text-destructive"
                            >
                                {{ $t('channels.connection_lost_hint') }}
                            </p>
                            <Link
                                :href="channelPublishUrl(channel.id)"
                                :class="channelCardItemClass"
                                :data-testid="`sidebar-channel-${channel.id}-card-publish`"
                            >
                                <IconCalendarEvent />
                                {{ $t('channels.publish') }}
                            </Link>
                            <Link
                                :href="insights.url(channel.id)"
                                :class="channelCardItemClass"
                                :data-testid="`sidebar-channel-${channel.id}-card-insights`"
                            >
                                <IconTrendingUp />
                                {{ $t('channels.insights') }}
                            </Link>
                        </HoverCardContent>
                    </HoverCard>
                    <TooltipProvider
                        v-if="isConnectionLost(channel) && canManageAccounts"
                        :delay-duration="200"
                    >
                        <Tooltip>
                            <TooltipTrigger as-child>
                                <button
                                    type="button"
                                    class="absolute start-1.5 top-0.5 z-10 size-2 cursor-pointer rounded-full bg-destructive ring-2 ring-sidebar outline-hidden focus-visible:ring-sidebar-ring group-data-[collapsible=icon]:start-0 group-data-[collapsible=icon]:top-0"
                                    :aria-label="$t('channels.reconnect')"
                                    :data-testid="`sidebar-channel-${channel.id}-lost`"
                                    @click="reconnect(channel)"
                                />
                            </TooltipTrigger>
                            <TooltipContent side="right">
                                {{ $t('channels.connection_lost_hint') }}
                            </TooltipContent>
                        </Tooltip>
                    </TooltipProvider>
                    <SidebarMenuBadge
                        :class="[
                            'top-2 right-0 group-hover/channel:hidden group-has-focus-visible/channel:hidden max-md:hidden',
                            isExpanded(channel) ? 'hidden' : '',
                        ]"
                        :data-testid="`sidebar-channel-count-${channel.id}`"
                    >
                        {{ channel.scheduled_posts_count }}
                    </SidebarMenuBadge>
                    <SidebarGroupAction
                        v-if="canCreatePost"
                        :class="['right-[30px]', channelActionClass]"
                        :title="$t('channels.new_post')"
                        :data-testid="`sidebar-channel-${channel.id}-new-post`"
                        @click="openPostComposer({ socialAccountIds: [channel.id] })"
                    >
                        <IconPlus />
                    </SidebarGroupAction>

                    <SidebarGroupAction
                        :class="['right-0.5 aria-expanded:bg-transparent', channelActionClass]"
                        :aria-label="
                            $t('sidebar.channel_submenu', {
                                name: channelName(channel),
                            })
                        "
                        :aria-expanded="isExpanded(channel)"
                        :data-testid="`sidebar-channel-${channel.id}-toggle`"
                        @click="toggleChannel(channel)"
                    >
                        <IconChevronDown
                            :class="[
                                'transition-transform duration-200 ease-[ease] motion-reduce:transition-none',
                                isExpanded(channel) ? 'rotate-180' : '',
                            ]"
                        />
                    </SidebarGroupAction>

                    <div
                        class="motion-collapse"
                        :data-expanded="isExpanded(channel)"
                        :data-testid="`sidebar-channel-${channel.id}-submenu`"
                    >
                        <div>
                            <SidebarMenuSub>
                                <SidebarMenuSubItem>
                                    <SidebarMenuSubButton
                                        as-child
                                        :is-active="isChannelPublishPage(channel.id)"
                                    >
                                        <Link
                                            :href="channelPublishUrl(channel.id)"
                                            :data-testid="`sidebar-channel-${channel.id}-publish`"
                                        >
                                            <IconCalendarEvent />
                                            <span class="min-w-0 flex-1 truncate">{{ $t('channels.publish') }}</span>
                                            <span
                                                v-if="channel.scheduled_posts_count > 0"
                                                class="flex w-6 shrink-0 justify-center tabular-nums"
                                                :data-testid="`sidebar-channel-${channel.id}-publish-count`"
                                            >{{ channel.scheduled_posts_count }}</span>
                                        </Link>
                                    </SidebarMenuSubButton>
                                </SidebarMenuSubItem>
                                <SidebarMenuSubItem>
                                    <SidebarMenuSubButton
                                        as-child
                                        :is-active="urlIsActive(insights.url(channel.id))"
                                    >
                                        <Link
                                            :href="insights.url(channel.id)"
                                            :data-testid="`sidebar-channel-${channel.id}-insights`"
                                        >
                                            <IconTrendingUp />
                                            <span>{{ $t('channels.insights') }}</span>
                                        </Link>
                                    </SidebarMenuSubButton>
                                </SidebarMenuSubItem>
                            </SidebarMenuSub>
                        </div>
                    </div>
                </SidebarMenuItem>
            </template>
        </TransitionGroup>
    </SidebarGroup>
</template>