CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
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 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 | /**
* Competitive Intelligence Engine — Admin UI.
*
* GET /admin/intelligence — dashboard (latest report per competitor)
* POST /admin/intelligence/scan — trigger a new scan (fire-and-forget)
* GET /admin/intelligence/:competitor — history for one competitor
*
* All routes gated by `isSiteAdmin`.
*/
import { Hono } from "hono";
import { softAuth } from "../middleware/auth";
import type { AuthEnv } from "../middleware/auth";
import { isSiteAdmin } from "../lib/admin";
import { Layout } from "../views/layout";
import {
runIntelligenceScan,
getLatestReports,
getReportHistory,
getLastScanRun,
COMPETITORS,
type CompetitorReport,
type GapIdentified,
type FeatureShipped,
} from "../lib/competitive-intel";
const competitiveIntel = new Hono<AuthEnv>();
competitiveIntel.use("*", softAuth);
// ---------------------------------------------------------------------------
// Auth gate helper (mirrors admin.tsx pattern)
// ---------------------------------------------------------------------------
async function gate(c: any): Promise<{ user: any } | Response> {
const user = c.get("user");
if (!user) return c.redirect("/login?next=/admin/intelligence");
if (!(await isSiteAdmin(user.id))) {
return c.html(
<Layout title="Forbidden" user={user}>
<div class="empty-state">
<h2>403 — Not a site admin</h2>
<p>You don't have permission to view this page.</p>
</div>
</Layout>,
403
);
}
return { user };
}
// ---------------------------------------------------------------------------
// Priority badge helper
// ---------------------------------------------------------------------------
function PriorityBadge({ priority }: { priority: "high" | "medium" | "low" }) {
const styles: Record<string, string> = {
high: "background:#f85149;color:#fff",
medium: "background:#d29922;color:#0d1117",
low: "background:#30363d;color:#8b949e",
};
return (
<span
style={`display:inline-block;padding:2px 8px;border-radius:3px;font-size:11px;font-weight:600;text-transform:uppercase;${styles[priority] ?? styles.low}`}
>
{priority}
</span>
);
}
// ---------------------------------------------------------------------------
// Competitor display name helper
// ---------------------------------------------------------------------------
function competitorName(id: string): string {
const c = COMPETITORS.find((c) => c.id === id);
return c ? c.name : id;
}
// ---------------------------------------------------------------------------
// Format a date string nicely
// ---------------------------------------------------------------------------
function fmtDate(d: string | Date | null | undefined): string {
if (!d) return "—";
try {
return new Date(d as string).toLocaleDateString("en-US", {
year: "numeric",
month: "short",
day: "numeric",
});
} catch {
return String(d);
}
}
// ---------------------------------------------------------------------------
// Compute gap priority counts across all reports
// ---------------------------------------------------------------------------
function countGapsByPriority(reports: CompetitorReport[]) {
let high = 0;
let medium = 0;
let low = 0;
for (const r of reports) {
const gaps = (r.gapsIdentified ?? []) as GapIdentified[];
for (const g of gaps) {
if (g.priority === "high") high++;
else if (g.priority === "medium") medium++;
else low++;
}
}
return { high, medium, low };
}
// ---------------------------------------------------------------------------
// GET /admin/intelligence — dashboard
// ---------------------------------------------------------------------------
competitiveIntel.get("/admin/intelligence", async (c) => {
const g = await gate(c);
if (g instanceof Response) return g;
const { user } = g;
const [reports, lastRun] = await Promise.all([
getLatestReports(),
getLastScanRun(),
]);
const scanStarted = c.req.query("scan") === "started";
const gapCounts = countGapsByPriority(reports);
// Map reports by competitor id for easy lookup
const reportMap = new Map<string, CompetitorReport>(
reports.map((r) => [r.competitor, r])
);
return c.html(
<Layout title="Competitive Intelligence — Admin" user={user}>
<div style="max-width:1100px;margin:0 auto;padding:24px 16px">
{/* Header */}
<div
style="display:flex;justify-content:space-between;align-items:flex-start;flex-wrap:wrap;gap:12px;margin-bottom:8px"
>
<div>
<h1 style="margin:0;font-size:22px;font-weight:700">
Competitive Intelligence
</h1>
<p style="color:var(--text-muted);font-size:13px;margin-top:4px">
Weekly gap analysis — what competitors are shipping vs. what
Gluecron has.
</p>
</div>
<div style="display:flex;gap:8px;align-items:center;flex-wrap:wrap">
<a href="/admin" class="btn btn-sm">
Back to Admin
</a>
<form method="post" action="/admin/intelligence/scan">
<button
type="submit"
class="btn btn-primary btn-sm"
onclick="this.disabled=true;this.textContent='Scanning…';this.form.submit()"
>
Run scan now
</button>
</form>
</div>
</div>
{/* Scan started banner */}
{scanStarted && (
<div
class="auth-success"
style="margin-bottom:16px;font-size:13px"
>
Scan started in the background. Refresh in a few minutes to see
updated reports.
</div>
)}
{/* Last scan status */}
{lastRun && (
<div
style="background:var(--bg-secondary);border:1px solid var(--border);border-radius:var(--radius);padding:10px 16px;margin-bottom:20px;font-size:13px;display:flex;gap:16px;flex-wrap:wrap;align-items:center"
>
<span>
<span style="color:var(--text-muted)">Last scan:</span>{" "}
{fmtDate(lastRun.startedAt as unknown as string)}
</span>
<span>
<span style="color:var(--text-muted)">Status:</span>{" "}
<span
style={
lastRun.status === "completed"
? "color:var(--green);font-weight:600"
: lastRun.status === "failed"
? "color:var(--red);font-weight:600"
: "color:var(--yellow);font-weight:600"
}
>
{lastRun.status}
</span>
</span>
<span>
<span style="color:var(--text-muted)">Reports created:</span>{" "}
{lastRun.competitorsScanned}
</span>
{lastRun.error && (
<span style="color:var(--red);font-size:12px">
{lastRun.error}
</span>
)}
</div>
)}
{/* Gap summary counts */}
<div
style="display:grid;grid-template-columns:repeat(auto-fit,minmax(140px,1fr));gap:12px;margin-bottom:24px"
>
<div
style="background:var(--bg-secondary);border:1px solid var(--border);border-radius:var(--radius);padding:14px;text-align:center"
>
<div style="font-size:26px;font-weight:700;color:var(--red)">
{gapCounts.high}
</div>
<div
style="font-size:11px;text-transform:uppercase;color:var(--text-muted);margin-top:2px"
>
High-priority gaps
</div>
</div>
<div
style="background:var(--bg-secondary);border:1px solid var(--border);border-radius:var(--radius);padding:14px;text-align:center"
>
<div style="font-size:26px;font-weight:700;color:var(--yellow)">
{gapCounts.medium}
</div>
<div
style="font-size:11px;text-transform:uppercase;color:var(--text-muted);margin-top:2px"
>
Medium-priority gaps
</div>
</div>
<div
style="background:var(--bg-secondary);border:1px solid var(--border);border-radius:var(--radius);padding:14px;text-align:center"
>
<div style="font-size:26px;font-weight:700;color:var(--text-muted)">
{gapCounts.low}
</div>
<div
style="font-size:11px;text-transform:uppercase;color:var(--text-muted);margin-top:2px"
>
Low-priority gaps
</div>
</div>
<div
style="background:var(--bg-secondary);border:1px solid var(--border);border-radius:var(--radius);padding:14px;text-align:center"
>
<div style="font-size:26px;font-weight:700">
{reports.length}
</div>
<div
style="font-size:11px;text-transform:uppercase;color:var(--text-muted);margin-top:2px"
>
Competitors tracked
</div>
</div>
</div>
{/* Competitor cards grid */}
{reports.length === 0 ? (
<div
style="background:var(--bg-secondary);border:1px solid var(--border);border-radius:var(--radius);padding:48px;text-align:center;color:var(--text-muted)"
>
<p style="font-size:15px;margin-bottom:8px">No reports yet.</p>
<p style="font-size:13px">
Click "Run scan now" to fetch the latest competitor changelogs and
analyse gaps with Claude.
</p>
</div>
) : (
<div
style="display:grid;grid-template-columns:repeat(auto-fill,minmax(340px,1fr));gap:16px"
>
{COMPETITORS.map((comp) => {
const report = reportMap.get(comp.id);
return (
<div
style="background:var(--bg-secondary);border:1px solid var(--border);border-radius:var(--radius);overflow:hidden;display:flex;flex-direction:column"
>
{/* Card header */}
<div
style="padding:14px 16px;border-bottom:1px solid var(--border);display:flex;justify-content:space-between;align-items:center"
>
<div>
<span style="font-weight:700;font-size:15px">
{comp.name}
</span>
{report && (
<span
style="margin-left:8px;font-size:12px;color:var(--text-muted)"
>
{fmtDate(report.reportDate as unknown as string)}
</span>
)}
</div>
<a
href={`/admin/intelligence/${comp.id}`}
style="font-size:12px;color:var(--text-link)"
>
History
</a>
</div>
{/* Card body */}
<div style="padding:14px 16px;flex:1;display:flex;flex-direction:column;gap:12px">
{!report ? (
<p style="font-size:13px;color:var(--text-muted)">
No report available. Run a scan to populate.
</p>
) : (
<>
{/* Summary */}
{report.summary && (
<p style="font-size:13px;color:var(--text-muted);line-height:1.55">
{report.summary}
</p>
)}
{/* Gaps — high priority first */}
{(report.gapsIdentified as GapIdentified[]).length >
0 && (
<div>
<div
style="font-size:11px;font-weight:600;text-transform:uppercase;color:var(--text-muted);margin-bottom:6px;letter-spacing:0.05em"
>
Gaps identified (
{
(report.gapsIdentified as GapIdentified[])
.length
}
)
</div>
<ul
style="list-style:none;display:flex;flex-direction:column;gap:6px"
>
{(report.gapsIdentified as GapIdentified[])
.sort((a, b) => {
const order = {
high: 0,
medium: 1,
low: 2,
};
return (
(order[a.priority] ?? 3) -
(order[b.priority] ?? 3)
);
})
.slice(0, 5)
.map((gap) => (
<li
style="display:flex;align-items:flex-start;gap:8px;font-size:13px"
>
<PriorityBadge priority={gap.priority} />
<span style="color:var(--text)">
{gap.feature}
</span>
</li>
))}
{(report.gapsIdentified as GapIdentified[])
.length > 5 && (
<li
style="font-size:12px;color:var(--text-muted)"
>
+
{(report.gapsIdentified as GapIdentified[])
.length - 5}{" "}
more —{" "}
<a
href={`/admin/intelligence/${comp.id}`}
style="color:var(--text-link)"
>
see all
</a>
</li>
)}
</ul>
</div>
)}
{/* Features shipped */}
{(report.featuresShipped as FeatureShipped[]).length >
0 && (
<div>
<div
style="font-size:11px;font-weight:600;text-transform:uppercase;color:var(--text-muted);margin-bottom:6px;letter-spacing:0.05em"
>
Features shipped (
{
(report.featuresShipped as FeatureShipped[])
.length
}
)
</div>
<ul
style="list-style:none;display:flex;flex-direction:column;gap:4px"
>
{(report.featuresShipped as FeatureShipped[])
.slice(0, 4)
.map((f) => (
<li style="font-size:13px;color:var(--text-muted)">
{f.url ? (
<a
href={f.url}
target="_blank"
rel="noopener noreferrer"
style="color:var(--text-link)"
>
{f.title}
</a>
) : (
<span style="color:var(--text)">
{f.title}
</span>
)}
</li>
))}
{(report.featuresShipped as FeatureShipped[])
.length > 4 && (
<li
style="font-size:12px;color:var(--text-muted)"
>
+
{(report.featuresShipped as FeatureShipped[])
.length - 4}{" "}
more
</li>
)}
</ul>
</div>
)}
</>
)}
</div>
{/* Card footer */}
<div
style="padding:10px 16px;border-top:1px solid var(--border);display:flex;justify-content:space-between;align-items:center"
>
<a
href={comp.changelogUrl}
target="_blank"
rel="noopener noreferrer"
style="font-size:12px;color:var(--text-muted)"
>
Changelog ↗
</a>
<a
href={`/admin/intelligence/${comp.id}`}
class="btn btn-sm"
style="font-size:12px"
>
View history
</a>
</div>
</div>
);
})}
</div>
)}
</div>
</Layout>
);
});
// ---------------------------------------------------------------------------
// POST /admin/intelligence/scan — trigger scan (fire-and-forget)
// ---------------------------------------------------------------------------
competitiveIntel.post("/admin/intelligence/scan", async (c) => {
const g = await gate(c);
if (g instanceof Response) return g;
// Fire-and-forget — do NOT await
runIntelligenceScan().catch((err) => {
console.error("[competitive-intel] background scan error:", err);
});
return c.redirect("/admin/intelligence?scan=started");
});
// ---------------------------------------------------------------------------
// GET /admin/intelligence/:competitor — history for one competitor
// ---------------------------------------------------------------------------
competitiveIntel.get("/admin/intelligence/:competitor", async (c) => {
const g = await gate(c);
if (g instanceof Response) return g;
const { user } = g;
const competitorId = c.req.param("competitor");
const knownCompetitor = COMPETITORS.find((x) => x.id === competitorId);
if (!knownCompetitor) {
return c.html(
<Layout title="Not Found" user={user}>
<div class="empty-state">
<h2>404</h2>
<p>Competitor not found: {competitorId}</p>
<a href="/admin/intelligence" style="margin-top:12px;display:inline-block">
Back to Intelligence
</a>
</div>
</Layout>,
404
);
}
const history = await getReportHistory(competitorId, 20);
const name = knownCompetitor.name;
return c.html(
<Layout title={`${name} — Intelligence History`} user={user}>
<div style="max-width:960px;margin:0 auto;padding:24px 16px">
{/* Header */}
<div
style="display:flex;justify-content:space-between;align-items:center;flex-wrap:wrap;gap:8px;margin-bottom:20px"
>
<div>
<h1 style="font-size:22px;font-weight:700;margin:0">
{name} — Intelligence History
</h1>
<p style="color:var(--text-muted);font-size:13px;margin-top:4px">
{history.length} report{history.length === 1 ? "" : "s"} on
record.{" "}
<a
href={knownCompetitor.changelogUrl}
target="_blank"
rel="noopener noreferrer"
style="color:var(--text-link)"
>
View live changelog ↗
</a>
</p>
</div>
<a href="/admin/intelligence" class="btn btn-sm">
Back to Dashboard
</a>
</div>
{/* Timeline */}
{history.length === 0 ? (
<div
style="background:var(--bg-secondary);border:1px solid var(--border);border-radius:var(--radius);padding:48px;text-align:center;color:var(--text-muted)"
>
<p>No reports for {name} yet. Run a scan to generate one.</p>
</div>
) : (
<div style="display:flex;flex-direction:column;gap:20px">
{history.map((report, idx) => {
const gaps = (report.gapsIdentified ?? []) as GapIdentified[];
const features = (
report.featuresShipped ?? []
) as FeatureShipped[];
const highGaps = gaps.filter((g) => g.priority === "high");
const medGaps = gaps.filter((g) => g.priority === "medium");
const lowGaps = gaps.filter((g) => g.priority === "low");
return (
<div
style="background:var(--bg-secondary);border:1px solid var(--border);border-radius:var(--radius);overflow:hidden"
>
{/* Report header */}
<div
style="padding:14px 16px;border-bottom:1px solid var(--border);display:flex;justify-content:space-between;align-items:center;flex-wrap:wrap;gap:8px"
>
<div style="display:flex;align-items:center;gap:12px">
<span
style="background:var(--accent);color:#fff;font-size:11px;font-weight:600;padding:2px 8px;border-radius:3px"
>
{idx === 0 ? "LATEST" : `#${idx + 1}`}
</span>
<span style="font-weight:600;font-size:15px">
Week of{" "}
{fmtDate(report.reportDate as unknown as string)}
</span>
</div>
<div
style="display:flex;gap:8px;font-size:12px;color:var(--text-muted)"
>
<span>
<span style="color:var(--red);font-weight:600">
{highGaps.length}
</span>{" "}
high
</span>
<span>
<span style="color:var(--yellow);font-weight:600">
{medGaps.length}
</span>{" "}
medium
</span>
<span>
<span style="color:var(--text-muted);font-weight:600">
{lowGaps.length}
</span>{" "}
low
</span>
<span>{features.length} features shipped</span>
</div>
</div>
{/* Report body */}
<div
style="padding:16px;display:grid;grid-template-columns:1fr 1fr;gap:20px"
>
{/* Left: summary + gaps */}
<div>
{report.summary && (
<div style="margin-bottom:14px">
<div
style="font-size:11px;font-weight:600;text-transform:uppercase;color:var(--text-muted);margin-bottom:6px;letter-spacing:0.05em"
>
Summary
</div>
<p
style="font-size:13px;color:var(--text-muted);line-height:1.6"
>
{report.summary}
</p>
</div>
)}
{gaps.length > 0 && (
<div>
<div
style="font-size:11px;font-weight:600;text-transform:uppercase;color:var(--text-muted);margin-bottom:8px;letter-spacing:0.05em"
>
Gaps identified ({gaps.length})
</div>
<ul
style="list-style:none;display:flex;flex-direction:column;gap:8px"
>
{[...highGaps, ...medGaps, ...lowGaps].map(
(gap) => (
<li style="font-size:13px">
<div
style="display:flex;align-items:flex-start;gap:8px;margin-bottom:2px"
>
<PriorityBadge priority={gap.priority} />
<span
style="color:var(--text);font-weight:500"
>
{gap.feature}
</span>
</div>
{gap.notes && (
<p
style="font-size:12px;color:var(--text-muted);margin-top:3px;padding-left:4px;line-height:1.5"
>
{gap.notes}
</p>
)}
</li>
)
)}
</ul>
</div>
)}
</div>
{/* Right: features shipped */}
<div>
{features.length > 0 && (
<div>
<div
style="font-size:11px;font-weight:600;text-transform:uppercase;color:var(--text-muted);margin-bottom:8px;letter-spacing:0.05em"
>
Features shipped ({features.length})
</div>
<ul
style="list-style:none;display:flex;flex-direction:column;gap:10px"
>
{features.map((f) => (
<li style="font-size:13px">
<div style="font-weight:500;color:var(--text);margin-bottom:2px">
{f.url ? (
<a
href={f.url}
target="_blank"
rel="noopener noreferrer"
style="color:var(--text-link)"
>
{f.title}
</a>
) : (
f.title
)}
</div>
{f.description && (
<p
style="font-size:12px;color:var(--text-muted);line-height:1.5"
>
{f.description}
</p>
)}
</li>
))}
</ul>
</div>
)}
{features.length === 0 && gaps.length === 0 && (
<p style="font-size:13px;color:var(--text-muted)">
No structured data extracted for this report.
</p>
)}
</div>
</div>
</div>
);
})}
</div>
)}
</div>
{/* Mobile: single-column layout for the report body grid */}
<style>{`
@media (max-width: 640px) {
.intel-report-body {
grid-template-columns: 1fr !important;
}
}
`}</style>
</Layout>
);
});
export default competitiveIntel;
|