summaryrefslogtreecommitdiff
path: root/apps/web/src/animation/channel-data.ts
blob: 2fc08b5c00050b0d3375ed2976ea91583aca0f69 (plain) (blame)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import type {
	AnimationChannel,
	ChannelData,
	CompositeChannelData,
} from "@/animation/types";

const LEGACY_ANIMATION_STORAGE_KEYS = new Set(["bindings", "channels"]);

function isRecord(value: unknown): value is Record<string, unknown> {
	return typeof value === "object" && value !== null;
}

export function isLeafChannelData(
	data: ChannelData | undefined,
): data is AnimationChannel {
	return isRecord(data) && Array.isArray(data.keys);
}

export function isCompositeChannelData(
	data: ChannelData | undefined,
): data is CompositeChannelData {
	return isRecord(data) && !Array.isArray(data.keys);
}

export function getChannelsFromData({
	data,
}: {
	data: ChannelData | undefined;
}): AnimationChannel[] {
	if (isLeafChannelData(data)) {
		return [data];
	}
	if (!isCompositeChannelData(data)) {
		return [];
	}
	return Object.values(data).filter(isLeafChannelData);
}

export function getChannelEntriesFromData({
	data,
}: {
	data: ChannelData | undefined;
}): Array<[string, AnimationChannel]> {
	if (isLeafChannelData(data)) {
		return [["value", data]];
	}
	if (!isCompositeChannelData(data)) {
		return [];
	}
	return Object.entries(data).flatMap(([componentKey, channel]) =>
		isLeafChannelData(channel) ? [[componentKey, channel]] : [],
	);
}

export function isAnimationStorageKey({ key }: { key: string }): boolean {
	return !LEGACY_ANIMATION_STORAGE_KEYS.has(key);
}