File size: 928 Bytes
884908f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { addLogEntry } from "./logEntries";
import { getQuerySuggestions, updateQuerySuggestions } from "./pubSub";

export async function getRandomQuerySuggestion() {
  if (getQuerySuggestions().length === 0) await refillQuerySuggestions(25);

  const querySuggestions = getQuerySuggestions();

  const randomQuerySuggestion = querySuggestions.pop() as string;

  updateQuerySuggestions(querySuggestions);

  return randomQuerySuggestion;
}

async function refillQuerySuggestions(limit?: number) {
  const querySuggestionsFileUrl = new URL(
    "/query-suggestions.json",
    self.location.origin,
  );

  const fetchResponse = await fetch(querySuggestionsFileUrl.toString());

  const querySuggestionsList: string[] = await fetchResponse.json();

  updateQuerySuggestions(
    querySuggestionsList.sort(() => Math.random() - 0.5).slice(0, limit),
  );

  addLogEntry(`Query suggestions refilled with ${limit} suggestions`);
}