2021-11-11 23:37:42 +01:00
|
|
|
// import { useSWR } from "sswr";
|
2022-08-01 21:08:34 +02:00
|
|
|
import { derived, type Readable } from "svelte/store";
|
2022-01-28 16:46:08 +01:00
|
|
|
import { UpListing, UpObject } from "upend";
|
2023-05-20 22:33:22 +02:00
|
|
|
import type { ListingResult, EntityListing, EntityInfo } from "upend/types";
|
2021-12-19 13:54:16 +01:00
|
|
|
import { useSWR } from "../util/fetch";
|
2023-05-22 20:57:06 +02:00
|
|
|
import api from "./api";
|
2023-06-05 12:56:33 +02:00
|
|
|
import debug from "debug";
|
2023-09-03 10:39:07 +02:00
|
|
|
const dbg = debug("kestrel:lib");
|
2021-11-11 23:37:42 +01:00
|
|
|
|
2021-12-19 13:54:16 +01:00
|
|
|
export function useEntity(address: string) {
|
2022-02-07 20:46:17 +01:00
|
|
|
const { data, error, revalidate } = useSWR<EntityListing, unknown>(
|
2023-08-01 22:02:52 +02:00
|
|
|
`${api.apiUrl}/obj/${address}`,
|
2021-11-11 23:37:42 +01:00
|
|
|
);
|
2021-12-19 13:54:16 +01:00
|
|
|
|
|
|
|
const entity: Readable<UpObject | undefined> = derived(data, ($listing) => {
|
|
|
|
if ($listing) {
|
2022-02-07 20:46:17 +01:00
|
|
|
const listing = new UpListing($listing.entries);
|
2021-12-19 19:20:09 +01:00
|
|
|
return listing.getObject(address);
|
2021-12-19 13:54:16 +01:00
|
|
|
}
|
2021-11-11 23:37:42 +01:00
|
|
|
});
|
|
|
|
|
2022-02-07 20:46:17 +01:00
|
|
|
const entityInfo: Readable<EntityInfo | undefined> = derived(
|
|
|
|
data,
|
|
|
|
($listing) => {
|
|
|
|
if ($listing) {
|
|
|
|
return $listing.entity;
|
|
|
|
}
|
2023-08-01 22:02:52 +02:00
|
|
|
},
|
2022-02-07 20:46:17 +01:00
|
|
|
);
|
|
|
|
|
2021-11-11 23:37:42 +01:00
|
|
|
return {
|
2021-12-19 13:54:16 +01:00
|
|
|
entity,
|
2022-02-07 20:46:17 +01:00
|
|
|
entityInfo,
|
2021-11-11 23:37:42 +01:00
|
|
|
error,
|
|
|
|
revalidate,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-02-06 22:27:56 +01:00
|
|
|
export function query(query: string) {
|
2023-06-05 12:56:33 +02:00
|
|
|
dbg(`Querying: ${query}`);
|
2021-11-11 23:37:42 +01:00
|
|
|
const { data, error, revalidate } = useSWR<ListingResult, unknown>(
|
2023-05-22 20:57:06 +02:00
|
|
|
`${api.apiUrl}/query`,
|
2023-08-01 22:02:52 +02:00
|
|
|
{ method: "POST", body: query },
|
2021-11-11 23:37:42 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
const result = derived(data, ($values) => {
|
2021-12-19 13:54:16 +01:00
|
|
|
return $values ? new UpListing($values) : undefined;
|
2021-11-11 23:37:42 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
result,
|
|
|
|
error,
|
|
|
|
revalidate,
|
|
|
|
};
|
|
|
|
}
|