All files / src/components/dashboard WidgetPicker.tsx

15% Statements 3/20
0% Branches 0/10
0% Functions 0/8
15% Lines 3/20

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            2x               2x                                     2x                                                                                                                                                                                                        
import { WidgetTypeSpec } from "@/objects/Dashboard";
import { Radio, Sparkles, X } from "lucide-react";
import React from "react";
import { TONES } from "@/components/appearance/tones";
import { presenterFor } from "./widgets/registry";
 
const CATEGORY_LABELS: Record<string, string> = {
  content: "Your content",
  platform: "Platform data",
  network: "Networks",
  feeds: "Live sources",
  intelligence: "Intelligence",
};
 
const CATEGORY_BLURBS: Record<string, string> = {
  content: "Things you write or keep.",
  platform: "Live figures from your own usage.",
  network: "How your agents, knowledge and memory connect — drawn from your own records.",
  feeds: "Exact data pulled straight from a named source.",
  intelligence: "Tiles an agent keeps up to date for you — a model's judgement, not fact.",
};
 
export interface WidgetPickerProps {
  specs: WidgetTypeSpec[];
  onCancel: () => void;
  onPick: (spec: WidgetTypeSpec) => void;
}
 
/** The add-widget catalogue.
 *
 *  Driven entirely by the backend registry, so a widget type added server-side
 *  appears here without a frontend change.
 */
const WidgetPicker: React.FC<WidgetPickerProps> = ({
  specs,
  onCancel,
  onPick,
}) => {
  const grouped = React.useMemo(() => {
    const order = ["content", "platform", "network", "feeds", "intelligence"];
    const groups = new Map<string, WidgetTypeSpec[]>();
    specs.forEach((spec) => {
      const list = groups.get(spec.category) ?? [];
      list.push(spec);
      groups.set(spec.category, list);
    });
    return [...groups.entries()].sort(
      (a, b) => order.indexOf(a[0]) - order.indexOf(b[0]),
    );
  }, [specs]);
 
  return (
    <div
      className="fixed inset-0 z-50 flex items-center justify-center bg-black/40 p-4"
      onClick={onCancel}
    >
      <div
        className="w-full max-w-2xl max-h-[85vh] flex flex-col rounded-xl bg-white dark:bg-gray-800 shadow-xl"
        onClick={(e) => e.stopPropagation()}
      >
        <div className="flex items-center justify-between px-4 py-3 border-b border-gray-100 dark:border-gray-700">
          <h3 className="text-sm font-semibold text-gray-900 dark:text-white">
            Add a widget
          </h3>
          <button
            type="button"
            onClick={onCancel}
            className="rounded-lg p-1 text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-700"
          >
            <X className="w-4 h-4" />
          </button>
        </div>
 
        <div className="flex-1 overflow-auto px-4 py-3 space-y-5">
          {grouped.map(([category, items]) => (
            <div key={category}>
              <p className="text-xs font-semibold text-gray-900 dark:text-white">
                {CATEGORY_LABELS[category] ?? category}
              </p>
              <p className="text-[11px] text-gray-400 dark:text-gray-500 mb-2">
                {CATEGORY_BLURBS[category] ?? ""}
              </p>
 
              <div className="grid gap-2 sm:grid-cols-2">
                {items.map((spec) => {
                  const presenter = presenterFor(spec.type);
                  const Icon = presenter.icon;
                  const tone = TONES[presenter.tone];
                  return (
                    <button
                      key={spec.type}
                      type="button"
                      onClick={() => onPick(spec)}
                      className="flex items-start gap-2.5 rounded-lg border border-gray-200 dark:border-gray-700 p-2.5 text-left hover:border-gray-300 dark:hover:border-gray-600 hover:bg-gray-50 dark:hover:bg-gray-700/40 transition-colors"
                    >
                      <span className={`shrink-0 rounded-lg p-1.5 ${tone.bg}`}>
                        <Icon className={`w-4 h-4 ${tone.fg}`} />
                      </span>
                      <span className="min-w-0">
                        <span className="flex items-center gap-1.5">
                          <span className="text-xs font-medium text-gray-900 dark:text-white">
                            {spec.label}
                          </span>
                          {spec.data_kind === "generated" && (
                            <Sparkles
                              className="w-3 h-3 text-violet-500"
                              aria-label="AI generated"
                            />
                          )}
                          {spec.data_kind === "feed" && (
                            <Radio
                              className="w-3 h-3 text-sky-500"
                              aria-label="Live source"
                            />
                          )}
                        </span>
                        <span className="block text-[11px] leading-snug text-gray-500 dark:text-gray-400">
                          {spec.description}
                        </span>
                      </span>
                    </button>
                  );
                })}
              </div>
            </div>
          ))}
        </div>
      </div>
    </div>
  );
};
 
export default WidgetPicker;