upend/ui/src/lib/entity.ts

41 lines
1.1 KiB
TypeScript
Raw Normal View History

import { IEntry, ListingResult } from "@/types/base";
import { fetcher } from "@/utils";
import useSWRV from "swrv";
import { computed } from "vue";
export function useEntity(address: string) {
const { data, error, mutate } = useSWRV<ListingResult, unknown>(
() => `/api/obj/${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(() => {
return entries.value.filter(([_, e]) => e.entity === address);
});
const backlinks = computed(() => {
return entries.value.filter(([_, e]) => e.entity !== address);
});
return {
entries,
attributes,
backlinks,
data,
error,
mutate
}
}