All files / app/service bugs.ts

17.07% Statements 7/41
0% Branches 0/4
0% Functions 0/6
17.94% Lines 7/39

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  117x 117x   117x   117x                                   116x                                       116x                                     116x                              
import { BugReportFormData } from "components/Navigation/ReportDialog";
import Sentry from "platform/sentry";
import { GeolocationClickInfoReq, GeolocationSearchInfoReq, ReportBugReq, ScreenResolution } from "proto/bugs_pb";
 
import client from "./client";
 
const REPLAY_FLUSH_TIMEOUT_MS = 2000;
 
// Force the buffered replay to upload and return its id. Best-effort: a failure
// here must never block the bug report. The timeout only caps how long the user
// waits on submit — the upload carries on in the background, so we still return
// the id rather than drop the link.
async function flushSentryReplay(): Promise<string> {
  const replay = Sentry.getReplay();
  if (!replay) return "";
  try {
    const flushed = replay.flush().catch(() => {});
    await Promise.race([flushed, new Promise((resolve) => setTimeout(resolve, REPLAY_FLUSH_TIMEOUT_MS))]);
    return replay.getReplayId() ?? "";
  } catch {
    return "";
  }
}
 
export async function reportBug({ description, results, subject }: BugReportFormData) {
  const req = new ReportBugReq();
 
  const screenResolution = new ScreenResolution();
  screenResolution.setWidth(window.innerWidth);
  screenResolution.setHeight(window.innerHeight);
 
  req.setSubject(subject);
  req.setDescription(description);
  req.setResults(results);
  req.setFrontendVersion(process.env.NEXT_PUBLIC_VERSION);
  req.setUserAgent(navigator.userAgent);
  req.setScreenResolution(screenResolution);
  req.setPage(window.location.href);
  req.setSentryReplayId(await flushSentryReplay());
 
  const res = await client.bugs.reportBug(req);
  return res.toObject();
}
 
export async function geolocationSearchInfo({
  searchString,
  nominatimResultJson,
  formattedResultJson,
  durationMs,
}: {
  searchString: string;
  nominatimResultJson: string;
  formattedResultJson: string;
  durationMs: number;
}) {
  const req = new GeolocationSearchInfoReq();
  req.setSearchString(searchString);
  req.setNominatimResultJson(nominatimResultJson);
  req.setFormattedResultJson(formattedResultJson);
  req.setDurationMs(Math.max(Math.round(durationMs), 0));
  await client.bugs.geolocationSearchInfo(req);
}
 
export async function geolocationClickInfo({
  context,
  formattedResultJson,
  searchChoiceJson,
}: {
  context: string;
  formattedResultJson: string;
  searchChoiceJson: string;
}) {
  const req = new GeolocationClickInfoReq();
  req.setContext(context);
  req.setFormattedResultJson(formattedResultJson);
  req.setSearchChoiceJson(searchChoiceJson);
  await client.bugs.geolocationClickInfo(req);
}