diff --git a/tools/upend_js/index.ts b/tools/upend_js/index.ts index 21326fb..b16b18d 100644 --- a/tools/upend_js/index.ts +++ b/tools/upend_js/index.ts @@ -14,42 +14,54 @@ import type { IEntry, ListingResult, VALUE_TYPE } from "./types"; export class UpListing { public readonly entries: UpEntry[]; + private _objects: { [key: string]: UpObject } = {}; constructor(listing: ListingResult) { - this.entries = Object.entries(listing).map((lr) => new UpEntry(...lr)); + this.entries = Object.entries(listing).map( + (lr) => new UpEntry(...lr, this) + ); } - public get objects(): UpObject[] { + public get objects() { const allEntities = new Set(this.entries.map((e) => e.entity)); - return Array.from(allEntities).map( - (entity) => new UpObject(entity, this.entries) + const result: { [key: string]: UpObject } = {}; + Array.from(allEntities).forEach( + (entity) => (result[entity] = new UpObject(entity, this)) ); + return result; + } + + public getObject(address: string) { + if (!this._objects[address]) { + this._objects[address] = new UpObject(address, this); + } + return this._objects[address]; } } export class UpObject { public readonly address; - private entries: UpEntry[]; + public listing: UpListing | undefined; - constructor(address: string, entries?: UpEntry[]) { + constructor(address: string, listing?: UpListing) { this.address = address; - this.entries = entries || []; + this.listing = listing; } - public bind(entries: UpEntry[]) { - this.entries = entries; - } - - public bindAppend(entries: UpEntry[]) { - this.entries.push(...entries); + public bind(listing: UpListing) { + this.listing = listing; } public get attributes() { - return this.entries.filter((e) => e.entity === this.address); + return (this.listing?.entries || []).filter( + (e) => e.entity === this.address + ); } public get backlinks() { - return this.entries.filter((e) => e.value.c === this.address); + return (this.listing?.entries || []).filter( + (e) => e.value.c === this.address + ); } public get attr() { @@ -70,18 +82,10 @@ export class UpObject { } public identify(): string[] { - // Get all places where this Object is "had" - const hasEntries = this.backlinks + const hasAliases = this.backlinks .filter((entry) => entry.attribute === "HAS") - .map((entry) => entry.address); - - // Out of those relations, retrieve their ALIAS attrs - const hasAliases = this.entries - .filter( - (entry) => - entry.attribute === "ALIAS" && hasEntries.includes(entry.entity) - ) - .map((entry) => String(entry.value.c)); + .map((entry) => entry.get("ALIAS")) + .filter(Boolean) as string[]; const lblValues = (this.attr["LBL"] || []).map((e) => String(e.value.c)); @@ -101,8 +105,8 @@ export class UpEntry extends UpObject implements IEntry { attribute: string; value: { t: VALUE_TYPE; c: string | number }; - constructor(address: string, entry: IEntry) { - super(address); + constructor(address: string, entry: IEntry, listing: UpListing) { + super(address, listing); this.entity = entry.entity; this.attribute = entry.attribute; diff --git a/ui/src/components/Address.svelte b/ui/src/components/Address.svelte index f9ba4b0..2b7976d 100644 --- a/ui/src/components/Address.svelte +++ b/ui/src/components/Address.svelte @@ -5,9 +5,11 @@ import Ellipsis from "./Ellipsis.svelte"; import UpLink from "./UpLink.svelte"; import { useEntity } from "../lib/entity"; + import type { UpObject } from "upend"; const dispatch = createEventDispatcher(); export let address: string; + export let labels: string[] = []; export let link = false; export let resolve = true; export let banner = false; @@ -29,7 +31,8 @@ } } - $: label = inferredIds.join(" | ") || address; + $: displayLabel = + Array.from(new Set(inferredIds.concat(labels))).join(" | ") || address; $: dispatch("resolved", inferredIds); @@ -37,17 +40,17 @@