All files / apps/developer-portal/layers/workflows/app/composables useProjectReplay.ts

100% Statements 29/29
100% Branches 17/17
100% Functions 9/9
100% Lines 24/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43      3x     3x 11x 11x 11x   3x 3x   3x 20x     11x   11x 13x 3x   11x 11x 8x 8x   2x     11x 10x 10x 10x   1x     11x    
import type { ComputedRef } from "vue";
import { projectView, replayRange, type ProjectView } from "#layers/workflows/shared/workflows.ts";
 
const DAY_MS = 86_400_000;
 
/** Lays the synced project over any day its snapshot covers, the latest by default. */
export const useProjectReplay = (base: ComputedRef<ProjectView | undefined>) => {
    const range = computed(() => replayRange(base.value?.snapshot ?? null));
    const days = computed(() => {
        if (!range.value) return [];
 
        const first = Date.parse(`${range.value.first}T00:00:00Z`);
        const last = Date.parse(`${range.value.last}T00:00:00Z`);
 
        return Array.from({ length: Math.round((last - first) / DAY_MS) + 1 }, (_, index) =>
            new Date(first + index * DAY_MS).toISOString().slice(0, 10),
        );
    });
    const picked = ref<number | null>(null);
    // Derived on read: the snapshot arrives after setup, so a clamp taken then would stick at -1.
    const dayIndex = computed({
        get: () => Math.min(picked.value ?? Number.MAX_SAFE_INTEGER, days.value.length - 1),
        set: (index: number) => (picked.value = index >= days.value.length - 1 ? null : index),
    });
    const isLatest = computed(() => dayIndex.value >= days.value.length - 1);
    const dayLabel = computed(() => {
        const day = days.value[dayIndex.value];
        if (!day) return "";
 
        return isLatest.value ? `${calendarDay(day)} ยท today` : calendarDay(day);
    });
 
    const view = computed(() => {
        const synced = base.value;
        const day = days.value[dayIndex.value];
        if (!synced || isLatest.value || !day) return synced;
 
        return projectView(synced.project, synced.template, synced.snapshot, day);
    });
 
    return { range, days, dayIndex, isLatest, dayLabel, view };
};