All files / apps/developer-portal/app/utils dashboardLayout.ts

98.5% Statements 66/67
85.71% Branches 42/49
100% Functions 20/20
100% Lines 51/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 15033x                                               39x   199x 199x     33x   33x           10x 10x   10x 18x   18x 18x 18x   18x   16x 16x     10x     33x         10x   10x 3x   2x 2x               33x       13x   13x 11x   55x 10x 10x 10x   10x 55x   37x 24x     10x     33x   5x         33x     1x       2x   2x       33x         8x 4x   4x   2x 2x   2x     33x     9x                    
const WIDGET_SIZES = ["small", "medium", "wide"] as const;
 
export type WidgetSize = (typeof WIDGET_SIZES)[number];
 
export interface WidgetDefinition {
    id: string;
    label: string;
    icon?: string;
    size: WidgetSize;
    /** Starts in the Add widget list rather than on the dashboard. */
    hidden?: boolean;
}
 
export interface WidgetPlacement {
    id: string;
    size: WidgetSize;
}
 
export interface DashboardLayout {
    version: 1;
    widgets: WidgetPlacement[];
    hidden: string[];
}
 
export const defaultLayout = (definitions: readonly WidgetDefinition[]): DashboardLayout => ({
    version: 1,
    widgets: definitions.filter((widget) => !widget.hidden).map(({ id, size }) => ({ id, size })),
    hidden: definitions.filter((widget) => widget.hidden).map((widget) => widget.id),
});
 
const isSize = (value: unknown): value is WidgetSize => WIDGET_SIZES.includes(value as WidgetSize);
 
const savedPlacements = (
    saved: object,
    byId: Map<string, WidgetDefinition>,
    seen: Set<string>,
): WidgetPlacement[] => {
    const entries: unknown[] =
        "widgets" in saved && Array.isArray(saved.widgets) ? saved.widgets : [];
    const widgets: WidgetPlacement[] = [];
 
    for (const entry of entries) {
        Iif (typeof entry !== "object" || entry === null) continue;
 
        const id = "id" in entry ? entry.id : undefined;
        const size = "size" in entry ? entry.size : undefined;
        const definition = typeof id === "string" ? byId.get(id) : undefined;
 
        if (!definition || seen.has(definition.id)) continue;
 
        seen.add(definition.id);
        widgets.push({ id: definition.id, size: isSize(size) ? size : definition.size });
    }
 
    return widgets;
};
 
const savedHiddenIds = (
    saved: object,
    byId: Map<string, WidgetDefinition>,
    seen: Set<string>,
): string[] => {
    const entries: unknown[] = "hidden" in saved && Array.isArray(saved.hidden) ? saved.hidden : [];
 
    return entries.filter((id): id is string => {
        if (typeof id !== "string" || !byId.has(id) || seen.has(id)) return false;
 
        seen.add(id);
        return true;
    });
};
 
/**
 * Reads whatever the browser had saved against the widgets that exist now: unknown ids are
 * dropped, and a widget the saved layout never saw takes its default place.
 */
export const resolveLayout = (
    saved: unknown,
    definitions: readonly WidgetDefinition[],
): DashboardLayout => {
    const fallback = defaultLayout(definitions);
 
    if (typeof saved !== "object" || saved === null || !("version" in saved)) return fallback;
    if (saved.version !== 1) return fallback;
 
    const byId = new Map(definitions.map((widget) => [widget.id, widget]));
    const seen = new Set<string>();
    const widgets = savedPlacements(saved, byId, seen);
    const hidden = savedHiddenIds(saved, byId, seen);
 
    for (const definition of definitions) {
        if (seen.has(definition.id)) continue;
 
        if (definition.hidden) hidden.push(definition.id);
        else widgets.push({ id: definition.id, size: definition.size });
    }
 
    return { version: 1, widgets, hidden };
};
 
export const removeWidget = (layout: DashboardLayout, id: string): DashboardLayout => ({
    ...layout,
    widgets: layout.widgets.filter((widget) => widget.id !== id),
    hidden: layout.hidden.includes(id) ? layout.hidden : [...layout.hidden, id],
});
 
/** Puts the widget first, where whoever just added it is already looking. */
export const addWidget = (
    layout: DashboardLayout,
    definition: WidgetDefinition,
): DashboardLayout => ({
    ...layout,
    widgets: [
        { id: definition.id, size: definition.size },
        ...layout.widgets.filter((widget) => widget.id !== definition.id),
    ],
    hidden: layout.hidden.filter((id) => id !== definition.id),
});
 
/** Swaps a widget with its neighbour, the keyboard's stand-in for dragging. */
export const moveWidget = (
    layout: DashboardLayout,
    id: string,
    direction: -1 | 1,
): DashboardLayout => {
    const from = layout.widgets.findIndex((widget) => widget.id === id);
    const to = from + direction;
 
    if (from === -1 || to < 0 || to >= layout.widgets.length) return layout;
 
    const widgets = [...layout.widgets];
    [widgets[from], widgets[to]] = [widgets[to]!, widgets[from]!];
 
    return { ...layout, widgets };
};
 
export const cycleSize = (layout: DashboardLayout, id: string): DashboardLayout => ({
    ...layout,
    widgets: layout.widgets.map((widget) =>
        widget.id === id
            ? {
                  ...widget,
                  size: WIDGET_SIZES[
                      (WIDGET_SIZES.indexOf(widget.size) + 1) % WIDGET_SIZES.length
                  ]!,
              }
            : widget,
    ),
});