2021-04-04 10:37:19 +02:00
|
|
|
import { IEntry, ListingResult } from "@/types/base";
|
|
|
|
import { fetcher } from "@/utils";
|
|
|
|
import useSWRV from "swrv";
|
2021-04-04 11:58:23 +02:00
|
|
|
import { computed, Ref } from "vue";
|
2021-04-04 10:37:19 +02:00
|
|
|
|
|
|
|
|
2021-04-04 11:58:23 +02:00
|
|
|
export function useEntity(address: string | (() => string)) {
|
2021-04-04 10:37:19 +02:00
|
|
|
const { data, error, mutate } = useSWRV<ListingResult, unknown>(
|
2021-04-04 11:58:23 +02:00
|
|
|
() => `/api/obj/${typeof address === "string" ? address : address()}`,
|
2021-04-04 10:37:19 +02:00
|
|
|
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(() => {
|
2021-04-04 11:58:23 +02:00
|
|
|
const addr = typeof address === "string" ? address : address();
|
|
|
|
return entries.value.filter(([_, e]) => e.entity === addr);
|
2021-04-04 10:37:19 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const backlinks = computed(() => {
|
2021-04-04 11:58:23 +02:00
|
|
|
const addr = typeof address === "string" ? address : address();
|
|
|
|
return entries.value.filter(([_, e]) => e.entity !== addr);
|
2021-04-04 10:37:19 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
entries,
|
|
|
|
attributes,
|
|
|
|
backlinks,
|
|
|
|
data,
|
|
|
|
error,
|
|
|
|
mutate
|
|
|
|
}
|
|
|
|
}
|