upend/ui/src/components/Address.svelte

115 lines
2.4 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";
import Ellipsis from "./Ellipsis.svelte";
2021-11-11 23:37:42 +01:00
import UpLink from "./UpLink.svelte";
import { useEntity } from "../lib/entity";
import type { UpObject } from "upend";
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-11-30 21:18:47 +01:00
let resolving = resolve;
2021-11-11 23:37:42 +01:00
$: ({ 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-11-11 23:37:42 +01:00
$: {
if (resolve) {
2021-11-30 21:18:47 +01:00
resolving = true;
inferredIds = $entity?.identify() || [];
resolving = false;
2021-11-11 23:37:42 +01:00
}
}
$: 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() {
fetch(`/api/raw/${address}?native=1`);
}
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) {
padding: 0 .25em;
}
}
2021-11-11 23:37:42 +01:00
</style>