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.

2.6 KB
Raw Blame
<script setup lang="ts">
import { usePage } from '@inertiajs/vue3';
import { IconShare2 } from '@tabler/icons-vue';
import { computed, onMounted, ref, shallowRef } from 'vue';

import ShareChartDialog from '@/components/analytics/ShareChartDialog.vue';
import { Button } from '@/components/ui/button';
import {
    Tooltip,
    TooltipContent,
    TooltipProvider,
    TooltipTrigger,
} from '@/components/ui/tooltip';
import {
    prepareShareCard,
    type ShareMetricOption,
} from '@/lib/shareCard';
import { whenIdle } from '@/lib/utils';

const props = defineProps<{
    /** The channel the numbers belong to; the current workspace when left out. */
    subject?: string;
    range: string;
    /** Builds what the card shows; runs on open, so its labels read the loaded locale. */
    build: () => ShareMetricOption;
    testId: string;
}>();

const page = usePage();
/** Whose numbers the card shows: the channel, else the current workspace. */
const shareSubject = computed((): string => {
    if (props.subject) {
        return props.subject;
    }

    return page.props.auth.currentWorkspace?.name ?? '';
});
const open = ref(false);
const option = shallowRef<ShareMetricOption | null>(null);

/** The card's fonts and channel photos load while the page is idle, so the first preview paints at once. */
const preloaded = ref(false);

onMounted(() => {
    whenIdle(async () => {
        await prepareShareCard(props.build());
        preloaded.value = true;
    });
});

const openDialog = (): void => {
    option.value = props.build();
    open.value = true;
};
</script>

<template>
    <div class="contents">
        <TooltipProvider :delay-duration="300">
            <Tooltip>
                <TooltipTrigger as-child>
                    <Button
                        type="button"
                        variant="outline"
                        size="icon"
                        class="shrink-0"
                        :aria-label="$t('analytics.share.button')"
                        :data-testid="testId"
                        :data-state="preloaded ? 'ready' : 'loading'"
                        @click="openDialog"
                    >
                        <IconShare2 aria-hidden="true" />
                    </Button>
                </TooltipTrigger>
                <TooltipContent>{{ $t('analytics.share.button') }}</TooltipContent>
            </Tooltip>
        </TooltipProvider>
        <ShareChartDialog
            v-model:open="open"
            :subject="shareSubject"
            :range="range"
            :option="option"
        />
    </div>
</template>