upend/webui/src/views/Search.svelte

192 lines
4.4 KiB
Svelte
Raw Normal View History

2021-12-21 14:32:47 +01:00
<script lang="ts">
import debounce from "lodash/debounce";
import { Readable, readable } from "svelte/store";
2022-01-30 16:50:23 +01:00
import type { UpListing } from "upend";
2022-01-14 22:05:17 +01:00
import Spinner from "../components/utils/Spinner.svelte";
import UpEntryDisplay from "../components/display/UpEntry.svelte";
import UpObjectCard from "../components/display/UpObjectCard.svelte";
2022-01-16 19:53:40 +01:00
import { useNavigate } from "svelte-navigator";
import { baseSearch, createLabelled } from "../util/search";
2022-02-04 22:44:40 +01:00
import { updateTitle } from "../util/title";
import { query as queryFn } from "../lib/entity";
import AttributeView from "../components/AttributeView.svelte";
import { UpType } from "../lib/types";
import { queryOnce } from "../lib/api";
2022-01-16 19:53:40 +01:00
const navigate = useNavigate();
2021-12-21 14:32:47 +01:00
export let query: string;
let debouncedQuery = "";
const updateQuery = debounce((query: string) => {
debouncedQuery = query;
}, 200);
$: updateQuery(query);
$: looksLikeQuery =
debouncedQuery.startsWith("(") && debouncedQuery.endsWith(")");
2021-12-21 14:32:47 +01:00
let result: Readable<UpListing> = readable();
let error: Readable<unknown> = readable();
$: if (debouncedQuery.length) {
({ result, error } = looksLikeQuery
? queryFn(debouncedQuery)
: baseSearch(debouncedQuery));
exactHits = [];
2022-01-14 22:05:17 +01:00
}
2022-01-16 19:29:15 +01:00
$: objects = ($result?.entries || []).filter((e) => e.attribute === "LBL");
let exactHits: string[] = [];
$: {
const addressesString = objects.map((e) => `"${e.entity}"`).join(" ");
queryOnce(`(matches (in ${addressesString}) "LBL" ? )`).then(
(labelListing) => {
exactHits = labelListing.entries
.filter(
(e) => String(e.value.c).toLowerCase() === query.toLowerCase()
)
.map((e) => e.entity);
2022-01-16 19:29:15 +01:00
}
);
2022-01-16 19:29:15 +01:00
}
2022-01-16 19:53:40 +01:00
$: console.log({ exactHits });
2022-01-16 19:53:40 +01:00
async function create() {
const createdAddress = await createLabelled(query);
navigate(`/browse/${createdAddress}`);
2022-01-16 19:53:40 +01:00
}
2022-02-04 22:44:40 +01:00
$: updateTitle("Search", query);
const SearchViewType = new UpType();
SearchViewType.name = "SEARCH_VIEW";
2021-12-21 14:32:47 +01:00
</script>
<div>
2022-01-14 22:05:17 +01:00
{#if !$error}
{#if !looksLikeQuery}
<section class="exact">
{#if exactHits.length}
<ul>
{#each exactHits as address}
<li>
<UpObjectCard {address} --width="100%" --height="100%" />
</li>
{/each}
</ul>
{:else}
<div class="create">
<div>Create new object?</div>
<button class="create-object" on:click={create}>"{query}"</button>
</div>
{/if}
</section>
{/if}
2022-01-14 22:05:17 +01:00
{#if $result}
2022-01-16 18:53:18 +01:00
<section class="objects">
{#await objects}
2022-01-16 19:53:40 +01:00
<h2>Objects</h2>
2022-02-06 12:20:56 +01:00
<Spinner centered />
2022-01-16 18:53:18 +01:00
{:then objects}
2022-01-16 19:53:40 +01:00
{#if objects.length}
<h2>Objects</h2>
<AttributeView
--current-background="var(--background)"
entries={objects}
type={SearchViewType}
/>
2022-01-16 19:53:40 +01:00
{/if}
2022-01-16 18:53:18 +01:00
{/await}
</section>
2022-01-16 19:53:40 +01:00
<section class="raw">
{#if $result?.entries.length}
2022-01-14 22:05:17 +01:00
<h2>Raw results</h2>
<ul>
{#each $result.entries as entry}
<li><UpEntryDisplay {entry} resolve={false} /></li>
2022-01-14 22:05:17 +01:00
{/each}
</ul>
2022-01-16 19:53:40 +01:00
{:else}
<div class="global">No results found.</div>
{/if}
</section>
2022-01-14 22:05:17 +01:00
{:else}
<div class="global">
2022-02-06 12:20:56 +01:00
<Spinner centered />
2022-01-14 22:05:17 +01:00
</div>
{/if}
{:else}
2022-01-16 19:53:40 +01:00
<div class="error global">
2022-01-14 22:05:17 +01:00
{$error}
</div>
2021-12-21 14:32:47 +01:00
{/if}
</div>
2022-01-13 19:08:36 +01:00
<style lang="scss">
h2 {
text-align: center;
}
ul {
list-style: none;
margin: 0;
2022-01-14 22:05:17 +01:00
padding: 0;
2022-01-13 19:08:36 +01:00
}
2022-01-14 22:05:17 +01:00
2022-01-16 19:29:15 +01:00
.exact {
2022-01-17 16:59:12 +01:00
margin-top: 1rem;
2022-01-16 18:53:18 +01:00
ul {
2022-01-16 19:29:15 +01:00
display: flex;
2022-01-16 18:53:18 +01:00
gap: 1rem;
2022-01-16 19:29:15 +01:00
justify-content: center;
flex-wrap: wrap;
2022-01-16 18:53:18 +01:00
margin: 0 1rem;
}
li {
width: 14rem;
height: 8rem;
}
2022-01-16 19:29:15 +01:00
.create {
text-align: center;
}
2022-01-16 19:53:40 +01:00
.create-object {
display: inline-block;
color: var(--foreground);
background: var(--background-lighter);
border: 1px solid var(--foreground);
border-radius: 4px;
padding: 0.2em;
font-size: 1.25em;
cursor: pointer;
2022-01-17 16:59:12 +01:00
margin-top: 1rem;
2022-01-16 19:53:40 +01:00
}
2022-01-16 19:29:15 +01:00
}
.objects {
max-width: 66em;
margin: auto;
2022-01-16 18:53:18 +01:00
}
.raw {
li {
margin: 1em auto;
max-width: 66em;
}
2022-01-14 22:05:17 +01:00
}
.global {
font-size: 48px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
2022-01-13 19:08:36 +01:00
}
</style>