diff --git a/ui/src/lib/entity.ts b/ui/src/lib/entity.ts index c51e7c1..a73fa1f 100644 --- a/ui/src/lib/entity.ts +++ b/ui/src/lib/entity.ts @@ -3,122 +3,143 @@ import { fetcher } from "@/utils"; import useSWRV from "swrv"; import { computed, ComputedRef, Ref } from "vue"; +export function useEntity( + address: string | (() => string), + condition?: () => Boolean +) { + const { data, error, mutate } = useSWRV( + () => + condition === undefined || condition() + ? `/api/obj/${typeof address === "string" ? address : address()}` + : null, + fetcher, + { revalidateOnFocus: false } + ); -export function useEntity(address: string | (() => string), condition?: () => Boolean) { - const { data, error, mutate } = useSWRV( - () => (condition === undefined || condition()) ? `/api/obj/${typeof address === "string" ? address : address()}` : null, - fetcher, { revalidateOnFocus: false } - ); - - const entries = computed(() => { - if (data?.value) { - const entries = Object.entries(data.value) as [string, IEntry][]; - return entries - .sort(([_, a], [__, b]) => String(a.value.c).localeCompare(b.value.c)) - .sort(([_, a], [__, b]) => String(a.value.t).localeCompare(b.value.t)) - .sort(([_, a], [__, b]) => a.attribute.localeCompare(b.attribute)); - } else { - return []; - } - }); - - const attributes = computed(() => { - const addr = typeof address === "string" ? address : address(); - return entries.value.filter(([_, e]) => e.entity === addr); - }); - - const backlinks = computed(() => { - const addr = typeof address === "string" ? address : address(); - return entries.value.filter(([_, e]) => e.entity !== addr); - }); - - return { - entries, - attributes, - backlinks, - data, - error, - mutate + const entries = computed(() => { + if (data?.value) { + const entries = Object.entries(data.value) as [string, IEntry][]; + return entries + .sort(([_, a], [__, b]) => String(a.value.c).localeCompare(b.value.c)) + .sort(([_, a], [__, b]) => String(a.value.t).localeCompare(b.value.t)) + .sort(([_, a], [__, b]) => a.attribute.localeCompare(b.attribute)); + } else { + return []; } + }); + + const attributes = computed(() => { + const addr = typeof address === "string" ? address : address(); + return entries.value.filter(([_, e]) => e.entity === addr); + }); + + const backlinks = computed(() => { + const addr = typeof address === "string" ? address : address(); + return entries.value.filter(([_, e]) => e.entity !== addr); + }); + + return { + entries, + attributes, + backlinks, + data, + error, + mutate + }; } +export function query( + query: string | (() => string), + condition?: () => Boolean +) { + const { data, error, mutate } = useSWRV( + () => + condition === undefined || condition() + ? `/api/obj?query=${typeof query === "string" ? query : query()}` + : null, + fetcher, + { revalidateOnFocus: false } + ); -export function query(query: string | (() => string), condition?: () => Boolean) { - const { data, error, mutate } = useSWRV( - () => (condition === undefined || condition()) ? `/api/obj?query=${typeof query === "string" ? query : query()}` : null, - fetcher, { revalidateOnFocus: false } - ); - - const result = computed(() => { - if (data?.value) { - const entries = Object.entries(data.value) as [string, IEntry][]; - return entries - .sort(([_, a], [__, b]) => String(a.value.c).localeCompare(b.value.c)) - .sort(([_, a], [__, b]) => String(a.value.t).localeCompare(b.value.t)) - .sort(([_, a], [__, b]) => a.attribute.localeCompare(b.attribute)); - } else { - return []; - } - }); - - return { - result, - data, - error, - mutate + const result = computed(() => { + if (data?.value) { + const entries = Object.entries(data.value) as [string, IEntry][]; + return entries + .sort(([_, a], [__, b]) => String(a.value.c).localeCompare(b.value.c)) + .sort(([_, a], [__, b]) => String(a.value.t).localeCompare(b.value.t)) + .sort(([_, a], [__, b]) => a.attribute.localeCompare(b.attribute)); + } else { + return []; } + }); + + return { + result, + data, + error, + mutate + }; } interface EntityIdentification { - type: string; - value: string; + type: string; + value: string; } -export function identify(attributes: ComputedRef<[string, IEntry][]>): ComputedRef { - // Get all identities of the object - const isEntries = computed(() => { +export function identify( + attributes: ComputedRef<[string, IEntry][]> +): ComputedRef { + // Get all identities of the object + const isEntries = computed(() => { + return attributes.value + .filter(([_, entry]) => entry.attribute === "IS") + .map(([_, entry]) => entry.value.c); + }); + + // Out of those, retrieve their TYPE_ID entries + const { data: typeIdListing } = query(() => { + return ( + isEntries.value && + `(matches (in ${isEntries.value + .map(e => `"${e}"`) + .join(" ")}) "TYPE_ID" ?)` + ); + }); + + const typeIdAttributes: ComputedRef<[string, string][]> = computed(() => { + return Object.values(typeIdListing.value || {}).map(entry => { + return [entry.entity, entry.value.c]; + }); + }); + + // Finally, filter own object's attributes according to TYPE_IDs + return computed(() => { + // For each identity/TYPE_ID pair + return typeIdAttributes.value + .map(([type, attrName]) => { + // And each associated TYPE_ID attribute... + // return own matchin attributes return attributes.value - .filter(([_, entry]) => entry.attribute === "IS") - .map(([_, entry]) => entry.value.c); - }) - - - // Out of those, retrieve their TYPE_ID entries - const { data: typeIdListing } = query(() => { - return isEntries.value && `(matches (in ${isEntries.value.map((e) => `"${e}"`).join(" ")}) "TYPE_ID" ?)`; - }); - - const typeIdAttributes: ComputedRef<[string, string][]> = computed(() => { - return Object.values(typeIdListing.value || {}).map((entry) => { - return [entry.entity, entry.value.c]; - }); - }); - - - - // Finally, filter own object's attributes according to TYPE_IDs - return computed(() => { - // For each identity/TYPE_ID pair - return typeIdAttributes.value - .map(([type, attrName]) => { - // And each associated TYPE_ID attribute... - // return own matchin attributes - return attributes.value - .filter(([_, e]) => e.attribute === attrName) - .map(([_, attr]) => { - return { - type, - value: attr.value.c - } - }) - }).flat(); - }); + .filter(([_, e]) => e.attribute === attrName) + .map(([_, attr]) => { + return { + type, + value: attr.value.c + }; + }); + }) + .flat(); + }); } -export function asDict(attributes: [string, IEntry][]): { [key: string]: string } { - const result = {} as { [key: string]: string }; - attributes.map(([_, attribute]) => attribute).forEach((attribute) => { - result[attribute.attribute] = attribute.value.c; +export function asDict( + attributes: [string, IEntry][] +): { [key: string]: string } { + const result = {} as { [key: string]: string }; + attributes + .map(([_, attribute]) => attribute) + .forEach(attribute => { + result[attribute.attribute] = attribute.value.c; }); - return result; -} \ No newline at end of file + return result; +}