All files / src/components/dashboard GenerateDashboard.tsx

90% Statements 36/40
88.37% Branches 38/43
87.5% Functions 14/16
92.3% Lines 36/39

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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333                                      3x       3x                     3x                                                                   3x                 30x 30x 30x     30x 14x 11x 11x       30x 3x 3x 3x     3x   3x     30x 52x     30x 5x 5x               9x                           30x             8x                                                     1x                     12x                                                                   1x                       2x                                 52x 52x 52x 52x 52x   52x                       3x                                                                                                                                                            
import {
  CreateDashboardBody,
  GenerationProposal,
  ProposedWidget,
} from "@/objects/Dashboard";
import {
  AlertTriangle,
  Database,
  Loader2,
  Radio,
  Sparkles,
  Wand2,
  X,
} from "lucide-react";
import React from "react";
import { TONES } from "@/components/appearance/tones";
import { presenterFor } from "./widgets/registry";
 
const field =
  "w-full rounded-lg border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-900 px-2.5 py-1.5 text-xs text-gray-900 dark:text-gray-100 focus:outline-none focus:ring-2 focus:ring-blue-500/40";
 
/** Examples that show the shape of a good description — specific about what
 *  to watch, not just a topic. */
const EXAMPLES = [
  "Monitor the market and news so I know what stocks to look at — prices for the big tech names, financial headlines, and a daily summary of what moved.",
  "Track how my agents are performing: spend, errors, latency, and a weekly comparison of which models give the best results.",
  "Follow UK AI regulation — official sources, the trade press, and a weekly digest of what changed.",
];
 
/** The same provenance vocabulary the tiles use, so review reads the way the
 *  finished board will. */
const KIND_BADGE: Record<
  string,
  { icon: React.ElementType; label: string; className: string }
> = {
  feed: {
    icon: Radio,
    label: "Live source",
    className: "text-sky-600 dark:text-sky-400",
  },
  generated: {
    icon: Sparkles,
    label: "AI generated",
    className: "text-violet-600 dark:text-violet-400",
  },
  platform: {
    icon: Database,
    label: "Your data",
    className: "text-gray-400 dark:text-gray-500",
  },
};
 
export interface GenerateDashboardProps {
  onCancel: () => void;
  onGenerate: (description: string) => void;
  onCreate: (body: CreateDashboardBody) => void;
  generating: boolean;
  creating: boolean;
  proposal: GenerationProposal | null;
  error: string | null;
}
 
/** Describe a board, review what the model proposed, then create it.
 *
 *  Review is not a formality: the user can drop any tile before anything is
 *  persisted, and each tile shows why it was included and whether its content
 *  will be fact or inference.
 */
const GenerateDashboard: React.FC<GenerateDashboardProps> = ({
  onCancel,
  onGenerate,
  onCreate,
  generating,
  creating,
  proposal,
  error,
}) => {
  const [description, setDescription] = React.useState("");
  const [name, setName] = React.useState("");
  const [dropped, setDropped] = React.useState<Set<number>>(new Set());
 
  // A fresh proposal replaces whatever was being reviewed.
  React.useEffect(() => {
    if (proposal) {
      setName(proposal.name);
      setDropped(new Set());
    }
  }, [proposal]);
 
  const toggle = (index: number) =>
    setDropped((current) => {
      const next = new Set(current);
      Iif (next.has(index)) {
        next.delete(index);
      } else {
        next.add(index);
      }
      return next;
    });
 
  const kept: ProposedWidget[] = (proposal?.widgets ?? []).filter(
    (_, index) => !dropped.has(index),
  );
 
  const create = () => {
    Iif (!proposal) return;
    onCreate({
      name: name.trim() || proposal.name,
      description: proposal.description,
      icon: proposal.icon,
      accent: proposal.accent,
      // What stamps the board as generated and resolves it back to this
      // invocation; without it a generated board read as hand-built.
      generation_id: proposal.generation_id,
      widgets: kept.map((widget) => ({
        widget_type: widget.widget_type,
        title: widget.title,
        config: widget.config,
        x: widget.x,
        y: widget.y,
        w: widget.w,
        h: widget.h,
        position: widget.position,
        refresh_interval_seconds: widget.refresh_interval_seconds,
      })),
    });
  };
 
  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-[88vh] flex flex-col rounded-xl bg-white dark:bg-gray-800 shadow-xl"
        onClick={(e) => e.stopPropagation()}
      >
        <div className="flex items-start justify-between gap-3 px-4 py-3 border-b border-gray-100 dark:border-gray-700">
          <div>
            <h3 className="flex items-center gap-1.5 text-sm font-semibold text-gray-900 dark:text-white">
              <Wand2 className="w-4 h-4 text-violet-500" />
              Describe a dashboard
            </h3>
            <p className="text-[11px] text-gray-400 dark:text-gray-500">
              Say what you want to watch. Nothing is created until you approve
              it.
            </p>
          </div>
          <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">
          <textarea
            className={`${field} min-h-[80px]`}
            placeholder="Monitor the market and news so I know what stocks to look at…"
            value={description}
            onChange={(e) => setDescription(e.target.value)}
            disabled={generating}
          />
 
          {!proposal && (
            <div className="mt-2">
              <p className="text-[11px] text-gray-400 dark:text-gray-500 mb-1">
                Try one of these:
              </p>
              <div className="space-y-1">
                {EXAMPLES.map((example) => (
                  <button
                    key={example}
                    type="button"
                    onClick={() => setDescription(example)}
                    className="block w-full rounded-lg border border-gray-100 dark:border-gray-700 px-2.5 py-1.5 text-left text-[11px] leading-snug text-gray-500 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-700/40"
                  >
                    {example}
                  </button>
                ))}
              </div>
            </div>
          )}
 
          {error && (
            <p className="mt-2 rounded-lg bg-red-50 dark:bg-red-900/20 px-3 py-2 text-[11px] text-red-600 dark:text-red-400">
              {error}
            </p>
          )}
 
          {generating && (
            <p className="mt-3 flex items-center gap-2 text-xs text-gray-500 dark:text-gray-400">
              <Loader2 className="w-3.5 h-3.5 animate-spin" />
              Designing your board — this takes a few moments.
            </p>
          )}
 
          {proposal && !generating && (
            <div className="mt-4 border-t border-gray-100 dark:border-gray-700 pt-3">
              <p className="mb-1 text-xs font-medium text-gray-700 dark:text-gray-200">
                Name
              </p>
              <input
                className={field}
                value={name}
                onChange={(e) => setName(e.target.value)}
              />
 
              {proposal.reasoning && (
                <p className="mt-2 text-[11px] italic leading-snug text-gray-500 dark:text-gray-400">
                  {proposal.reasoning}
                </p>
              )}
 
              {proposal.warnings.length > 0 && (
                <ul className="mt-2 space-y-1">
                  {proposal.warnings.map((warning, index) => (
                    <li
                      key={index}
                      className="flex items-start gap-1.5 rounded-lg bg-amber-50 dark:bg-amber-900/20 px-2.5 py-1.5 text-[11px] leading-snug text-amber-700 dark:text-amber-300"
                    >
                      <AlertTriangle className="w-3 h-3 mt-0.5 shrink-0" />
                      {warning}
                    </li>
                  ))}
                </ul>
              )}
 
              <p className="mt-3 mb-1.5 text-xs font-medium text-gray-700 dark:text-gray-200">
                {kept.length} of {proposal.widgets.length} widgets
              </p>
 
              <div className="space-y-1.5">
                {proposal.widgets.map((widget, index) => {
                  const presenter = presenterFor(widget.widget_type);
                  const Icon = presenter.icon;
                  const tone = TONES[presenter.tone];
                  const badge = KIND_BADGE[widget.data_kind];
                  const isDropped = dropped.has(index);
 
                  return (
                    <label
                      key={index}
                      className={`flex items-start gap-2.5 rounded-lg border p-2.5 cursor-pointer transition-colors ${
                        isDropped
                          ? "border-gray-100 dark:border-gray-700/50 opacity-50"
                          : "border-gray-200 dark:border-gray-700 hover:bg-gray-50 dark:hover:bg-gray-700/40"
                      }`}
                    >
                      <input
                        type="checkbox"
                        checked={!isDropped}
                        onChange={() => toggle(index)}
                        className="mt-1 shrink-0"
                      />
                      <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 flex-1">
                        <span className="flex items-center gap-1.5 flex-wrap">
                          <span className="text-xs font-medium text-gray-900 dark:text-white">
                            {widget.title}
                          </span>
                          {badge && (
                            <span
                              className={`inline-flex items-center gap-0.5 text-[10px] ${badge.className}`}
                            >
                              <badge.icon className="w-2.5 h-2.5" />
                              {badge.label}
                            </span>
                          )}
                        </span>
                        {widget.rationale && (
                          <span className="block text-[11px] leading-snug text-gray-500 dark:text-gray-400">
                            {widget.rationale}
                          </span>
                        )}
                        {widget.runs_on && (
                          <span className="block text-[11px] leading-snug text-gray-400 dark:text-gray-500">
                            Runs on {widget.runs_on}
                          </span>
                        )}
                      </span>
                    </label>
                  );
                })}
              </div>
            </div>
          )}
        </div>
 
        <div className="flex items-center justify-between gap-2 px-4 py-3 border-t border-gray-100 dark:border-gray-700">
          <span className="text-[10px] text-gray-400 dark:text-gray-500 truncate">
            {proposal ? `Designed by ${proposal.model.model_name}` : ""}
          </span>
 
          <span className="flex gap-2 shrink-0">
            <button
              type="button"
              onClick={onCancel}
              className="rounded-lg px-3 py-1.5 text-xs font-medium text-gray-600 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700"
            >
              Cancel
            </button>
            <button
              type="button"
              disabled={generating || description.trim().length < 20}
              onClick={() => onGenerate(description.trim())}
              className="rounded-lg border border-gray-200 dark:border-gray-700 px-3 py-1.5 text-xs font-medium text-gray-700 dark:text-gray-200 hover:bg-gray-50 dark:hover:bg-gray-700/50 disabled:opacity-50"
            >
              {proposal ? "Try again" : "Design it"}
            </button>
            {proposal && (
              <button
                type="button"
                disabled={creating || kept.length === 0}
                onClick={create}
                className="rounded-lg bg-gray-900 dark:bg-white px-3 py-1.5 text-xs font-medium text-white dark:text-gray-900 hover:opacity-80 disabled:opacity-50 transition-opacity"
              >
                {creating ? "Creating…" : `Create with ${kept.length}`}
              </button>
            )}
          </span>
        </div>
      </div>
    </div>
  );
};
 
export default GenerateDashboard;