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.

4.4 KB
Raw Blame
<script setup lang="ts">
import {
    VisAxis,
    VisStackedBar,
    VisStackedBarSelectors,
    VisXYContainer,
} from '@unovis/vue';
import { trans } from 'laravel-vue-i18n';
import { computed } from 'vue';

import {
    formatCountTick,
    socialAccountChartConfig,
} from '@/components/analytics/workspace/charts/socialAccountChart';
import {
    ChartContainer,
    ChartTooltip,
    ChartTooltipContent,
    type ChartTooltipDetail,
    componentToString,
    numericTick,
} from '@/components/ui/chart';
import { useChartAppContext } from '@/composables/analytics/useChartAppContext';
import { accountColor } from '@/lib/analyticsColors';
import { bucketRangeLabel } from '@/lib/channelMetrics';
import { formatFullNumber } from '@/lib/utils';
import type {
    BucketResolution,
    PostAccount,
    PostBucket,
} from '@/types/analytics';


type ChartPoint = {
    index: number;
    label: string;
    [key: string]: string | number;
};

const props = defineProps<{
    accounts: PostAccount[];
    buckets: PostBucket[];
    resolution: BucketResolution;
    colors: Record<string, string>;
}>();

const chartData = computed<ChartPoint[]>(() =>
    props.buckets.map((bucket, index) => {
        const point: ChartPoint = { index, label: bucketRangeLabel(bucket, props.resolution) };

        props.accounts.forEach((account, accountIndex) => {
            point[`account_${accountIndex}`] =
                bucket.accounts[account.social_account_key] ?? 0;
        });

        return point;
    }),
);
const chartConfig = computed(() =>
    socialAccountChartConfig(props.accounts, props.colors),
);
const xAccessor = (point: ChartPoint): number => point.index;
const yAccessors = computed(() =>
    props.accounts.map(
        (_, index) =>
            (point: ChartPoint): number =>
                Number(point[`account_${index}`] ?? 0),
    ),
);
const barColors = computed(() =>
    props.accounts.map(
        (account, index) =>
            props.colors[account.social_account_key] ?? accountColor(index),
    ),
);
const formatBucket = numericTick(
    (tick): string => chartData.value[Math.round(tick)]?.label ?? '',
);
/** The posts of a column across every channel, as the report counts them. */
const bucketTotal = numericTick((tick): ChartTooltipDetail | null => {
    const bucket = props.buckets[Math.round(tick)];

    if (!bucket) {
        return null;
    }

    return {
        label: trans('analytics.dashboard.total_posts'),
        value: formatFullNumber(bucket.total),
    };
});
const ticks = computed(() => props.buckets.map((_, index) => index));
const appContext = useChartAppContext();
const tooltipTemplate = computed(() =>
    componentToString(
        chartConfig.value,
        ChartTooltipContent,
        { labelFormatter: formatBucket, totalFormatter: bucketTotal },
        appContext,
    ),
);
const tooltipTriggers = computed(() => ({
    [VisStackedBarSelectors.bar]: (bar: {
        datum: ChartPoint;
    }): string | undefined => {
        const point = bar.datum;
        return tooltipTemplate.value?.(point, point.index);
    },
}));
const barAttributes = {
    [VisStackedBarSelectors.bar]: { 'data-testid': 'analytics-post-bar' },
};
</script>

<template>
    <ChartContainer
        :config="chartConfig"
        cursor
        class="h-72 w-full sm:h-80 [--vis-stacked-bar-stroke-color:var(--card)] [--vis-stacked-bar-stroke-width:1.5px]"
        data-testid="posts-unovis-chart"
    >
        <VisXYContainer
            :data="chartData"
            :padding="{ top: 14, right: 8, bottom: 8, left: 0 }"
        >
            <VisStackedBar
                :x="xAccessor"
                :y="yAccessors"
                :color="barColors"
                :rounded-corners="4"
                :bar-padding="0.35"
                :bar-max-width="28"
                :attributes="barAttributes"
            />
            <VisAxis
                type="x"
                :tick-format="formatBucket"
                :tick-values="ticks"
                :grid-line="false"
                :domain-line="false"
                :tick-line="false"
            />
            <VisAxis
                type="y"
                :tick-format="formatCountTick"
                :num-ticks="5"
                :grid-line="true"
                :domain-line="false"
                :tick-line="false"
            />
            <ChartTooltip :triggers="tooltipTriggers" />
        </VisXYContainer>
    </ChartContainer>
</template>