import { IEntry, ListingResult } from "@/types/base"; import { fetcher } from "@/utils"; import useSWRV from "swrv"; import { computed, Ref } from "vue"; export function useEntity(address: string | (() => string)) { const { data, error, mutate } = useSWRV( () => `/api/obj/${typeof address === "string" ? address : address()}`, fetcher ); 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 } }