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.

1.7 KB
Raw Blame
import { type AppContext, type Component, h, render } from 'vue';

import { activeLocale } from '@/language';

import type { ChartConfig } from '.';

export const componentToString = (
    config: ChartConfig,
    component: Component,
    props: Record<string, unknown> = {},
    appContext: AppContext | null = null,
): ((data: Record<string, unknown>, x?: number | Date) => string) | undefined => {
    if (typeof document === 'undefined') return undefined;

    const cache = new Map<string, string>();

    return (source, x) => {
        const data =
            'data' in source && source.data && typeof source.data === 'object'
                ? (source.data as Record<string, unknown>)
                : source;
        const key = `${activeLocale.value}-${x}-${JSON.stringify(data)}`;
        const cached = cache.get(key);
        if (cached) return cached;

        const element = document.createElement('div');
        const tooltip = h(component, { ...props, payload: data, config, x });
        tooltip.appContext = appContext;
        render(tooltip, element);
        const content = element.innerHTML;
        render(null, element);
        cache.set(key, content);
        return content;
    };
};

/**
 * unovis types an axis tick as `number | Date`, since the same axis also
 * draws time. Our charts only run on numbers (a column's index or a value),
 * so each formatter takes the number, and this is the one place that reads
 * it out of unovis' type.
 */
export const numericTick =
  <Result>(format: (tick: number, index: number, ticks: number[]) => Result) =>
  (tick: number | Date, index = 0, ticks: number[] | Date[] = []): Result =>
    format(Number(tick), index, ticks.map(Number))