File size: 15,871 Bytes
935d7f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c43695
935d7f2
5c43695
 
 
 
 
 
935d7f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c43695
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
935d7f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c43695
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
935d7f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
019606d
 
e384183
 
 
019606d
e384183
 
 
 
 
 
 
935d7f2
 
 
 
 
 
 
019606d
935d7f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e384183
019606d
e384183
 
019606d
 
e384183
 
019606d
 
935d7f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c43695
 
 
 
 
 
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
/**
 * PlatformOverviewCard β€” macro view across ALL users in the window.
 *
 * Layout:
 *   β”Œβ”€ PLATFORM OVERVIEW Β· last 30 days ──────────────────────────┐
 *   β”‚  [7 users] [261 turns] [21 topics] [16 strategies]          β”‚
 *   β”‚                                                             β”‚
 *   β”‚  β”Œβ”€β”€β”€β”€ Top topics ────┐  β”Œβ”€β”€β”€β”€ Top strategies ──────┐       β”‚
 *   β”‚  β”‚ roth_ira  β–ˆβ–ˆβ–ˆβ–ˆ 3   β”‚  β”‚ decision_card  0.83 β–“β–“β–“ β”‚       β”‚
 *   β”‚  β”‚ compound  β–ˆβ–ˆβ–ˆ  2   β”‚  β”‚ comparison_t.  0.73 β–“β–“  β”‚       β”‚
 *   β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜       β”‚
 *   β”‚                                                             β”‚
 *   β”‚  β”Œβ”€ Decision-stage funnel ─┐  β”Œβ”€ Readiness ─┐               β”‚
 *   β”‚  β”‚ Action-ready  β–“β–“   2    β”‚  β”‚ Ready    2   β”‚               β”‚
 *   β”‚  β”‚ Evaluation    β–“    1    β”‚  β”‚ Likely   1   β”‚               β”‚
 *   β”‚  β”‚ Exploration   β–“β–“   2    β”‚  β”‚ Nurture  2   β”‚               β”‚
 *   β”‚  β”‚ Awareness     β–“β–“   2    β”‚  β”‚ Too early 2  β”‚               β”‚
 *   β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜               β”‚
 *   β”‚                                                             β”‚
 *   β”‚  Intent mix: Comparison 27% Β· Decision 21% Β· …              β”‚
 *   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
 *
 * No per-user identifiers β€” only aggregates. Pairs with the single-user
 * Inspect view below.
 */
import InfoHint from "./InfoHint.jsx";
import MiniLineChart from "./MiniLineChart.jsx";

export default function PlatformOverviewCard({
  data,
  windowLabel = "",
  platformDailySeries = null,   // [{date, total_turns, unique_users}]
  topicsDailySeries   = null,   // [{topic, series: [{date, count}]}]
}) {
  if (!data) return null;

  const maxTopicUsers = Math.max(...(data.by_topic || []).map((t) => t.total_users), 1);
  const maxStratPulls = Math.max(...(data.by_strategy || []).map((s) => s.total_pulls), 1);
  const maxStageCount = Math.max(...(data.stage_funnel || []).map((s) => s.count), 1);
  const maxReadyCount = Math.max(...(data.readiness_funnel || []).map((r) => r.count), 1);

  return (
    <div className="platform-card">
      <div className="platform-head">
        <div>
          <span className="profile-eyebrow">Platform overview</span>
          <h2>
            All users <span className="window-tag">{windowLabel}</span>
          </h2>
        </div>
        <div className="platform-numbers">
          <PlatformNumber value={data.total_active_users} label="Active users" />
          <PlatformNumber value={data.total_turns} label="Turns" />
          <PlatformNumber value={data.total_topics} label="Topics" />
          <PlatformNumber value={data.total_strategies} label="Strategies pulled" />
        </div>
      </div>

      {/* Activity trend chart β€” daily turns across the window */}
      {platformDailySeries && platformDailySeries.length > 0 && (
        <div className="trend-tile">
          <div className="trend-tile-head">
            <span>
              Activity trend
              <InfoHint width={300}>
                Total <strong>turns per day</strong> across all users in this window.
                Hover any point to see the exact count. Rising = engagement growing;
                falling = users dropping off. This is the simplest gut-check for product health.
              </InfoHint>
            </span>
            <span className="trend-tile-totals">
              {sum(platformDailySeries.map((d) => d.total_turns))} turns total
            </span>
          </div>
          <MiniLineChart
            data={platformDailySeries.map((d) => ({ date: d.date, value: d.total_turns }))}
            width={640}
            height={88}
            color="#d76a35"
            yLabel="turns/day"
            formatValue={(v) => `${v} turn${v === 1 ? "" : "s"}`}
          />
        </div>
      )}

      {/* Two-column: top topics + top strategies */}
      <div className="platform-cols-2">
        <div className="platform-block">
          <div className="platform-block-head">
            Top topics by user reach
            <InfoHint width={320}>
              Ranked by <strong>unique users</strong> then sum of interest_score.
              The bar shows user reach; the sub-text shows avg interest and turn volume.
              "1 user" cells are weaker evidence than "3 users" cells with the same score.
            </InfoHint>
          </div>
          <ol className="platform-ranklist">
            {(data.by_topic || []).map((t) => (
              <li key={t.topic}>
                <div className="rank-row">
                  <span className="rank-name">
                    <span className="topic-chip">{t.topic}</span>
                  </span>
                  <span className="rank-bar-wrap">
                    <span
                      className="rank-bar"
                      style={{ width: `${(t.total_users / maxTopicUsers) * 100}%` }}
                    />
                  </span>
                  <span className="rank-num">{t.total_users}<span className="rank-num-unit"> users</span></span>
                </div>
                <div className="rank-sub">
                  avg interest {fmt(t.avg_interest_score)} Β· {t.turns_in_window} turns Β· ΞΌ-reward {fmt(t.avg_reward)}
                </div>
              </li>
            ))}
            {(data.by_topic || []).length === 0 && (
              <li className="rank-empty">No topic data in window.</li>
            )}
          </ol>
          {/* Per-topic trend chart β€” one line per top-N topic */}
          {topicsDailySeries && topicsDailySeries.length > 0 && (
            <div className="trend-tile">
              <div className="trend-tile-head">
                <span>
                  Topic trends
                  <InfoHint width={320}>
                    Daily turns for the top {topicsDailySeries.length} topics over this window.
                    Hover to compare same-day counts. Use this to spot topics
                    <strong> heating up</strong> (rising line) vs <strong>cooling off</strong>
                    (falling line) β€” instruction or outreach focus often follows.
                  </InfoHint>
                </span>
              </div>
              <MiniLineChart
                series={topicsDailySeries.map((t) => ({
                  name:   t.topic,
                  points: (t.series || []).map((p) => ({ date: p.date, value: p.count })),
                }))}
                width={640}
                height={120}
                showLegend
                formatValue={(v) => `${v} turn${v === 1 ? "" : "s"}`}
              />
            </div>
          )}
        </div>

        <div className="platform-block">
          <div className="platform-block-head">
            Top strategies (popularity Γ— reward)
            <InfoHint width={340}>
              Strategies ranked by <code>avg_reward Γ— log(pulls + 1)</code> β€” rewards
              that have been validated by volume. The big number is ΞΌ-reward
              (average normalized_reward in [-1, +1]); a strategy with ΞΌ=0.83 over
              40 pulls is more trustworthy than one with ΞΌ=0.95 over 2.
            </InfoHint>
          </div>
          <ol className="platform-ranklist">
            {(data.by_strategy || []).map((s) => (
              <li key={s.strategy}>
                <div className="rank-row">
                  <span className="rank-name">{prettyName(s.strategy)}</span>
                  <span className="rank-bar-wrap">
                    <span
                      className="rank-bar rank-bar-strategy"
                      style={{ width: `${(s.total_pulls / maxStratPulls) * 100}%` }}
                    />
                  </span>
                  <span className="rank-num">
                    {fmt(s.avg_reward)}<span className="rank-num-unit"> ΞΌ</span>
                  </span>
                </div>
                <div className="rank-sub">
                  {s.total_pulls} pulls Β· {s.unique_users} users Β· {s.unique_cells} cells
                </div>
              </li>
            ))}
            {(data.by_strategy || []).length === 0 && (
              <li className="rank-empty">No strategies pulled yet.</li>
            )}
          </ol>
        </div>
      </div>

      {/* Stage + readiness funnels */}
      <div className="platform-cols-2">
        <div className="platform-block">
          <div className="platform-block-head">
            Decision-stage funnel
            <InfoHint width={340}>
              Where users are in their buying journey, inferred from each user's recent intents.
              <strong> Awareness</strong> (Definitional-heavy) β†’ <strong>Exploration</strong> (mixed)
              β†’ <strong>Evaluation</strong> (Comparison-heavy) β†’ <strong>Action-ready</strong> (Decision-heavy).
              <strong> Support-needed</strong> users are stuck and may need human help.
            </InfoHint>
          </div>
          <ul className="platform-funnel">
            {STAGE_ORDER.map((stage) => {
              const row = (data.stage_funnel || []).find((r) => r.stage === stage);
              const count = row?.count || 0;
              return (
                <li key={stage} className={count ? "" : "dim"}>
                  <span className="funnel-label">{stage}</span>
                  <span className="funnel-bar-wrap">
                    <span
                      className={`funnel-bar funnel-${stage.toLowerCase().replace(/\W+/g, "-")}`}
                      style={{ width: `${(count / maxStageCount) * 100}%` }}
                    />
                  </span>
                  <span className="funnel-count">{count}</span>
                </li>
              );
            })}
          </ul>
        </div>

        <div className="platform-block">
          <div className="platform-block-head">
            Outreach-readiness distribution
            <InfoHint width={340}>
              How many users land in each readiness tier.
              <strong> Ready</strong> β‰₯ 0.70 β€” surface for outreach.
              <strong> Likely</strong> β‰₯ 0.50 β€” strong candidates, nurture lightly.
              <strong> Nurture</strong> β‰₯ 0.30 β€” keep engaged but don't push.
              <strong> Too early</strong> &lt; 0.30 β€” leave them alone.
              The stage gate means an "Awareness" user can't be Ready regardless of engagement.
            </InfoHint>
          </div>
          <ul className="platform-funnel">
            {READINESS_ORDER.map((tier) => {
              const row = (data.readiness_funnel || []).find((r) => r.tier === tier);
              const count = row?.count || 0;
              return (
                <li key={tier} className={count ? "" : "dim"}>
                  <span className="funnel-label">{tier}</span>
                  <span className="funnel-bar-wrap">
                    <span
                      className={`funnel-bar funnel-tier-${tier.toLowerCase().replace(/\W+/g, "-")}`}
                      style={{ width: `${(count / maxReadyCount) * 100}%` }}
                    />
                  </span>
                  <span className="funnel-count">{count}</span>
                </li>
              );
            })}
          </ul>
        </div>
      </div>

      {/* Intent + signal mix */}
      <div className="platform-cols-2">
        <div className="platform-block">
          <div className="platform-block-head">
            Intent mix
            <InfoHint width={300}>
              Percentage of all turns in this window by intent. A balanced spread is healthy;
              a heavy <em>Definitional</em> skew means the user base is still learning. A heavy
              <em> Comparison/Decision</em> skew means they're at the action end.
            </InfoHint>
          </div>
          <div className="pct-row">
            {(data.intent_mix || []).map((i) => (
              <span key={i.intent} className="pct-pill">
                <span className="pct-name">{i.intent}</span>
                <span className="pct-num">{i.pct}%</span>
              </span>
            ))}
            {(data.intent_mix || []).length === 0 && <span className="muted">No intents recorded.</span>}
          </div>
        </div>

        <div className="platform-block">
          <div className="platform-block-head">
            Signal mix (across all firings)
            <InfoHint width={380}>
              How often each signal fired across all rewarded turns. The
              catalog is the 9-signal set from the reward doc: thumbs (UI)
              plus six LLM-detected text signals.
              <br/><br/>
              Green = positive (<code>thumbs_up</code>,
              <code> format_praise_explicit</code>, <code>it_worked_statement</code>,
              <code> deeper_question</code>).
              Red = negative (<code>thumbs_down</code>,
              <code> format_change_request</code>, <code>content_correction</code>,
              <code> reask_same_question</code>).
              A healthy mix is mostly green with rare explicit complaints.
            </InfoHint>
          </div>
          <div className="pct-row">
            {(data.signal_mix || []).map((s) => (
              <span
                key={s.signal}
                className={`pct-pill signal-${signalToneClass(s.signal)}`}
                title={`${s.count} firings`}
              >
                <span className="pct-name">{s.signal.replace(/_/g, " ")}</span>
                <span className="pct-num">{s.pct}%</span>
              </span>
            ))}
            {(data.signal_mix || []).length === 0 && <span className="muted">No signals recorded.</span>}
          </div>
        </div>
      </div>
    </div>
  );
}

// ---------- Helpers ----------

const STAGE_ORDER     = ["Awareness", "Exploration", "Evaluation", "Action-ready", "Support-needed", "Unknown"];
const READINESS_ORDER = ["Ready", "Likely", "Nurture", "Too early"];

function signalToneClass(name) {
  if (!name) return "neutral";
  // Legacy composite patterns may exist in old data β€” keep them distinct
  if (name.startsWith("pattern_")) return "composite";
  // Positive β€” current catalog first, legacy names kept for old records
  if (/^(thumbs_up|format_praise_explicit|it_worked_statement|deeper_question|copy_save|format_keep_request|format_compliance_pass|session_continue)$/.test(name)) {
    return "pos";
  }
  // Negative β€” current catalog first, legacy names kept for old records
  if (/^(thumbs_down|format_change_request|content_correction|reask_same_question|regenerate_click|session_abandon|format_compliance_fail)$/.test(name)) {
    return "neg";
  }
  return "neutral";
}

function PlatformNumber({ value, label }) {
  return (
    <div className="platform-num">
      <div className="platform-num-value">{value ?? "β€”"}</div>
      <div className="platform-num-label">{label}</div>
    </div>
  );
}

function fmt(v) {
  if (v == null) return "β€”";
  const x = Number(v);
  if (!Number.isFinite(x)) return "β€”";
  return x.toFixed(2);
}

function prettyName(s) {
  if (!s) return "β€”";
  return String(s).replace(/_/g, " ");
}

function sum(arr) {
  let s = 0;
  for (const x of arr) s += Number(x) || 0;
  return s;
}