upend/webui/src/components/Inspect.svelte

623 lines
15 KiB
Svelte
Raw Normal View History

2021-11-11 23:37:42 +01:00
<script lang="ts">
2023-06-19 11:53:35 +02:00
import EntryView, { type Widget } from "./EntryView.svelte";
2023-06-28 14:26:34 +02:00
import { useEntity } from "../lib/entity";
2022-03-22 21:57:00 +01:00
import UpObject from "./display/UpObject.svelte";
2022-02-20 18:04:47 +01:00
import { createEventDispatcher, setContext } from "svelte";
2023-06-28 14:26:34 +02:00
import { derived, writable, type Readable } from "svelte/store";
2022-03-22 21:57:00 +01:00
import type { UpEntry } from "upend";
2022-01-21 15:57:53 +01:00
import Spinner from "./utils/Spinner.svelte";
import NotesEditor from "./utils/NotesEditor.svelte";
import type { AttributeChange } from "../types/base";
import Selector from "./utils/Selector.svelte";
2023-06-28 18:50:33 +02:00
import type { ADDRESS_TYPE, EntityInfo, IValue } from "upend/types";
2022-01-30 16:40:48 +01:00
import IconButton from "./utils/IconButton.svelte";
import type { BrowseContext } from "../util/browse";
2023-01-10 21:45:03 +01:00
import { Link, useParams } from "svelte-navigator";
2022-02-20 18:04:47 +01:00
import Icon from "./utils/Icon.svelte";
import BlobViewer from "./display/BlobViewer.svelte";
import { i18n } from "../i18n";
2023-01-07 11:00:55 +01:00
import EntryList from "./widgets/EntryList.svelte";
import api from "../lib/api";
2023-06-16 16:30:17 +02:00
import Gallery from "./widgets/Gallery.svelte";
2023-06-28 14:26:34 +02:00
import { ATTR_IN, ATTR_LABEL, ATTR_KEY, ATTR_OF } from "upend/constants";
2022-02-20 18:04:47 +01:00
const dispatch = createEventDispatcher();
const params = useParams();
2021-11-11 23:37:42 +01:00
export let address: string;
export let index: number | undefined;
export let detail: boolean;
2021-11-11 23:37:42 +01:00
export let editable = false;
2023-04-23 14:11:19 +02:00
let showAsEntries = false;
let highlightedType: string | undefined;
2021-11-11 23:37:42 +01:00
2022-03-25 14:11:00 +01:00
let blobHandled = false;
let indexStore = writable(index);
$: $indexStore = index;
let addressesStore = writable([]);
$: $addressesStore = $params.addresses?.split(",") || [];
setContext("browse", {
index: indexStore,
addresses: addressesStore,
} as BrowseContext);
2023-01-07 11:00:55 +01:00
$: ({ entity, entityInfo, error, revalidate } = useEntity(address));
2021-11-11 23:37:42 +01:00
2023-06-28 14:26:34 +02:00
$: allTypes = derived(
entityInfo,
($entityInfo, set) => {
getAllTypes($entityInfo).then((allTypes) => {
set(allTypes);
});
},
{}
) as Readable<{
[key: string]: {
labels: string[];
attributes: string[];
};
}>;
async function getAllTypes(entityInfo: EntityInfo) {
const allTypes = {};
if (!entityInfo) {
return {};
}
2021-11-11 23:37:42 +01:00
2023-06-28 14:26:34 +02:00
const typeAddresses: string[] = [
await api.getAddress(entityInfo.t),
...$entity.attr[ATTR_IN].map((e) => e.value.c as string),
];
const typeAddressesIn = typeAddresses.map((addr) => `@${addr}`).join(" ");
2021-11-11 23:37:42 +01:00
2023-06-28 14:26:34 +02:00
const labelsQuery = await api.query(
`(matches (in ${typeAddressesIn}) "${ATTR_LABEL}" ?)`
);
2023-06-28 18:50:33 +02:00
await Promise.all(
typeAddresses.map(async (address) => {
let labels = labelsQuery.getObject(address).identify();
let typeLabel: string | undefined;
await Promise.all(
(["Hash", "Uuid", "Attribute", "Url"] as ADDRESS_TYPE[]).map(
async (t) => {
if ((await api.getAddress(t)) == address) {
labels.push(`[${t}]`);
}
}
)
);
if (typeLabel) {
labels.unshift(typeLabel);
}
if (!labels.length) {
labels.push(address);
}
allTypes[address] = {
labels,
attributes: [],
};
})
);
2021-11-11 23:37:42 +01:00
2023-06-28 14:26:34 +02:00
const attributes = await api.query(
`(matches ? "${ATTR_OF}" (in ${typeAddressesIn}))`
);
await Promise.all(
typeAddresses.map(async (address) => {
allTypes[address].attributes = (
await Promise.all(
(attributes.getObject(address).attr[`~${ATTR_OF}`] || []).map(
async (e) => {
try {
const { t, c } = await api.addressToComponents(e.entity);
if (t == "Attribute") {
return c;
}
} catch (err) {
console.error(err);
return false;
}
}
)
)
).filter(Boolean);
})
);
2021-11-11 23:37:42 +01:00
2023-06-28 14:26:34 +02:00
return allTypes;
2021-11-11 23:37:42 +01:00
}
let typedAttributes = {} as { [key: string]: UpEntry[] };
let untypedAttributes = [] as UpEntry[];
2021-11-11 23:37:42 +01:00
$: {
typedAttributes = {};
untypedAttributes = [];
($entity?.attributes || []).forEach((entry) => {
2023-06-28 14:26:34 +02:00
const entryTypes = Object.entries($allTypes || {}).filter(([_, t]) =>
2021-11-11 23:37:42 +01:00
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;
2022-01-21 15:57:53 +01:00
untypedAttributes = untypedAttributes;
2021-11-11 23:37:42 +01:00
}
2022-01-21 15:57:53 +01:00
$: filteredUntypedAttributes = untypedAttributes.filter(
(entry) =>
2022-03-22 21:57:00 +01:00
![
2023-06-24 16:26:14 +02:00
ATTR_LABEL,
ATTR_IN,
ATTR_KEY,
2022-03-22 21:57:00 +01:00
"NOTE",
"LAST_VISITED",
"NUM_VISITED",
"LAST_ATTRIBUTE_WIDGET",
].includes(entry.attribute)
2022-01-21 15:57:53 +01:00
);
$: currentUntypedAttributes = editable
? untypedAttributes
: filteredUntypedAttributes;
2022-01-28 22:39:08 +01:00
$: currentBacklinks =
(editable
? $entity?.backlinks
: $entity?.backlinks.filter(
2023-06-24 16:26:14 +02:00
(entry) => ![ATTR_IN].includes(entry.attribute)
2022-01-28 22:39:08 +01:00
)) || [];
2023-06-24 16:26:14 +02:00
$: groups = ($entity?.attr[ATTR_IN] || []).map((e) => [
2023-06-19 13:02:34 +02:00
e.value.c as string,
e.address,
]);
2023-06-24 16:26:14 +02:00
$: tagged = $entity?.attr[`~${ATTR_IN}`] || [];
2022-01-28 22:39:08 +01:00
2023-01-07 11:00:55 +01:00
let attributesUsed: UpEntry[] = [];
$: {
if ($entityInfo?.t === "Attribute") {
api
.query(`(matches ? "${$entityInfo.c}" ?)`)
.then((result) => (attributesUsed = result.entries));
2023-01-07 11:00:55 +01:00
}
}
async function onChange(ev: CustomEvent<AttributeChange>) {
const change = ev.detail;
switch (change.type) {
case "create":
await api.putEntry({
entity: address,
attribute: change.attribute,
value: change.value,
});
break;
case "delete":
await api.deleteEntry(change.address);
break;
case "update":
await api.putEntityAttribute(address, change.attribute, change.value);
break;
default:
console.error("Unimplemented AttributeChange", change);
return;
}
revalidate();
}
2022-02-20 18:04:47 +01:00
let identities = [address];
function onResolved(ev: CustomEvent<string[]>) {
identities = ev.detail;
dispatch("resolved", ev.detail);
}
let groupToAdd: IValue | undefined;
async function addGroup() {
if (!groupToAdd) {
return;
}
await api.putEntry([
{
entity: address,
2023-06-24 16:26:14 +02:00
attribute: ATTR_IN,
value: {
t: "Address",
c: String(groupToAdd.c),
},
},
]);
revalidate();
groupToAdd = undefined;
}
2022-01-30 16:40:48 +01:00
2022-02-20 18:04:47 +01:00
async function removeGroup(groupAddress: string) {
await api.deleteEntry(groupAddress);
2022-01-30 16:40:48 +01:00
revalidate();
}
2022-02-20 18:04:47 +01:00
async function deleteObject() {
if (confirm(`${$i18n.t("Really delete")} "${identities.join(" | ")}"?`)) {
await api.deleteEntry(address);
2022-02-20 18:04:47 +01:00
dispatch("close");
}
}
2022-03-22 20:35:01 +01:00
async function onAttributeWidgetSwitch(ev: CustomEvent<string>) {
2023-06-19 16:45:55 +02:00
await api.putEntityAttribute(
address,
"LAST_ATTRIBUTE_WIDGET",
{
t: "String",
c: ev.detail,
},
"UI_PREFERENCES"
);
2022-03-22 20:35:01 +01:00
}
2023-06-19 11:53:35 +02:00
const attributeWidgets: Widget[] = [
{
name: "List",
2023-06-19 12:58:42 +02:00
icon: "list-check",
2023-06-19 11:53:35 +02:00
components: (entries) => [
{
component: EntryList,
props: {
entries,
columns: "attribute, value",
},
},
],
},
{
name: "Gallery",
2023-06-19 12:58:42 +02:00
icon: "image",
2023-06-19 11:53:35 +02:00
components: (entries) => [
{
component: Gallery,
props: {
entities: entries
.filter((e) => e.value.t == "Address")
.map((e) => e.value.c),
thumbnails: true,
},
},
],
},
];
const taggedWidgets: Widget[] = [
{
name: "List",
2023-06-19 12:58:42 +02:00
icon: "list-check",
2023-06-19 11:53:35 +02:00
components: (entries) => [
{
component: Gallery,
props: {
entities: entries.map((e) => e.entity),
thumbnails: false,
},
},
],
},
{
name: "Gallery",
2023-06-19 12:58:42 +02:00
icon: "image",
2023-06-19 11:53:35 +02:00
components: (entries) => [
{
component: Gallery,
props: {
entities: entries.map((e) => e.entity),
thumbnails: true,
},
},
],
},
];
$: entity.subscribe(async (object) => {
if (object && object.listing.entries.length) {
2023-06-19 16:45:55 +02:00
await api.putEntityAttribute(
object.address,
"LAST_VISITED",
{
t: "Number",
c: new Date().getTime() / 1000,
},
"IMPLICIT"
);
await api.putEntityAttribute(
object.address,
"NUM_VISITED",
{
t: "Number",
c: (parseInt(String(object.get("NUM_VISITED"))) || 0) + 1,
},
"IMPLICIT"
);
}
});
2021-11-11 23:37:42 +01:00
</script>
2022-03-25 14:11:00 +01:00
<div class="inspect" class:detail class:blob={blobHandled}>
2022-01-28 22:39:08 +01:00
<header>
<h2>
{#if $entity}
2022-03-22 21:57:00 +01:00
<UpObject banner {address} on:resolved={onResolved} />
2022-01-28 22:39:08 +01:00
{:else}
2022-02-06 12:20:56 +01:00
<Spinner centered />
2022-01-28 22:39:08 +01:00
{/if}
</h2>
</header>
2023-04-23 14:11:19 +02:00
{#if !showAsEntries}
<div class="main-content">
<div class="detail-col">
<div class="blob-viewer">
<BlobViewer
{address}
{editable}
{detail}
on:handled={(ev) => (blobHandled = ev.detail)}
/>
</div>
<NotesEditor {address} {editable} on:change={onChange} />
{#if !$error}
2023-06-16 16:30:17 +02:00
{#if groups.length}
<section class="groups labelborder">
<header><h3>{$i18n.t("Groups")}</h3></header>
2023-05-03 21:13:09 +02:00
<div class="content">
2023-06-19 13:02:34 +02:00
{#each groups as [groupAddress, groupEntryAddress]}
2023-05-03 21:13:09 +02:00
<div
2023-06-16 16:30:17 +02:00
class="tag"
2023-06-19 13:02:34 +02:00
on:mouseenter={() => (highlightedType = groupAddress)}
2023-05-03 21:13:09 +02:00
on:mouseleave={() => (highlightedType = undefined)}
>
2023-06-19 13:02:34 +02:00
<UpObject address={groupAddress} link />
2023-05-03 21:13:09 +02:00
{#if editable}
<IconButton
name="x-circle"
2023-06-19 13:02:34 +02:00
on:click={() => removeGroup(groupEntryAddress)}
2023-05-03 21:13:09 +02:00
/>
{/if}
</div>
{/each}
{#if editable}
<div class="selector">
<Selector
type="value"
valueTypes={["Address"]}
bind:value={groupToAdd}
on:input={addGroup}
placeholder="Choose an entity..."
2023-04-23 14:11:19 +02:00
/>
2023-05-03 21:13:09 +02:00
</div>
{/if}
</div>
</section>
{/if}
2023-06-16 16:30:17 +02:00
<div class="attributes">
{#each Object.entries(typedAttributes) as [typeAddr, entries] (typeAddr)}
<EntryView
{entries}
{editable}
2023-06-19 11:53:35 +02:00
widgets={attributeWidgets}
2023-06-16 16:30:17 +02:00
on:change={onChange}
highlighted={highlightedType == typeAddr}
2023-06-28 14:26:34 +02:00
title={$allTypes[typeAddr].labels.join(" | ")}
2023-06-16 16:30:17 +02:00
/>
{/each}
{#if currentUntypedAttributes.length > 0 || editable}
<EntryView
title={$i18n.t("Attributes")}
{editable}
2023-06-19 11:53:35 +02:00
widgets={attributeWidgets}
2023-06-16 16:30:17 +02:00
entries={currentUntypedAttributes}
on:change={onChange}
/>
{/if}
2022-03-25 14:11:00 +01:00
2023-06-16 16:30:17 +02:00
{#if currentBacklinks.length > 0}
<EntryView
title={`${$i18n.t("Referred to")} (${currentBacklinks.length})`}
entries={currentBacklinks}
on:change={onChange}
/>
{/if}
2023-01-07 11:00:55 +01:00
2023-06-16 16:30:17 +02:00
{#if tagged.length > 0}
<EntryView
title={`${$i18n.t("Members")}`}
2023-06-19 11:53:35 +02:00
widgets={taggedWidgets}
2023-06-16 16:30:17 +02:00
entries={tagged}
on:change={onChange}
/>
{/if}
2023-04-23 14:11:19 +02:00
2023-06-16 16:30:17 +02:00
{#if $entityInfo?.t === "Attribute"}
<div class="buttons">
<div class="button">
<Link to="/surface?x={$entityInfo.c}">
{$i18n.t("Surface view")}
</Link>
2023-04-23 14:11:19 +02:00
</div>
2023-01-10 21:45:03 +01:00
</div>
2023-06-16 16:30:17 +02:00
<section class="labelborder">
<header>
<h3>{$i18n.t("Used")} ({attributesUsed.length})</h3>
</header>
<EntryList
columns="entity,value"
columnWidths={["auto", "33%"]}
entries={attributesUsed}
orderByValue
/>
</section>
2023-01-07 11:00:55 +01:00
{/if}
2023-06-16 16:30:17 +02:00
</div>
{#if editable}
<!-- svelte-ignore a11y-click-events-have-key-events -->
<div class="button" on:click={deleteObject}>
<Icon name="trash" />
</div>
{/if}
2022-03-25 14:11:00 +01:00
{:else}
2023-04-23 14:11:19 +02:00
<div class="error">
{$error}
</div>
2022-02-20 18:04:47 +01:00
{/if}
2023-04-23 14:11:19 +02:00
</div>
</div>
{:else}
<div class="entries">
<h2>{$i18n.t("Attributes")}</h2>
<EntryList
{editable}
entries={$entity.attributes}
columns={detail
? "timestamp, provenance, attribute, value"
: "attribute, value"}
/>
2023-04-23 14:11:19 +02:00
<h2>{$i18n.t("Backlinks")}</h2>
<EntryList
entries={$entity.backlinks}
columns={detail
? "timestamp, provenance, entity, attribute"
: "entity, attribute"}
/>
2022-03-25 14:11:00 +01:00
</div>
2023-04-23 14:11:19 +02:00
{/if}
<div class="footer">
<IconButton
name="detail"
title="Show as entries"
active={showAsEntries}
on:click={() => (showAsEntries = !showAsEntries)}
/>
</div>
2021-11-11 23:37:42 +01:00
</div>
<style scoped lang="scss">
2022-01-28 22:39:08 +01:00
@use "./util";
header h2 {
margin-bottom: 0;
}
.inspect,
.main-content {
2021-11-30 00:29:27 +01:00
flex: auto;
display: flex;
flex-direction: column;
2022-01-28 22:39:08 +01:00
gap: 0.5rem;
min-height: 0;
2021-11-30 00:29:27 +01:00
}
.groups {
2022-01-28 22:39:08 +01:00
margin: 0.25rem 0;
.content {
display: flex;
flex-wrap: wrap;
gap: 0.5rem 0.5rem;
align-items: center;
}
2022-01-30 16:40:48 +01:00
.tag {
2022-01-30 16:40:48 +01:00
display: inline-flex;
align-items: center;
}
.selector {
width: 100%;
}
2022-01-28 22:39:08 +01:00
}
2021-11-30 00:29:27 +01:00
.attributes {
flex: auto;
height: 0; // https://stackoverflow.com/a/14964944
min-height: 12em;
2021-11-30 00:29:27 +01:00
overflow-y: auto;
}
@media screen and (max-height: 1080px) {
.main-content {
overflow-y: auto;
// min-height: 0;
}
.attributes {
height: unset;
min-height: unset;
overflow-y: unset;
}
}
2022-03-25 14:11:00 +01:00
.inspect.detail {
.main-content {
position: relative;
flex-direction: row;
justify-content: end;
2022-03-25 14:11:00 +01:00
}
.blob-viewer {
width: 73%;
2023-02-26 15:33:39 +01:00
height: 100%;
position: absolute;
left: 1%;
top: 0;
2022-03-25 14:11:00 +01:00
}
.detail-col {
width: 25%;
2022-03-25 14:11:00 +01:00
}
&.blob {
2022-03-25 14:11:00 +01:00
.detail-col {
flex-grow: 0;
2022-03-25 14:11:00 +01:00
}
}
}
2022-04-09 19:58:44 +02:00
.main-content .detail-col {
display: flex;
flex-direction: column;
flex-grow: 1;
}
2023-04-23 14:11:19 +02:00
.entries {
flex-grow: 1;
}
.footer {
display: flex;
justify-content: end;
}
2023-01-10 21:45:03 +01:00
.buttons {
display: flex;
}
2021-11-11 23:37:42 +01:00
.error {
color: red;
}
</style>