SPB Git

Repository Public

itemmeta.ts 4.0 KB 64 lines typescript

1// Presentation helpers for items: icons, subtitles, sidebar filters.2import { type Item, type ItemType, parseOrigin, TEMPLATES } from "../../../../packages/protocol/src/index.ts";3import type { ItemView, VaultView } from "../core/engine.ts";4import type { IconName } from "./common.tsx";56export const TYPE_ICON: Record<ItemType, IconName> = {7  login: "login", passkey: "passkey", card: "card", identity: "id", note: "note", wifi: "wifi", license: "cert", document: "doc",8  "ssh-key": "terminal", "api-token": "key", certificate: "cert", database: "db", env: "code",9};10export const ITEM_TYPES = Object.keys(TEMPLATES) as ItemType[];11export const keyOf = (v: { vaultId: string; id: string }) => `${v.vaultId}/${v.id}`;1213export function hostOf(item: Item): string {14  for (const u of item.urls) { const o = parseOrigin(u.url); if (o) return o.display; }15  return "";16}17/** Second line of a list row: username / host / type. Never a secret. */18export function subtitle(item: Item): string {19  const f = item.fields;20  const who = f.username || f.userName || f.holder || f.fullName || f.ssid || f.licensee || f.service || f.subject || f.project || "";21  const where = hostOf(item) || f.rpId || f.host || f.product || "";22  return [who, where].filter(Boolean).join(" · ") || TEMPLATES[item.type].label;23}2425export const CATEGORIES: { id: string; label: string; icon: IconName; test: (i: Item) => boolean }[] = [26  { id: "passwords", label: "Passwords", icon: "key", test: (i) => TEMPLATES[i.type].section === "passwords" },27  { id: "passkeys", label: "Passkeys", icon: "passkey", test: (i) => i.type === "passkey" },28  { id: "totp", label: "One-time codes", icon: "clock", test: (i) => !!i.fields.totp },29  { id: "cards", label: "Cards", icon: "card", test: (i) => i.type === "card" },30  { id: "notes", label: "Secure Notes", icon: "note", test: (i) => i.type === "note" },31  { id: "documents", label: "Documents", icon: "doc", test: (i) => TEMPLATES[i.type].section === "documents" },32  { id: "developer", label: "Developer Secrets", icon: "code", test: (i) => TEMPLATES[i.type].section === "developer" },33];3435export function applyFilter(list: ItemView[], filter: string): ItemView[] {36  if (filter === "trash") return list.filter((v) => v.item.trashedAt);37  const live = list.filter((v) => !v.item.trashedAt);38  if (filter === "fav") return live.filter((v) => v.item.favorite);39  if (filter.startsWith("vault:")) return live.filter((v) => v.vaultId === filter.slice(6));40  if (filter.startsWith("tag:")) return live.filter((v) => v.item.tags.includes(filter.slice(4)));41  if (filter.startsWith("cat:")) { const c = CATEGORIES.find((x) => x.id === filter.slice(4)); return c ? live.filter((v) => c.test(v.item)) : live; }42  return live;43}44export function filterTitle(filter: string, vaults: VaultView[]): string {45  if (filter === "trash") return "Trash";46  if (filter === "fav") return "Favorites";47  if (filter.startsWith("vault:")) return vaults.find((v) => v.vaultId === filter.slice(6))?.meta.name ?? "Vault";48  if (filter.startsWith("tag:")) return `#${filter.slice(4)}`;49  if (filter.startsWith("cat:")) return CATEGORIES.find((x) => x.id === filter.slice(4))?.label ?? "Items";50  return "All Items";51}52export type Sort = "title" | "updated";53export function sortItems(list: ItemView[], sort: Sort): ItemView[] {54  const out = [...list];55  if (sort === "title") out.sort((a, b) => a.item.title.localeCompare(b.item.title, undefined, { sensitivity: "base", numeric: true }) || a.id.localeCompare(b.id));56  else out.sort((a, b) => b.item.updatedAt.localeCompare(a.item.updatedAt) || a.id.localeCompare(b.id));57  return out;58}59export const canWrite = (v?: VaultView) => !!v && v.role !== "reader";60export const KIND_LABEL: Record<string, string> = { personal: "Personal", work: "Work", infrastructure: "Infrastructure", shared: "Shared" };61/** Multiline template fields that are sensitive even though the template does not mark them as secret. */62export const SENSITIVE_KEYS = new Set(["recoveryCodes"]);63export const MULTILINE_SECRETS = new Set(["privateKey", "dotenv", "recoveryCodes"]);64
Files main
Menu

Theme