import { Delete } from "@openstatus/icons"; export const actions = [ { id: "delete", label: "Delete", icon: Delete, variant: "destructive" as const, }, ] as const; export type PageComponentAction = (typeof actions)[number]; export const getActions = ( props: Partial Promise | void>>, ): (PageComponentAction & { onClick?: () => Promise | void })[] => { return actions.map((action) => ({ ...action, onClick: props[action.id as keyof typeof props], })); }; type OrderedComponent = { id: number; name: string; groupId?: number | null; order?: number | null; groupOrder?: number | null; }; export type ComponentSection = { /** null for a run of ungrouped components */ group: { id: number; name: string } | null; items: C[]; }; /** * Splits components into sections in page order, as the components config * and the public page show them: ungrouped components by `order`, a group * placed at its first member's `order`, members by `groupOrder`. Consecutive * ungrouped components share one section; empty groups are dropped. */ export function toComponentSections( components: C[], groups: { id: number; name: string }[] = [], ): ComponentSection[] { const groupById = new Map(groups.map((g) => [g.id, g])); const members = new Map(); const ungrouped: C[] = []; for (const c of components) { if (c.groupId != null && groupById.has(c.groupId)) { members.set(c.groupId, [...(members.get(c.groupId) ?? []), c]); } else { ungrouped.push(c); } } const entries: { order: number; section: ComponentSection }[] = []; for (const c of ungrouped) { entries.push({ order: c.order ?? 0, section: { group: null, items: [c] } }); } for (const [groupId, items] of members) { const group = groupById.get(groupId); if (!group) continue; items.sort((a, b) => (a.groupOrder ?? 0) - (b.groupOrder ?? 0)); entries.push({ order: Math.min(...items.map((c) => c.order ?? 0)), section: { group: { id: group.id, name: group.name }, items }, }); } entries.sort((a, b) => a.order - b.order); // merge adjacent ungrouped singletons into one run const sections: ComponentSection[] = []; for (const { section } of entries) { const last = sections.at(-1); if (section.group === null && last?.group === null) { last.items.push(...section.items); } else { sections.push(section); } } return sections; } /** componentId -> group name, for components in a known group. */ export function toGroupNameLookup( components: { id: number; groupId?: number | null }[], groups: { id: number; name: string }[], ): Map { const groupName = new Map(groups.map((g) => [g.id, g.name])); const lookup = new Map(); for (const c of components) { const name = c.groupId != null ? groupName.get(c.groupId) : undefined; if (name) lookup.set(c.id, name); } return lookup; }