| 1 |
<!-- |
| 2 |
@component |
| 3 |
Renders a query result in a collapsible box. |
| 4 |
--> |
| 5 |
<svelte:options immutable /> |
| 6 |
|
| 7 |
<script lang="ts"> |
| 8 |
import Chart from "../../charts/Chart.svelte"; |
| 9 |
import { chartContext } from "../../charts/context"; |
| 10 |
import { getQueryChart } from "../../charts/query-charts"; |
| 11 |
import type { Result } from "../../lib/result"; |
| 12 |
import type { QueryResult } from "./query_table"; |
| 13 |
import QueryLinks from "./QueryLinks.svelte"; |
| 14 |
import QueryTable from "./QueryTable.svelte"; |
| 15 |
import ReadonlyQueryEditor from "./ReadonlyQueryEditor.svelte"; |
| 16 |
|
| 17 |
/** The query string. */ |
| 18 |
export let query: string; |
| 19 |
/** The query result, possibly missing or an error. */ |
| 20 |
export let result: Result<QueryResult, string> | undefined; |
| 21 |
/** Whether this box is open. */ |
| 22 |
export let open: boolean | undefined; |
| 23 |
|
| 24 |
/** Handler to run on 'select' (clicking the summary bar). */ |
| 25 |
export let onselect: () => void; |
| 26 |
/** Handler to run on 'delete' (clicking the x button). */ |
| 27 |
export let ondelete: () => void; |
| 28 |
|
| 29 |
$: inactive = !result; |
| 30 |
$: chart = |
| 31 |
result && result.is_ok && result.value.t === "table" |
| 32 |
? getQueryChart(result.value, $chartContext) |
| 33 |
: null; |
| 34 |
</script> |
| 35 |
|
| 36 |
<details bind:open> |
| 37 |
<summary class:inactive on:click={inactive ? onselect : null}> |
| 38 |
<ReadonlyQueryEditor value={query} error={result?.is_err} /> |
| 39 |
<span class="spacer"></span> |
| 40 |
{#if result && result.is_ok && result.value.t === "table"} |
| 41 |
<QueryLinks {query} /> |
| 42 |
{/if} |
| 43 |
<button |
| 44 |
type="button" |
| 45 |
on:click={(ev) => { |
| 46 |
ev.stopPropagation(); |
| 47 |
ondelete(); |
| 48 |
}} |
| 49 |
> |
| 50 |
x |
| 51 |
</button> |
| 52 |
</summary> |
| 53 |
<div> |
| 54 |
{#if result} |
| 55 |
{#if result.is_ok} |
| 56 |
{#if result.value.t === "string"} |
| 57 |
<pre><code>{result.value.contents}</code></pre> |
| 58 |
{:else} |
| 59 |
{#if chart} |
| 60 |
<Chart {chart} /> |
| 61 |
{/if} |
| 62 |
<QueryTable table={result.value} /> |
| 63 |
{/if} |
| 64 |
{:else} |
| 65 |
<pre><code>{result.error}</code></pre> |
| 66 |
{/if} |
| 67 |
{/if} |
| 68 |
</div> |
| 69 |
</details> |
| 70 |
|
| 71 |
<style> |
| 72 |
details > div { |
| 73 |
max-height: 70vh; |
| 74 |
overflow: auto; |
| 75 |
} |
| 76 |
|
| 77 |
.inactive { |
| 78 |
filter: opacity(0.5); |
| 79 |
} |
| 80 |
|
| 81 |
pre { |
| 82 |
margin: 0; |
| 83 |
} |
| 84 |
</style> |