| 1 |
<script lang="ts"> |
| 2 |
import { onMount } from "svelte"; |
| 3 |
|
| 4 |
import { get } from "../../api"; |
| 5 |
import { err, ok, type Result } from "../../lib/result"; |
| 6 |
import { log_error } from "../../log"; |
| 7 |
import router from "../../router"; |
| 8 |
import { filter_params } from "../../stores/filters"; |
| 9 |
import { query_shell_history } from "../../stores/query"; |
| 10 |
import { searchParams } from "../../stores/url"; |
| 11 |
import type { QueryResult } from "./query_table"; |
| 12 |
import QueryBox from "./QueryBox.svelte"; |
| 13 |
import QueryEditor from "./QueryEditor.svelte"; |
| 14 |
|
| 15 |
let query_string = ""; |
| 16 |
|
| 17 |
/** The currently loaded results. */ |
| 18 |
let results: Record<string, Result<QueryResult, string>> = {}; |
| 19 |
const is_open: Record<string, boolean> = {}; |
| 20 |
|
| 21 |
onMount(() => |
| 22 |
searchParams.subscribe((s) => { |
| 23 |
const search_query_string = s.get("query_string") ?? ""; |
| 24 |
// Set the query string to the value from the URL query if that changes (e.g. on navigation). |
| 25 |
if (search_query_string !== query_string) { |
| 26 |
query_string = search_query_string; |
| 27 |
submit(); |
| 28 |
} |
| 29 |
}), |
| 30 |
); |
| 31 |
|
| 32 |
onMount(() => |
| 33 |
filter_params.subscribe(() => { |
| 34 |
rerun_all_open(); |
| 35 |
}), |
| 36 |
); |
| 37 |
|
| 38 |
/** Submit the current query and load the result for it. */ |
| 39 |
function submit() { |
| 40 |
const query = query_string; |
| 41 |
if (!query) { |
| 42 |
return; |
| 43 |
} |
| 44 |
if (query.trim().toUpperCase() === "CLEAR") { |
| 45 |
query_shell_history.clear(); |
| 46 |
query_string = ""; |
| 47 |
router.set_search_param("query_string", ""); |
| 48 |
return; |
| 49 |
} |
| 50 |
query_shell_history.add(query); |
| 51 |
router.set_search_param("query_string", query); |
| 52 |
get("query", { query_string: query, ...$filter_params }) |
| 53 |
.then( |
| 54 |
(res) => ok(res), |
| 55 |
(error: unknown) => |
| 56 |
err(error instanceof Error ? error.message : "INTERNAL ERROR"), |
| 57 |
) |
| 58 |
.then((res) => { |
| 59 |
results[query] = res; |
| 60 |
is_open[query] = true; |
| 61 |
document.querySelector("article")?.scroll(0, 0); |
| 62 |
}) |
| 63 |
.catch(log_error); |
| 64 |
} |
| 65 |
|
| 66 |
/* Re-run all open queries on global filter change */ |
| 67 |
function rerun_all_open() { |
| 68 |
const to_rerun = [...Object.entries(is_open)] |
| 69 |
.filter(([, is_open]) => is_open) |
| 70 |
.map(([query]) => query); |
| 71 |
results = {}; |
| 72 |
for (const query of to_rerun) { |
| 73 |
get("query", { query_string: query, ...$filter_params }) |
| 74 |
.then( |
| 75 |
(res) => ok(res), |
| 76 |
(error: unknown) => |
| 77 |
err(error instanceof Error ? error.message : "INTERNAL ERROR"), |
| 78 |
) |
| 79 |
.then((res) => { |
| 80 |
results[query] = res; |
| 81 |
}) |
| 82 |
.catch(log_error); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
/** Delete the given query from the history and potentially clear it from the form. */ |
| 87 |
function delete_item(query: string) { |
| 88 |
query_shell_history.remove(query); |
| 89 |
if (query_string === query) { |
| 90 |
query_string = ""; |
| 91 |
router.set_search_param("query_string", ""); |
| 92 |
} |
| 93 |
} |
| 94 |
</script> |
| 95 |
|
| 96 |
<QueryEditor bind:value={query_string} {submit} /> |
| 97 |
{#each $query_shell_history as query (query)} |
| 98 |
<QueryBox |
| 99 |
{query} |
| 100 |
result={results[query]} |
| 101 |
bind:open={is_open[query]} |
| 102 |
onselect={() => { |
| 103 |
query_string = query; |
| 104 |
submit(); |
| 105 |
}} |
| 106 |
ondelete={() => { |
| 107 |
delete_item(query); |
| 108 |
}} |
| 109 |
/> |
| 110 |
{/each} |