2021-04-04 10:37:19 +02:00
|
|
|
import { IEntry, ListingResult } from "@/types/base";
|
|
|
|
import { fetcher } from "@/utils";
|
|
|
|
import useSWRV from "swrv";
|
2021-06-07 00:40:07 +02:00
|
|
|
import { computed, ComputedRef, Ref } from "vue";
|
2021-04-04 10:37:19 +02:00
|
|
|
|
|
|
|
|
2021-06-07 01:51:07 +02:00
|
|
|
export function useEntity(address: string | (() => string), condition?: () => Boolean) {
|
2021-04-04 10:37:19 +02:00
|
|
|
const { data, error, mutate } = useSWRV<ListingResult, unknown>(
|
2021-06-07 01:51:07 +02:00
|
|
|
() => (condition === undefined || condition()) ? `/api/obj/${typeof address === "string" ? address : address()}` : null,
|
2021-07-06 00:11:39 +02:00
|
|
|
fetcher, { revalidateOnFocus: false }
|
2021-04-04 10:37:19 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2021-06-07 00:40:07 +02:00
|
|
|
}
|
|
|
|
|
2021-06-19 17:01:43 +02:00
|
|
|
|
|
|
|
export function query(query: string | (() => string), condition?: () => Boolean) {
|
|
|
|
const { data, error, mutate } = useSWRV<ListingResult, unknown>(
|
|
|
|
() => (condition === undefined || condition()) ? `/api/obj?query=${typeof query === "string" ? query : query()}` : null,
|
2021-07-06 00:11:39 +02:00
|
|
|
fetcher, { revalidateOnFocus: false }
|
2021-06-19 17:01:43 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 00:40:07 +02:00
|
|
|
interface EntityIdentification {
|
|
|
|
type: string;
|
|
|
|
value: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function identify(attributes: ComputedRef<[string, IEntry][]>): ComputedRef<EntityIdentification[]> {
|
|
|
|
// Get all identities of the object
|
2021-06-07 01:48:32 +02:00
|
|
|
const isEntries = computed(() => {
|
|
|
|
return attributes.value
|
|
|
|
.filter(([_, entry]) => entry.attribute === "IS")
|
|
|
|
.map(([_, entry]) => entry.value.c);
|
|
|
|
})
|
2021-06-07 00:40:07 +02:00
|
|
|
|
2021-06-07 01:51:07 +02:00
|
|
|
|
2021-06-07 00:40:07 +02:00
|
|
|
// Out of those, retrieve their TYPE_ID entries
|
2021-07-03 21:20:20 +02:00
|
|
|
const { data: typeIdListing } = query(() => {
|
|
|
|
return isEntries.value && `(matches (in ${isEntries.value.map((e) => `"${e}"`).join(" ")}) "TYPE_ID" ?)`;
|
|
|
|
});
|
2021-06-07 01:48:32 +02:00
|
|
|
|
|
|
|
const typeIdAttributes: ComputedRef<[string, string][]> = computed(() => {
|
|
|
|
return Object.values(typeIdListing.value || {}).map((entry) => {
|
|
|
|
return [entry.entity, entry.value.c];
|
|
|
|
});
|
2021-06-07 00:40:07 +02:00
|
|
|
});
|
|
|
|
|
2021-06-07 01:51:07 +02:00
|
|
|
|
|
|
|
|
2021-06-07 00:40:07 +02:00
|
|
|
// Finally, filter own object's attributes according to TYPE_IDs
|
|
|
|
return computed(() => {
|
2021-06-07 01:48:32 +02:00
|
|
|
// For each identity/TYPE_ID pair
|
|
|
|
return typeIdAttributes.value
|
|
|
|
.map(([type, attrName]) => {
|
2021-06-07 00:40:07 +02:00
|
|
|
// And each associated TYPE_ID attribute...
|
2021-06-07 01:48:32 +02:00
|
|
|
// return own matchin attributes
|
|
|
|
return attributes.value
|
|
|
|
.filter(([_, e]) => e.attribute === attrName)
|
2021-06-07 00:40:07 +02:00
|
|
|
.map(([_, attr]) => {
|
|
|
|
return {
|
|
|
|
type,
|
|
|
|
value: attr.value.c
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}).flat();
|
|
|
|
});
|
2021-06-19 17:01:43 +02:00
|
|
|
}
|
2021-06-26 17:55:26 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|