upend/webui/src/components/Inspect.svelte

152 lines
3.7 KiB
Svelte
Raw Normal View History

2021-11-11 23:37:42 +01:00
<script lang="ts">
import AttributeView from "./AttributeView.svelte";
import { query, useEntity } from "../lib/entity";
2021-12-21 20:02:47 +01:00
import UpObject from "./display/UpObject.svelte";
2021-11-11 23:37:42 +01:00
import { UpType } from "../lib/types";
2021-12-21 20:02:47 +01:00
import BlobPreview from "./display/BlobPreview.svelte";
import { setContext } from "svelte";
import { writable } from "svelte/store";
import type { UpEntry } from "upend";
import Spinner from "./utils/Spinner.svelte";
2021-11-11 23:37:42 +01:00
export let address: string;
export let index: number | undefined;
2021-11-11 23:37:42 +01:00
export let editable = false;
let indexStore = writable(index);
$: $indexStore = index;
setContext("browse", { index: indexStore });
$: ({ entity, error, revalidate } = useEntity(address));
2021-11-11 23:37:42 +01:00
$: allTypeAddresses = ($entity?.attr["IS"] || []).map((attr) => attr.value.c);
2021-11-11 23:37:42 +01:00
$: allTypeEntries = query(
() =>
`(matches (in ${allTypeAddresses
.map((addr) => `"${addr}"`)
.join(" ")}) ? ?)`
).result;
let allTypes: { [key: string]: UpType } = {};
$: {
allTypes = {};
($allTypeEntries?.entries || []).forEach((entry) => {
2021-11-11 23:37:42 +01:00
if (allTypes[entry.entity] === undefined) {
allTypes[entry.entity] = new UpType(entry.entity);
}
switch (entry.attribute) {
case "TYPE":
allTypes[entry.entity].name = String(entry.value.c);
2021-11-11 23:37:42 +01:00
break;
case "TYPE_HAS":
allTypes[entry.entity].attributes.push(String(entry.value.c));
2021-11-11 23:37:42 +01:00
break;
}
});
allTypes = allTypes;
}
let typedAttributes = {} as { [key: string]: UpEntry[] };
let untypedAttributes = [] as UpEntry[];
2021-11-11 23:37:42 +01:00
$: {
typedAttributes = {};
untypedAttributes = [];
($entity?.attributes || []).forEach((entry) => {
2021-11-11 23:37:42 +01:00
const entryTypes = Object.entries(allTypes).filter(([_, t]) =>
t.attributes.includes(entry.attribute)
);
if (entryTypes.length > 0) {
entryTypes.forEach(([addr, _]) => {
if (typedAttributes[addr] == undefined) {
typedAttributes[addr] = [];
}
typedAttributes[addr].push(entry);
2021-11-11 23:37:42 +01:00
});
} else {
untypedAttributes.push(entry);
2021-11-11 23:37:42 +01:00
}
});
typedAttributes = typedAttributes;
untypedAttributes = untypedAttributes.filter(
(entry) => !["IS", "LBL"].includes(entry.attribute)
);
2021-11-11 23:37:42 +01:00
}
</script>
<div class="inspect">
<h2>
{#if $entity}
<UpObject banner {address} />
{:else}
<Spinner />
{/if}
2021-11-11 23:37:42 +01:00
</h2>
2021-11-30 23:26:40 +01:00
<BlobPreview {address} />
2021-11-11 23:37:42 +01:00
{#if !$error}
{#if Boolean($allTypeEntries)}
<div class="attributes">
{#each Object.entries(typedAttributes) as [typeAddr, entries] (typeAddr)}
<AttributeView
{address}
{entries}
type={allTypes[typeAddr]}
{editable}
on:changed={revalidate}
/>
{/each}
{#if untypedAttributes.length > 0 || editable}
<AttributeView
title="Other attributes"
{editable}
{address}
entries={untypedAttributes}
on:changed={revalidate}
/>
{/if}
{#if $entity?.backlinks.length > 0}
<AttributeView
title={`Referred to (${$entity.backlinks.length})`}
{address}
entries={$entity.backlinks}
reverse
on:changed={revalidate}
/>
{/if}
</div>
{:else}
<Spinner />
{/if}
2021-11-11 23:37:42 +01:00
{:else}
<div class="error">
{JSON.stringify($error)}
</div>
{/if}
</div>
<style scoped lang="scss">
2021-11-30 00:29:27 +01:00
.inspect {
flex: auto;
display: flex;
flex-direction: column;
}
.attributes {
flex: auto;
height: 0; // https://stackoverflow.com/a/14964944
overflow-y: auto;
}
2021-11-11 23:37:42 +01:00
.error {
color: red;
}
</style>