2022-11-21 16:54:45 +00:00
|
|
|
// Note: Currently, we use the REST API for Operand because of some unpkg/webpack issues.
|
|
|
|
// In the future, we'd like to use the SDK (https://github.com/operandinc/typescript-sdk).
|
|
|
|
// If someone knows how to do this w/o breaking the Operand typescript-sdk for npm users,
|
|
|
|
// please let Morgan (@morgallant) and/or (@_jzhao) know! <3
|
2022-11-20 23:09:58 +00:00
|
|
|
|
|
|
|
const apiKey = "{{$.Site.Data.config.search.operandApiKey}}"
|
|
|
|
const indexId = "{{$.Site.Data.config.search.operandIndexId}}"
|
2022-11-21 16:54:45 +00:00
|
|
|
|
|
|
|
function parseSearchResults(searchResults) {
|
|
|
|
return searchResults.matches.map((m) => ({
|
|
|
|
content: m.content,
|
|
|
|
title: searchResults.objects[m.objectId].properties.properties._title.text,
|
|
|
|
url: searchResults.objects[m.objectId].properties.properties._url.text,
|
|
|
|
}))
|
|
|
|
}
|
2022-07-31 19:16:36 +00:00
|
|
|
|
|
|
|
async function searchContents(query) {
|
2022-11-21 16:54:45 +00:00
|
|
|
const result = await fetch("https://api.operand.ai/operand.v1.ObjectService/SearchWithin", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: `${apiKey}`,
|
|
|
|
"Operand-Index-ID": `${indexId}`,
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
query: query,
|
|
|
|
limit: 8,
|
|
|
|
}),
|
2022-11-20 23:09:58 +00:00
|
|
|
})
|
2022-11-21 16:54:45 +00:00
|
|
|
if (result.ok) {
|
|
|
|
return parseSearchResults(await result.json())
|
|
|
|
} else {
|
|
|
|
console.error(result)
|
|
|
|
}
|
2022-07-31 19:16:36 +00:00
|
|
|
}
|
|
|
|
|
2022-08-01 01:21:17 +00:00
|
|
|
function debounce(func, timeout = 200) {
|
2022-11-21 16:54:45 +00:00
|
|
|
let timer
|
2022-08-01 01:21:17 +00:00
|
|
|
return (...args) => {
|
|
|
|
clearTimeout(timer)
|
2022-11-21 16:54:45 +00:00
|
|
|
timer = setTimeout(() => {
|
|
|
|
func.apply(this, args)
|
|
|
|
}, timeout)
|
|
|
|
}
|
2022-08-01 01:21:17 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 16:54:45 +00:00
|
|
|
registerHandlers(
|
|
|
|
debounce((e) => {
|
|
|
|
let term = e.target.value
|
|
|
|
if (term !== "") {
|
|
|
|
searchContents(term).then((results) => displayResults(results))
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
)
|