File size: 8,489 Bytes
65d68f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use client";

import { useId, useState } from "react";
import { MessageSquarePlus, X } from "lucide-react";
import { cn } from "@/lib/utils";
import { trackBetaEvent } from "@/lib/beta/betaAnalytics";
import {
  BETA_FEEDBACK_KIND_LABELS,
  MAX_FEEDBACK_MESSAGE_LENGTH,
  submitBetaFeedback,
  type BetaFeedbackKind,
  type BetaFeedbackRating,
} from "@/lib/beta/betaFeedback";

type BetaFeedbackButtonProps = {
  route?: string;
  subject?: string;
  chapterTitle?: string;
  missionTitle?: string;
  stepLabel?: string;
  className?: string;
};

const KIND_ORDER: BetaFeedbackKind[] = [
  "helped_understand",
  "still_confusing",
  "wrong_or_doubtful",
  "too_long",
  "need_more_examples",
  "class_confusing",
  "bug",
  "content_issue",
  "ui_issue",
  "too_hard",
  "too_easy",
  "liked",
  "other",
];

export function BetaFeedbackButton({
  route,
  subject,
  chapterTitle,
  missionTitle,
  stepLabel,
  className,
}: BetaFeedbackButtonProps) {
  const [open, setOpen] = useState(false);
  const [kind, setKind] = useState<BetaFeedbackKind>("class_confusing");
  const [rating, setRating] = useState<BetaFeedbackRating | null>(null);
  const [message, setMessage] = useState("");
  const [submitted, setSubmitted] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const titleId = useId();
  const messageId = useId();

  const context = { route, subject, chapterTitle, missionTitle, stepLabel };

  function openPanel() {
    setOpen(true);
    setSubmitted(false);
    setError(null);
    trackBetaEvent({ name: "feedback_opened", ...context });
  }

  function closePanel() {
    setOpen(false);
  }

  function handleSubmit() {
    const result = submitBetaFeedback({ kind, rating: rating ?? undefined, message, ...context });
    if (!result.ok) {
      setError(result.error === "empty_message" ? "Please write a short message first." : "Storage unavailable on this device.");
      return;
    }
    setSubmitted(true);
    setMessage("");
    setRating(null);
  }

  return (
    <div className={cn("relative", className)}>
      <button
        type="button"
        onClick={openPanel}
        aria-haspopup="dialog"
        aria-expanded={open}
        aria-label="Open beta feedback"
        className="inline-flex min-h-9 items-center gap-2 rounded-lg border border-[#2A2A2A] bg-[#111111] px-3 text-[12px] font-semibold text-white transition hover:bg-[#1A1A1A] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-white/35"
      >
        <MessageSquarePlus className="size-3.5" aria-hidden="true" />
        Beta feedback
      </button>

      {open ? (
        <div
          role="dialog"
          aria-modal="false"
          aria-labelledby={titleId}
          data-testid="beta-feedback-panel"
          className="absolute right-0 z-40 mt-2 w-[min(92vw,340px)] rounded-xl border border-[#2A2A2A] bg-[#0B0B0B] p-4 shadow-[0_24px_80px_rgba(0,0,0,0.5)]"
        >
          <div className="flex items-start justify-between gap-2">
            <div>
              <p id={titleId} className="text-[13px] font-bold text-white">
                Beta feedback
              </p>
              <p className="mt-1 text-[11px] leading-4 text-[#8B8B8B]">Tell us what confused you or what broke.</p>
            </div>
            <button
              type="button"
              onClick={closePanel}
              aria-label="Close beta feedback"
              className="rounded-lg border border-[#2A2A2A] bg-[#111111] p-1.5 text-[#A1A1AA] transition hover:text-white focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-white/35"
            >
              <X className="size-3.5" aria-hidden="true" />
            </button>
          </div>

          {submitted ? (
            <div className="mt-4" data-testid="beta-feedback-confirmation">
              <p className="text-[13px] font-semibold text-white">Thanks! Feedback saved.</p>
              <p className="mt-1 text-[11px] leading-4 text-[#8B8B8B]">Stored on this device for beta testing.</p>
              <button
                type="button"
                onClick={closePanel}
                className="mt-3 inline-flex min-h-9 w-full items-center justify-center rounded-lg bg-white px-3 text-[12px] font-bold text-black transition hover:bg-white/90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-white/35"
              >
                Close feedback
              </button>
            </div>
          ) : (
            <div className="mt-3 space-y-3">
              <div>
                <label htmlFor={`${titleId}-kind`} className="text-[10px] font-semibold uppercase tracking-wider text-[#6B7280]">
                  What is this about?
                </label>
                <select
                  id={`${titleId}-kind`}
                  aria-label="What is this about?"
                  value={kind}
                  onChange={(event) => setKind(event.target.value as BetaFeedbackKind)}
                  className="mt-1.5 w-full rounded-lg border border-[#2A2A2A] bg-[#050505] p-2 text-[12px] text-white focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-white/35"
                >
                  {KIND_ORDER.map((value) => (
                    <option key={value} value={value}>
                      {BETA_FEEDBACK_KIND_LABELS[value]}
                    </option>
                  ))}
                </select>
              </div>

              <div>
                <fieldset className="mt-1.5 flex gap-1.5" aria-label="Rating (optional)">
                  <legend className="sr-only">Rating (optional)</legend>
                  {([1, 2, 3, 4, 5] as BetaFeedbackRating[]).map((value) => (
                    <button
                      key={value}
                      type="button"
                      aria-label={`Rate ${value} out of 5`}
                      aria-pressed={rating === value}
                      onClick={() => setRating((current) => (current === value ? null : value))}
                      className={cn(
                        "size-8 rounded-lg border text-[12px] font-bold transition focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-white/35",
                        rating === value
                          ? "border-white bg-white text-black"
                          : "border-[#2A2A2A] bg-[#050505] text-white hover:bg-[#151515]",
                      )}
                    >
                      {value}
                    </button>
                  ))}
                </fieldset>
              </div>

              <div>
                <label htmlFor={messageId} className="text-[10px] font-semibold uppercase tracking-wider text-[#6B7280]">
                  Message
                </label>
                <textarea
                  id={messageId}
                  aria-label="Message"
                  value={message}
                  maxLength={MAX_FEEDBACK_MESSAGE_LENGTH}
                  onChange={(event) => {
                    setMessage(event.target.value);
                    if (error) setError(null);
                  }}
                  rows={4}
                  placeholder="What confused you or what broke?"
                  className="mt-1.5 w-full resize-y rounded-lg border border-[#2A2A2A] bg-[#050505] p-2.5 text-[12px] leading-5 text-white placeholder:text-[#5F6368] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-white/35"
                />
                <div className="mt-1 flex items-center justify-between">
                  <p className="text-[10px] text-[#5F6368]">{message.length}/{MAX_FEEDBACK_MESSAGE_LENGTH}</p>
                  {error ? <p className="text-[10px] font-semibold text-white">{error}</p> : null}
                </div>
              </div>

              <p className="text-[10px] leading-4 text-[#6B7280]">
                Beta feedback is saved on this device. Do not include private personal details.
              </p>

              <button
                type="button"
                onClick={handleSubmit}
                className="inline-flex min-h-9 w-full items-center justify-center rounded-lg bg-white px-3 text-[12px] font-bold text-black transition hover:bg-white/90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-white/35"
              >
                Submit feedback
              </button>
            </div>
          )}
        </div>
      ) : null}
    </div>
  );
}