logigate/app/lib/recent.ts

27 lines
835 B
TypeScript
Raw Normal View History

// Parts placed lately, newest first ("nand", "chip:<id>"), kept in this
// browser: the parts list and the quick-add box offer them first.
import { ref } from 'vue'
import type { NodeKind } from './logic/types'
const KEY = 'logigate:recent-parts'
function load(): string[] {
try {
const v = JSON.parse(localStorage.getItem(KEY) ?? '[]')
return Array.isArray(v) ? v.filter(x => typeof x === 'string').slice(0, 8) : []
} catch {
return []
}
}
export const recentParts = ref<string[]>(typeof localStorage === 'undefined' ? [] : load())
export function rememberPart(kind: NodeKind, chip?: string) {
const key = chip ? `chip:${chip}` : kind
recentParts.value = [key, ...recentParts.value.filter(k => k !== key)].slice(0, 8)
try {
localStorage.setItem(KEY, JSON.stringify(recentParts.value))
} catch {}
}