|
| 1 | +/** |
| 2 | + * First-run telemetry consent dialog. |
| 3 | + * |
| 4 | + * - Free-tier users: informational only — telemetry is required, single "I Understand" button. |
| 5 | + * - Paid/unknown users: genuine opt-in with "Enable" / "No Thanks" buttons. |
| 6 | + * |
| 7 | + * The consent choice is persisted to the KV store and loaded into the |
| 8 | + * Telemetry consent module on startup. |
| 9 | + */ |
| 10 | + |
| 11 | +import { TextAttributes } from "@opentui/core" |
| 12 | +import { useTheme } from "@tui/context/theme" |
| 13 | +import { useDialog, type DialogContext } from "@tui/ui/dialog" |
| 14 | +import { useKV } from "@tui/context/kv" |
| 15 | +import { createStore } from "solid-js/store" |
| 16 | +import { For, Show } from "solid-js" |
| 17 | +import { useKeyboard } from "@opentui/solid" |
| 18 | +import { Telemetry } from "@/telemetry" |
| 19 | + |
| 20 | +/** KV keys used by the consent dialog */ |
| 21 | +export const KV_TELEMETRY_CONSENT_SHOWN = "telemetry_consent_shown" |
| 22 | +export const KV_TELEMETRY_ENABLED = "telemetry_enabled" |
| 23 | + |
| 24 | +type Tier = "free" | "paid" |
| 25 | + |
| 26 | +export type DialogTelemetryConsentProps = { |
| 27 | + tier: Tier |
| 28 | + onResult: (accepted: boolean) => void |
| 29 | +} |
| 30 | + |
| 31 | +export function DialogTelemetryConsent(props: DialogTelemetryConsentProps) { |
| 32 | + const dialog = useDialog() |
| 33 | + const { theme } = useTheme() |
| 34 | + const kv = useKV() |
| 35 | + |
| 36 | + const isFree = () => props.tier === "free" |
| 37 | + |
| 38 | + const title = () => isFree() ? "Usage Data Collection" : "Enable Usage Telemetry?" |
| 39 | + |
| 40 | + const message = () => |
| 41 | + isFree() |
| 42 | + ? "CodeQ collects anonymous usage telemetry to improve the product.\n" + |
| 43 | + "This includes session metrics (token counts, tool usage, latency)\n" + |
| 44 | + "and is required for free-tier accounts. No source code or secrets\n" + |
| 45 | + "are collected. You can review our privacy policy at qbraid.com/privacy." |
| 46 | + : "CodeQ can collect anonymous usage telemetry to help us improve\n" + |
| 47 | + "the product. This includes session metrics like token counts,\n" + |
| 48 | + "tool usage, and latency. No source code or secrets are collected.\n\n" + |
| 49 | + "You can change this anytime in your config:\n" + |
| 50 | + ' qbraid.telemetry.enabled: true | false' |
| 51 | + |
| 52 | + // Free tier: single button. Paid tier: two-button confirm/decline. |
| 53 | + const buttons = () => |
| 54 | + isFree() ? ["understand"] as const : ["decline", "enable"] as const |
| 55 | + |
| 56 | + const [store, setStore] = createStore({ |
| 57 | + active: isFree() ? "understand" : "enable", |
| 58 | + }) |
| 59 | + |
| 60 | + const labels: Record<string, string> = { |
| 61 | + understand: "I Understand", |
| 62 | + enable: "Enable", |
| 63 | + decline: "No Thanks", |
| 64 | + } |
| 65 | + |
| 66 | + const handleSelect = (key: string) => { |
| 67 | + const accepted = key === "understand" || key === "enable" |
| 68 | + kv.set(KV_TELEMETRY_CONSENT_SHOWN, true) |
| 69 | + kv.set(KV_TELEMETRY_ENABLED, accepted) |
| 70 | + Telemetry.setConsent(accepted) |
| 71 | + props.onResult(accepted) |
| 72 | + dialog.clear() |
| 73 | + } |
| 74 | + |
| 75 | + useKeyboard((evt) => { |
| 76 | + if (evt.name === "return") { |
| 77 | + handleSelect(store.active) |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + if (!isFree() && (evt.name === "left" || evt.name === "right")) { |
| 82 | + setStore("active", store.active === "enable" ? "decline" : "enable") |
| 83 | + } |
| 84 | + }) |
| 85 | + |
| 86 | + return ( |
| 87 | + <box paddingLeft={2} paddingRight={2} gap={1}> |
| 88 | + <box flexDirection="row" justifyContent="space-between"> |
| 89 | + <text attributes={TextAttributes.BOLD} fg={theme.text}> |
| 90 | + {title()} |
| 91 | + </text> |
| 92 | + <Show when={!isFree()}> |
| 93 | + <text fg={theme.textMuted}>esc</text> |
| 94 | + </Show> |
| 95 | + </box> |
| 96 | + <box paddingBottom={1}> |
| 97 | + <text fg={theme.textMuted}>{message()}</text> |
| 98 | + </box> |
| 99 | + <box flexDirection="row" justifyContent="flex-end" paddingBottom={1} gap={1}> |
| 100 | + <For each={[...buttons()]}> |
| 101 | + {(key) => ( |
| 102 | + <box |
| 103 | + paddingLeft={2} |
| 104 | + paddingRight={2} |
| 105 | + backgroundColor={key === store.active ? theme.primary : undefined} |
| 106 | + onMouseUp={() => handleSelect(key)} |
| 107 | + > |
| 108 | + <text fg={key === store.active ? theme.selectedListItemText : theme.textMuted}> |
| 109 | + {labels[key]} |
| 110 | + </text> |
| 111 | + </box> |
| 112 | + )} |
| 113 | + </For> |
| 114 | + </box> |
| 115 | + </box> |
| 116 | + ) |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Show the consent dialog and return a promise that resolves with the user's choice. |
| 121 | + */ |
| 122 | +DialogTelemetryConsent.show = ( |
| 123 | + dialog: DialogContext, |
| 124 | + tier: Tier, |
| 125 | +): Promise<boolean> => { |
| 126 | + return new Promise<boolean>((resolve) => { |
| 127 | + dialog.replace( |
| 128 | + () => ( |
| 129 | + <DialogTelemetryConsent |
| 130 | + tier={tier} |
| 131 | + onResult={(accepted) => resolve(accepted)} |
| 132 | + /> |
| 133 | + ), |
| 134 | + // If user presses Esc on paid tier, treat as decline |
| 135 | + () => resolve(false), |
| 136 | + ) |
| 137 | + }) |
| 138 | +} |
0 commit comments