All files / components/JobPanel ThisPageTab.tsx

80.02% Statements 577/721
90.19% Branches 92/102
70% Functions 14/20
80.02% Lines 577/721

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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 7221x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 3x 135x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x       139x 139x 4x 4x 4x 4x 2x 2x 2x 4x 4x 4x 4x 134x 134x 134x 134x 139x 3x 3x 131x 139x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 82x 139x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 58x 139x 2x 2x 2x 2x 2x 2x 2x 56x 139x 11x 11x 11x 11x 2x 2x 2x 2x 2x 2x 2x 2x 2x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 8x 8x 8x 8x 8x 8x 11x 11x 4x 4x 4x 4x 4x 4x 4x 11x 11x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 45x 139x 45x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 43x 43x 43x 45x 1x 1x 1x 45x 45x 45x 45x 1x 1x 1x 1x 45x 45x 3x 3x 3x 3x 3x 1x 1x 1x 3x 3x 3x 3x 3x 3x 45x 45x 45x 9x 9x 9x 9x 9x 9x 45x 45x 12x 12x 12x 12x 12x 12x 45x 45x       45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x                                                     45x 45x 2x 2x 2x 2x 2x 41x                       41x 45x 45x 45x 45x 45x 45x 45x 24x 19x 45x 45x 45x 45x 45x 3x 1x 1x 3x 3x 3x 45x 45x 45x 45x 45x       1x 1x 45x 45x 45x 45x 45x 43x 43x 43x 45x 45x 45x 45x 45x 45x 45x 45x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 43x 1x 44x 44x 44x 44x 44x 44x 44x 44x 44x 28x 28x 28x 44x 44x 45x 45x 45x 45x 28x 45x 45x 44x 45x 45x 45x 45x 28x 28x 28x 28x 28x 28x 56x 56x 56x 56x 56x 56x 28x 28x 28x 45x 45x 45x 45x 1x 72x 72x 72x 72x 72x 72x 72x 72x 1x 44x 44x 44x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 3x 4x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x                   3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                                                                                
import { useEffect, useRef, useState } from "react";
import { formatSalary } from "../../lib/formatSalary";
import { learnedSelectors } from "../../services/learnedSelectors";
import { pickerService } from "../../services/pickerService";
import { RegionList } from "./RegionList";
import type { ExtractionStrategy, JobAnalysis, JobPanelError, SavedJob } from "../../types/job";
import { ArrangementBadge } from "./ArrangementBadge";
import { FitScore } from "./FitScore";
import { useLiveFingerprint, useRescoreCostLine } from "./useFreshness";
import { confirmSkill } from "../../services/profileStorage";
import { freshness, rescoreAction, scoredByLabel } from "../../../shared/fitView";
import { UsageExhausted } from "../UsageExhausted";
import { PageAccessPrompt } from "./PageAccessPrompt";
 
/** Whether the panel can read the current tab (see jobFlow's checkPageAccess). */
export type PageAccessState = "unknown" | "granted" | "needs-click";
 
export type JobViewStatus = "idle" | "analyzing" | "ready" | "error";
 
export interface JobView {
  status: JobViewStatus;
  analysis: JobAnalysis | null;
  error: JobPanelError | null;
  fallback: Partial<JobAnalysis> | null;
  canonicalUrl: string | null;
  sourceUrl: string | null;
  multiplePostings: boolean;
  cached: boolean;
  saved: SavedJob | null;
  /** How this result was produced (spec FR-035) — drives the learned-region badge (FR-032). */
  strategy?: ExtractionStrategy;
  /** Present when `strategy` is "picked"/"selection" — offers the always-analyze prompt (FR-026b). */
  scopedRegion?: { selector: string; host: string };
  /** `undefined` = unknown, `null` = the account has no profile (006 R7). */
  currentProfileFingerprint?: string | null;
  profileUnavailable?: boolean;
  postingUnavailable?: boolean;
  /** A rescore failed; `analysis` is the stored one, still out of date. */
  rescoreError?: JobPanelError | null;
}
 
interface ThisPageTabProps {
  tabId: number | null;
  view: JobView;
  /** Drives the idle state: "needs-click" shows where to click instead of a button that cannot work. */
  pageAccess?: PageAccessState;
  saveError?: string | null;
  onAnalyze: () => void;
  onCancel: () => void;
  onForceAnalyze: () => void;
  onReanalyze?: () => void;
  /** Rescore against the current profile; the argument is the fit's own fingerprint. */
  onRescore?: (priorFingerprint: string | null) => void;
  onSave?: () => void;
  onExport?: () => void;
  onPruneArchived?: () => void;
  onUpgrade?: () => void;
  /** Opens the paste composer (spec FR-017e) — the fallback when the page can't be read. */
  onOpenPaste?: () => void;
  /** A region was committed via the picker or the panel's keyboard list (spec FR-026). */
  onRegionCommitted?: (result: { text: string; selector: string }) => void;
}
 
export function ThisPageTab({
  tabId,
  view,
  pageAccess = "unknown",
  saveError,
  onAnalyze,
  onCancel,
  onForceAnalyze,
  onReanalyze,
  onRescore,
  onSave,
  onExport,
  onPruneArchived,
  onUpgrade,
  onOpenPaste,
  onRegionCommitted,
}: ThisPageTabProps) {
  // FR-023: reachable from a failed extraction AND from a successful-but-
  // wrong one. Both scoping mechanisms need page access, so they are not
  // offered while the panel is asking for it (PageAccessPrompt) — one click
  // on the toolbar icon makes them work.
  const [regionScopingOpen, setRegionScopingOpen] = useState(false);
  const regionButtonRef = useRef<HTMLButtonElement>(null);
 
  const liveFingerprint = useLiveFingerprint(view.currentProfileFingerprint);
  // "I have this" changes the profile here, so the new fingerprint applies at
  // once rather than waiting on the session-storage event. A later profile
  // edit elsewhere replaces it.
  const [confirmedFingerprint, setConfirmedFingerprint] = useState<{
    base: string | null | undefined;
    value: string | null;
  } | null>(null);
  const currentFingerprint =
    confirmedFingerprint && confirmedFingerprint.base === liveFingerprint
      ? confirmedFingerprint.value
      : liveFingerprint;
  const fit = view.analysis?.fit ?? null;
  const fitFreshness = freshness(fit, currentFingerprint);
  const unavailable = Boolean(
    view.postingUnavailable || view.saved?.unavailableAt
  );
  const action = rescoreAction(
    {
      source: view.saved?.source ?? "url",
      sourceUrl: view.sourceUrl ?? "",
      saveKey: view.canonicalUrl ?? "",
      unavailableAt: view.saved?.unavailableAt,
    },
    fitFreshness,
    "panel"
  );
  const costLine = useRescoreCostLine(action.kind !== "none");
 
  const closeRegionScoping = (): void => {
    setRegionScopingOpen(false);
    regionButtonRef.current?.focus();
  };
 
  if (regionScopingOpen && tabId !== null) {
    return (
      <RegionScoping
        tabId={tabId}
        onCommit={(result) => {
          setRegionScopingOpen(false);
          onRegionCommitted?.(result);
        }}
        onCancel={closeRegionScoping}
      />
    );
  }
 
  // Without an activeTab grant an in-panel button can never start an
  // analysis (Chrome grants it only for a toolbar-icon click or its
  // shortcut), so the idle state shows where to click instead.
  if (view.status === "idle" && pageAccess === "needs-click") {
    return <PageAccessPrompt variant="needs-click" onOpenPaste={onOpenPaste} />;
  }
 
  if (view.status === "idle") {
    return (
      <div className="flex flex-col items-center justify-center px-6 py-16 text-center">
        <p className="text-sm font-medium text-gray-600 dark:text-gray-400">
          Analyze the current page as a job posting
        </p>
        <button
          onClick={onAnalyze}
          className="mt-4 rounded-lg bg-blue-600 px-4 py-2 text-sm font-semibold text-white transition-colors hover:bg-blue-700"
        >
          Analyze this page
        </button>
      </div>
    );
  }
 
  if (view.status === "analyzing") {
    return (
      <div
        role="status"
        aria-label="Analyzing page, please wait"
        aria-live="polite"
        className="flex flex-col items-center justify-center px-4 py-16"
      >
        <div
          className="h-10 w-10 animate-spin rounded-full border-[3px] border-gray-200 border-t-blue-600 dark:border-gray-700 dark:border-t-blue-500"
          aria-hidden="true"
        />
        <p className="mt-4 text-sm text-gray-500 dark:text-gray-400">Analyzing page…</p>
        <button
          onClick={onCancel}
          className="mt-4 rounded-md px-3 py-1 text-xs font-medium text-gray-500 hover:bg-gray-100 hover:text-gray-700 dark:text-gray-400 dark:hover:bg-gray-800"
        >
          Cancel
        </button>
      </div>
    );
  }
 
  if (view.status === "error" && view.error?.code === "page-access") {
    return (
      <PageAccessPrompt
        variant={view.error.pageAccess ?? "needs-click"}
        onOpenPaste={onOpenPaste}
      />
    );
  }
 
  if (view.status === "error" && view.error) {
    // The exhausted state is a designed card, never the generic error
    // banner (FR-009/SC-003) — saved jobs/profile/history are unaffected,
    // since this branch only ever replaces the "This Page" analyze result.
    if (view.error.code === "usage-limit-reached" && view.error.usage) {
      return (
        <div className="p-4">
          <UsageExhausted
            usage={view.error.usage}
            onUpgrade={onUpgrade ?? (() => chrome.runtime.openOptionsPage())}
          />
        </div>
      );
    }
 
    // Action hierarchy (FR-007b, T090): Retry is primary (a filled button,
    // free and needs no further input); "Analyze a region" is secondary (an
    // outlined button — it needs the user to choose something); a paste
    // shortcut is the quietest action — a plain-text link, not a button —
    // since paste is the remedy for pages the other actions cannot fix (a
    // login wall, a bot check), not a first response to an ordinary parsing
    // miss. Unreadable pages never reach here — see PageAccessPrompt above.
    const showScopingAndPaste = view.error.code === "thin-content";
 
    return (
      <div className="space-y-4 p-4">
        <div className="rounded-xl border border-red-200 bg-red-50 p-4 dark:border-red-900/50 dark:bg-red-950/30">
          <p className="text-sm font-semibold text-red-800 dark:text-red-400">
            {view.error.message}
          </p>
          <p className="mt-1 text-sm text-red-700 dark:text-red-500">
            {view.error.action}
          </p>
          <div className="mt-3 flex items-center gap-3">
            {view.error.retryable && (
              <button
                onClick={onAnalyze}
                className="rounded-md bg-red-600 px-3 py-1.5 text-xs font-semibold text-white hover:bg-red-700"
              >
                Retry
              </button>
            )}
            {showScopingAndPaste && (
              <button
                ref={regionButtonRef}
                onClick={() => setRegionScopingOpen(true)}
                className="rounded-md border border-red-300 px-3 py-1.5 text-xs font-semibold text-red-700 hover:bg-red-100 disabled:cursor-not-allowed disabled:opacity-40 dark:border-red-800 dark:text-red-300 dark:hover:bg-red-950/40"
              >
                Analyze a region
              </button>
            )}
            {showScopingAndPaste && onOpenPaste && (
              <button
                onClick={onOpenPaste}
                className="text-xs font-medium text-red-700 underline hover:text-red-800 dark:text-red-400 dark:hover:text-red-300"
              >
                Paste text instead
              </button>
            )}
          </div>
        </div>
        {view.fallback && (
          <section aria-label="Fields extracted from page data">
            <h2 className="mb-2 text-xs font-semibold uppercase tracking-wide text-gray-400 dark:text-gray-500">
              From page data (analysis unavailable)
            </h2>
            <AnalysisFields analysis={view.fallback} />
          </section>
        )}
      </div>
    );
  }
 
  if (view.status === "ready" && view.analysis) {
    if (!view.analysis.isJobPosting) {
      return (
        <div className="p-4">
          <div className="rounded-xl border border-amber-200 bg-amber-50 p-4 dark:border-amber-900/50 dark:bg-amber-950/30">
            <p className="text-sm font-semibold text-amber-800 dark:text-amber-400">
              This doesn&apos;t look like a job posting
            </p>
            <p className="mt-1 text-sm text-amber-700 dark:text-amber-500">
              You can analyze it anyway if this really is a job posting.
            </p>
            <button
              onClick={onForceAnalyze}
              className="mt-3 rounded-md bg-amber-600 px-3 py-1.5 text-xs font-semibold text-white hover:bg-amber-700"
            >
              Analyze anyway
            </button>
          </div>
        </div>
      );
    }
 
    return (
      <div className="space-y-4 p-4">
        {view.strategy === "learned-selector" && view.canonicalUrl && (
          <p className="text-xs font-medium text-blue-600 dark:text-blue-400">
            Always analyzing this region on {hostOf(view.canonicalUrl)}
          </p>
        )}
        {(view.strategy === "picked" || view.strategy === "selection") &&
          view.scopedRegion && <AlwaysAnalyzeOffer scopedRegion={view.scopedRegion} />}
        {view.multiplePostings && (
          <p className="rounded-lg bg-gray-100 px-3 py-2 text-xs text-gray-600 dark:bg-gray-800 dark:text-gray-400">
            Multiple postings were found on this page; the first one was
            analyzed. A page for a single posting yields better results.
          </p>
        )}
        {view.saved && (
          <div className="rounded-xl border border-green-200 bg-green-50 p-3 dark:border-green-900/50 dark:bg-green-950/30">
            <p className="text-xs font-semibold text-green-800 dark:text-green-300">
              Already saved · status: {view.saved.status}
            </p>
            {view.saved.notes && (
              <p className="mt-1 whitespace-pre-wrap text-xs text-green-700 dark:text-green-400">
                {view.saved.notes}
              </p>
            )}
            <p className="mt-1 text-[10px] text-green-600/80 dark:text-green-500/80">
              Saved {new Date(view.saved.savedAt).toLocaleDateString()} — edit in
              the Saved tab.
            </p>
          </div>
        )}
        <div className="flex items-center gap-2">
          {onSave && !view.saved && (
            <button
              onClick={onSave}
              className="rounded-md bg-blue-600 px-3 py-1.5 text-xs font-semibold text-white transition-colors hover:bg-blue-700"
            >
              Save
            </button>
          )}
          {onReanalyze && (
            <button
              onClick={onReanalyze}
              className="rounded-md px-3 py-1.5 text-xs font-medium text-gray-500 hover:bg-gray-100 hover:text-gray-700 dark:text-gray-400 dark:hover:bg-gray-800"
            >
              Re-analyze
            </button>
          )}
          {view.cached && !view.saved && (
            <span className="text-[10px] text-gray-400 dark:text-gray-500">
              from cache
            </span>
          )}
          {/* FR-023: reachable from a successful-but-wrong result, too — the
              extraction can succeed and still have picked the wrong part of
              the page. */}
          <button
            ref={regionButtonRef}
            onClick={() => setRegionScopingOpen(true)}
            disabled={tabId === null}
            className="ml-auto text-xs font-medium text-gray-500 underline hover:text-gray-700 disabled:cursor-not-allowed disabled:opacity-40 dark:text-gray-400 dark:hover:text-gray-200"
          >
            Wrong region? Analyze a region
          </button>
        </div>
        {saveError && (
          <div
            role="alert"
            className="rounded-xl border border-amber-200 bg-amber-50 p-3 dark:border-amber-900/50 dark:bg-amber-950/30"
          >
            <p className="text-xs font-semibold text-amber-800 dark:text-amber-400">
              {saveError}
            </p>
            <div className="mt-2 flex gap-2">
              {onExport && (
                <button
                  onClick={onExport}
                  className="rounded-md bg-amber-600 px-2.5 py-1 text-xs font-semibold text-white hover:bg-amber-700"
                >
                  Export library
                </button>
              )}
              {onPruneArchived && (
                <button
                  onClick={onPruneArchived}
                  className="rounded-md px-2.5 py-1 text-xs font-semibold text-amber-800 hover:bg-amber-100 dark:text-amber-300 dark:hover:bg-amber-900/40"
                >
                  Prune archived
                </button>
              )}
            </div>
          </div>
        )}
        {view.rescoreError?.code === "usage-limit-reached" &&
        view.rescoreError.usage ? (
          <UsageExhausted
            usage={view.rescoreError.usage}
            onUpgrade={onUpgrade ?? (() => chrome.runtime.openOptionsPage())}
          />
        ) : view.rescoreError ? (
          <div
            role="alert"
            className="rounded-xl border border-amber-200 bg-amber-50 p-3 dark:border-amber-900/50 dark:bg-amber-950/30"
          >
            <p className="text-xs font-semibold text-amber-800 dark:text-amber-400">
              {view.rescoreError.message}
            </p>
            <p className="mt-0.5 text-xs text-amber-700 dark:text-amber-500">
              {view.rescoreError.action}
            </p>
          </div>
        ) : null}
        <FitScore
          fit={view.analysis.fit}
          freshness={fitFreshness}
          action={action}
          costLine={costLine}
          onRescore={
            onRescore
              ? () => onRescore(view.analysis?.fit?.profileFingerprint ?? null)
              : undefined
          }
          unavailable={unavailable}
          profileUnavailable={view.profileUnavailable}
          scoredBy={scoredByLabel(view.analysis)}
          onConfirmSkill={async (label) => {
            const saved = await confirmSkill(label);
            setConfirmedFingerprint({
              base: liveFingerprint,
              value: saved.fingerprint ?? null,
            });
          }}
        />
        <AnalysisFields analysis={view.analysis} />
      </div>
    );
  }

  return null;
}
 
export function AnalysisFields({ analysis }: { analysis: Partial<JobAnalysis> }) {
  return (
    <div className="space-y-3">
      {(analysis.title || analysis.company) && (
        <div>
          {analysis.title && (
            <h2 className="text-base font-semibold text-gray-800 dark:text-gray-100">
              {analysis.title}
            </h2>
          )}
          <p className="text-sm text-gray-500 dark:text-gray-400">
            {[analysis.company, analysis.location].filter(Boolean).join(" — ")}
          </p>
        </div>
      )}
 
      {analysis.arrangement && (
        <div className="space-y-1.5">
          <div className="flex items-center gap-2">
            <ArrangementBadge
              arrangement={analysis.arrangement}
              daysInOffice={analysis.daysInOffice ?? null}
              daysRemote={analysis.daysRemote ?? null}
              alsoOffers={analysis.alsoOffers}
            />
            {analysis.arrangement !== "unspecified" &&
              analysis.arrangementConfidence &&
              analysis.arrangementConfidence !== "none" && (
                <span className="text-xs text-gray-400 dark:text-gray-500">
                  {analysis.arrangementConfidence === "explicit"
                    ? "stated in posting"
                    : "inferred"}
                </span>
              )}
          </div>
          {analysis.arrangementEvidence && (
            <blockquote className="border-l-2 border-gray-300 pl-2 text-xs italic text-gray-500 dark:border-gray-700 dark:text-gray-400">
              “{analysis.arrangementEvidence}”
            </blockquote>
          )}
          {analysis.remoteRestrictions && (
            <p className="text-xs text-gray-500 dark:text-gray-400">
              Remote restrictions: {analysis.remoteRestrictions}
            </p>
          )}
        </div>
      )}
 
      <dl className="space-y-1.5 text-sm">
        {analysis.salary && (
          <Field label="Salary" value={formatSalary(analysis.salary)} />
        )}
        {analysis.seniority && analysis.seniority !== "unspecified" && (
          <Field label="Seniority" value={capitalize(analysis.seniority)} />
        )}
      </dl>
 
      {analysis.techStack && analysis.techStack.length > 0 && (
        <div>
          <h3 className="mb-1.5 text-xs font-semibold uppercase tracking-wide text-gray-400 dark:text-gray-500">
            Tech stack
          </h3>
          <ul aria-label="Tech stack" className="flex flex-wrap gap-1.5">
            {analysis.techStack.map((tech) => (
              <li
                key={tech}
                className="rounded-md bg-gray-100 px-2 py-0.5 text-xs text-gray-700 dark:bg-gray-800 dark:text-gray-300"
              >
                {tech}
              </li>
            ))}
          </ul>
        </div>
      )}
    </div>
  );
}
 
function Field({ label, value }: { label: string; value: string }) {
  return (
    <div className="flex gap-2">
      <dt className="w-20 shrink-0 text-gray-400 dark:text-gray-500">{label}</dt>
      <dd className="text-gray-700 dark:text-gray-200">{value}</dd>
    </div>
  );
}
 
function capitalize(value: string): string {
  return value.charAt(0).toUpperCase() + value.slice(1);
}
 
function hostOf(url: string): string {
  try {
    return new URL(url).hostname;
  } catch {
    return url;
  }
}
 
/**
 * Offered after a successful scoped analysis, never imposed by the act of
 * scoping (spec FR-026b) — a one-off correction on an unusual posting must
 * not silently teach a selector that misapplies to every posting on the
 * host. Accepting writes straight to `learnedSelectors` (chrome.storage.local
 * is reachable from any extension context; no background round-trip needed)
 * and shows a confirmation naming the site — declining leaves nothing
 * learned and shows nothing further.
 */
function AlwaysAnalyzeOffer({
  scopedRegion,
}: {
  scopedRegion: { selector: string; host: string };
}) {
  const [state, setState] = useState<"offering" | "accepted" | "declined">("offering");
 
  if (state === "declined") return null;
 
  if (state === "accepted") {
    return (
      <p className="rounded-lg bg-blue-50 px-3 py-2 text-xs font-medium text-blue-700 dark:bg-blue-950/40 dark:text-blue-300">
        Will always analyze this region on {scopedRegion.host}.
      </p>
    );
  }
 
  return (
    <div className="flex items-center justify-between gap-2 rounded-lg bg-gray-100 px-3 py-2 dark:bg-gray-800">
      <p className="text-xs text-gray-600 dark:text-gray-400">
        Always analyze this region on {scopedRegion.host}?
      </p>
      <div className="flex shrink-0 gap-2">
        <button
          onClick={() => {
            void learnedSelectors.put(scopedRegion.host, scopedRegion.selector);
            setState("accepted");
          }}
          className="rounded-md bg-blue-600 px-2 py-1 text-xs font-semibold text-white hover:bg-blue-700"
        >
          Always
        </button>
        <button
          onClick={() => setState("declined")}
          className="rounded-md px-2 py-1 text-xs font-medium text-gray-500 hover:bg-gray-200 dark:text-gray-400 dark:hover:bg-gray-700"
        >
          Not now
        </button>
      </div>
    </div>
  );
}
 
interface RegionScopingProps {
  tabId: number;
  onCommit: (result: { text: string; selector: string }) => void;
  onCancel: () => void;
}
 
/**
 * FR-023/FR-024/FR-024a: both scoping mechanisms are reachable from this one
 * entry point. The keyboard list is the default surface — FR-024a forbids
 * gating the keyboard path behind the pointer one — with the pointer
 * overlay offered as an alternative rather than run at the same time: both
 * are live, page-side, promise-resolving sessions (`pickerService`'s
 * pointer pick holds exclusive page listeners until it resolves), and
 * running them together would leave whichever one the user didn't use
 * dangling with no way to tear it down from here. This toggle keeps exactly
 * one session live while keeping both reachable without leaving the entry
 * point.
 */
function RegionScoping({ tabId, onCommit, onCancel }: RegionScopingProps) {
  const [mode, setMode] = useState<"list" | "pointer">("list");
 
  if (mode === "pointer") {
    return (
      <PointerScoping
        tabId={tabId}
        onCommit={onCommit}
        onUseList={() => setMode("list")}
        onCancel={onCancel}
      />
    );
  }
 
  return (
    <div className="flex h-full flex-col">
      <div className="min-h-0 flex-1">
        <RegionList tabId={tabId} onCommit={onCommit} onCancel={onCancel} />
      </div>
      <div className="shrink-0 border-t border-gray-200/70 px-3 py-2 text-center dark:border-gray-800">
        <button
          onClick={() => setMode("pointer")}
          className="text-xs font-medium text-blue-600 underline hover:text-blue-700 dark:text-blue-400"
        >
          Use the pointer on the page instead
        </button>
      </div>
    </div>
  );
}
 
/**
 * The pointer path (FR-024, FR-024c, FR-025): the page itself shows the
 * highlight and cursor-adjacent character count (pickerService.startPointerPick,
 * T100), so this surface is mostly a status line plus the two ways out. A
 * "no region found there" outcome is not a dismissal — it re-starts the same
 * pointer session so the user can just click again, rather than dropping
 * them out of pointer mode on a miss (T101 also lives in pickerService: the
 * page's own cleanup runs on every dismissal already).
 */
function PointerScoping({
  tabId,
  onCommit,
  onUseList,
  onCancel,
}: {
  tabId: number;
  onCommit: (result: { text: string; selector: string }) => void;
  onUseList: () => void;
  onCancel: () => void;
}) {
  const [notFound, setNotFound] = useState(false);
  const onCommitRef = useRef(onCommit);
  onCommitRef.current = onCommit;
  const onCancelRef = useRef(onCancel);
  onCancelRef.current = onCancel;

  useEffect(() => {
    let cancelled = false;
    void (async () => {
      while (!cancelled) {
        const outcome = await pickerService.startPointerPick(tabId);
        if (cancelled) return;
        if (outcome.ok) {
          onCommitRef.current({ text: outcome.text, selector: outcome.selector });
          return;
        }
        if (outcome.reason === "dismissed") {
          onCancelRef.current();
          return;
        }
        setNotFound(true);
      }
    })();
    // The pointer session may still be pending when this unmounts (the
    // buttons below exit via cancelPointerPick too, but a parent-driven
    // unmount — e.g. switching tabs — would not otherwise reach the page at
    // all). Harmless if the session already resolved on its own.
    return () => {
      cancelled = true;
      void pickerService.cancelPointerPick(tabId);
    };
  }, [tabId]);

  // Exits FROM THE PANEL never send an Escape keydown to the page, so the
  // still-pending pointer promise needs an explicit external cancel or it
  // would never resolve — the page would be stuck in crosshair mode with
  // live listeners (see pickerService's cancelPointerPick).
  const exitToList = (): void => {
    void pickerService.cancelPointerPick(tabId);
    onUseList();
  };
  const exitToCancel = (): void => {
    void pickerService.cancelPointerPick(tabId);
    onCancel();
  };

  return (
    <div className="flex h-full flex-col items-center justify-center gap-3 p-6 text-center">
      <p className="text-sm font-medium text-gray-600 dark:text-gray-400">
        Point at a region on the page and click it.
      </p>
      <p className="text-xs text-gray-400 dark:text-gray-500">
        Press Escape on the page to cancel.
      </p>
      {notFound && (
        <p role="alert" className="text-xs font-semibold text-amber-700 dark:text-amber-400">
          No region was found there. Try clicking directly on the posting text.
        </p>
      )}
      <div className="flex gap-4">
        <button
          onClick={exitToList}
          className="text-xs font-medium text-blue-600 underline hover:text-blue-700 dark:text-blue-400"
        >
          Use the list instead
        </button>
        <button
          onClick={exitToCancel}
          className="text-xs font-medium text-gray-500 hover:text-gray-700 dark:text-gray-400"
        >
          Cancel
        </button>
      </div>
    </div>
  );
}