upend/webui/src/util/search.ts

46 lines
1.2 KiB
TypeScript

import type { UpEntry } from "upend";
import type { ListingResult } from "upend/types";
import { query as queryFn, queryOnce } from "../lib/entity";
export function baseSearch(query: string) {
return queryFn(
`(or (matches ? (contains "${query}") ?) (matches ? ? (contains "${query}")))`
);
}
export function baseSearchOnce(query: string) {
return queryOnce(
`(or (matches ? (contains "${query}") ?) (matches ? ? (contains "${query}")))`
);
}
export async function getObjects(
entries: UpEntry[],
limit = 50
): Promise<[string, string][]> {
return entries
.slice(0, limit)
.filter((e) => e.attribute == "LBL")
.map((e) => [e.entity, String(e.value.c)] as [string, string]);
}
export async function createLabelled(label: string) {
const response = await fetch(`/api/obj`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
attribute: "LBL",
value: {
t: "String",
c: label,
},
}),
});
if (!response.ok) {
throw new Error(`Failed to create object: ${await response.text()}`);
}
const result = (await response.json()) as ListingResult;
const address = Object.values(result)[0].entity;
return address;
}