upend/ui/src/components/display/UpObject.svelte

132 lines
3.0 KiB
Svelte
Raw Normal View History

2021-11-11 23:37:42 +01:00
<script lang="ts">
import { createEventDispatcher } from "svelte";
2021-12-01 19:59:37 +01:00
import { BLOB_TYPE_ADDR } from "upend/constants";
2021-11-11 23:37:42 +01:00
import HashBadge from "./HashBadge.svelte";
2021-12-21 20:02:47 +01:00
import Ellipsis from "../utils/Ellipsis.svelte";
2021-11-11 23:37:42 +01:00
import UpLink from "./UpLink.svelte";
2021-12-21 20:02:47 +01:00
import { useEntity } from "../../lib/entity";
2021-12-20 13:49:06 +01:00
import { readable } from "svelte/store";
2021-12-21 20:02:47 +01:00
import { notify, UpNotification } from "../../notifications";
const dispatch = createEventDispatcher();
2021-11-11 23:37:42 +01:00
export let address: string;
export let labels: string[] = [];
2021-11-11 23:37:42 +01:00
export let link = false;
export let resolve = true;
2021-12-01 21:12:13 +01:00
export let banner = false;
2021-12-20 13:49:06 +01:00
let entity = readable(undefined);
$: if (resolve) ({ entity } = useEntity(address));
2021-12-01 19:59:37 +01:00
// isFile
$: isFile = $entity?.get("IS") === BLOB_TYPE_ADDR;
2021-12-01 19:59:37 +01:00
2021-11-11 23:37:42 +01:00
// Identification
let inferredIds: string[] = [];
2021-12-20 13:49:06 +01:00
$: inferredIds = $entity?.identify() || [];
2021-12-21 21:35:17 +01:00
$: resolving = inferredIds.concat(labels).length == 0 && !$entity;
$: displayLabel =
Array.from(new Set(inferredIds.concat(labels))).join(" | ") || address;
2021-12-01 13:37:23 +01:00
$: dispatch("resolved", inferredIds);
2021-12-19 22:38:41 +01:00
// Native open
function nativeOpen() {
notify.emit(
"notification",
new UpNotification(
`Opening ${
inferredIds[0] || address
} in a default native application...`
)
);
fetch(`/api/raw/${address}?native=1`)
.then((response) => {
if (!response.ok) {
throw new Error(response.statusText);
}
})
.catch((err) => {
notify.emit(
"notification",
new UpNotification(
`Failed to open in native application! (${err})`,
"error"
)
);
});
2021-12-19 22:38:41 +01:00
}
2021-11-11 23:37:42 +01:00
</script>
2021-12-01 21:12:13 +01:00
<div class="address" class:identified={Boolean(inferredIds)} class:banner>
2021-11-11 23:37:42 +01:00
<HashBadge {address} />
<div class="separator" />
<div class="label" class:resolving title={displayLabel}>
{#if banner && isFile}
<a href="/api/raw/{address}" target="_blank">
<Ellipsis value={displayLabel} />
</a>
{:else if link}
<UpLink to={{ entity: address }}>
<Ellipsis value={displayLabel} />
</UpLink>
{:else}
<Ellipsis value={displayLabel} />
{/if}
</div>
2021-12-19 22:38:41 +01:00
{#if banner && isFile}
2021-12-20 13:44:03 +01:00
<sl-icon-button
name="arrow-up-right-circle"
on:click={nativeOpen}
title="Open in default application..."
/>
2021-12-19 22:38:41 +01:00
{/if}
2021-11-11 23:37:42 +01:00
</div>
<style scoped lang="scss">
.address {
display: flex;
align-items: center;
padding: 0.1em;
font-family: var(--monospace-font);
line-break: anywhere;
2021-11-11 23:37:42 +01:00
2021-12-01 21:12:13 +01:00
background: var(--background-emph);
border: 0.1em solid var(--foreground-lighter);
border-radius: 0.2em;
&.banner {
border: 0.12em solid var(--foreground);
padding: 0.5em 0.25em;
}
2021-11-11 23:37:42 +01:00
&.identified {
font-family: var(--default-font);
font-size: 0.95em;
line-break: auto;
}
}
2021-11-11 23:37:42 +01:00
.label {
flex-grow: 1;
min-width: 0;
}
.separator {
width: 0.5em;
2021-11-11 23:37:42 +01:00
}
2021-11-30 21:18:47 +01:00
.resolving {
opacity: 0.7;
}
2021-12-20 13:44:03 +01:00
sl-icon-button {
&::part(base) {
2021-12-20 13:49:06 +01:00
padding: 0 0.25em;
2021-12-20 13:44:03 +01:00
}
}
2021-11-11 23:37:42 +01:00
</style>