summaryrefslogtreecommitdiff
path: root/apps/web/src/actions/keybindings/persisted-state.ts
blob: 57d83f6d4d5138e439ffaa0ef3fdb455b85e70ee (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
export type PersistedKeybindingConfig = Record<string, string | undefined>;

export interface PersistedKeybindingsState {
	keybindings: PersistedKeybindingConfig;
	isCustomized: boolean;
}

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

export function getPersistedKeybindingsState({
	state,
}: {
	state: unknown;
}): PersistedKeybindingsState | null {
	if (!isRecord(state)) return null;

	const { keybindings, isCustomized } = state;
	if (!isRecord(keybindings) || typeof isCustomized !== "boolean") {
		return null;
	}

	const normalizedKeybindings: PersistedKeybindingConfig = {};
	for (const [key, action] of Object.entries(keybindings)) {
		if (action !== undefined && typeof action !== "string") {
			return null;
		}

		normalizedKeybindings[key] = action;
	}

	return {
		keybindings: normalizedKeybindings,
		isCustomized,
	};
}