const SEARCH_API_KEY = props.searchApiKey ?? "0e42c01107b8f555a41bcc0fa7f2a4df"; const APPLICATION_ID = props.appId ?? "B6PI9UKKJT"; const INDEX = props.index ?? "prod_near-social-feed"; const API_URL = props.apiUrl ?? `https://${APPLICATION_ID}-dsn.algolia.net/1/indexes/${INDEX}/query?`; const INITIAL_PAGE = props.initialPage ?? 0; const facets = props.facets ?? ["All", "Users", "Apps", "Components", "Posts"]; const showHeader = props.showHeader ?? true; const showSearchBar = props.showSearchBar ?? true; const showPagination = props.showPagination ?? true; const userId = props.accountId ?? context.accountId; const componentsUrl = `/#/calebjacob.near/widget/ComponentsPage`; const peopleUrl = `/#/calebjacob.near/widget/PeoplePage`; //*********SEARCH FUNCTIONS ******** */ // Reset Search Results const resetSearcheHits = () => { console.log("reset search"); State.update({ currentPage: 0, search: undefined, paginate: undefined, facet: undefined, }); }; // updates search params as the user enters in a search value const writeStateTerm = (term) => { console.log("write term"); State.update({ term, }); if (term === "") { resetSearcheHits(); } }; writeStateTerm("dorgon"); // creates an array of profiles const profiles = (records) => { const profiles = []; for (const [i, record] of records ?? []) { profiles.push({ accountId: record.author, searchPosition: i, }); } return profiles; }; // creates an array of objects that provide the details of the loaded posts const posts = (content, postType) => { const posts = []; for (const [i, post] of content || []) { const accountId = post.author; const blockHeight = post.objectID.split("/").slice(-1)[0]; const postContent = { type: "md", text: post.content, }; const headerStyling = postType === "post" ? "border rounded-4 p-3 pb-1" : "pt-3 border-top pb-2"; posts.push({ accountId, blockHeight, postContent, postType, headerStyling, searchPosition: i, }); } return posts; }; // creates an array of components const components = (records) => { const components = []; for (const [i, component] of records || []) { const idParts = component.objectID.split("/"); const widgetName = idParts[idParts.length - 1]; const accountId = component.author; components.push({ accountId, widgetName, searchPosition: i, }); } return components; }; const categorizeSearchHits = (rawResp) => { const results = {}; for (const [i, result] of rawResp.hits?.entries()) { const { categories: categories_raw } = result; if (categories_raw.length > 1) { categories_raw.sort(); } const categories = categories_raw.join(", "); results[categories] = results[categories] || []; results[categories].push([i + 1, result]); } return { results, hitsTotal: rawResp.nbHits, hitsPerPage: rawResp.hitsPerPage, }; }; const debounce = (callable, timeout) => { return (args) => { clearTimeout(state.timer); State.update({ timer: setTimeout(() => callable(args), timeout ?? 50), }); }; }; const fetchSearchHits = (query, { pageNumber, configs, optionalFilters }) => { configs = configs ?? configsPerFacet(state.facet); let body = { query, page: pageNumber ?? 0, optionalFilters: optionalFilters ?? [ "categories:profile<score=3>", "categories:widget<score=2>", "categories:post<score=1>", "categories:comment<score=0>", ], clickAnalytics: true, ...configs, }; return asyncFetch(API_URL, { body: JSON.stringify(body), headers: { "Content-Type": "application/json; charset=UTF-8", "X-Algolia-Api-Key": SEARCH_API_KEY, "X-Algolia-Application-Id": APPLICATION_ID, }, method: "POST", }); }; const updateSearchHits = debounce(({ term, pageNumber, configs }) => { fetchSearchHits(term, { pageNumber, configs }).then((resp) => { const { results, hitsTotal, hitsPerPage } = categorizeSearchHits(resp.body); State.update({ search: { profiles: profiles(results["profile"]), components: components(results["widget"]), postsAndComments: posts(results["post"], "post").concat( posts(results["comment, post"], "comment") ), }, currentPage: 0, paginate: { hitsTotal, hitsPerPage, }, queryID: resp.body.queryID, }); }); }); const onSearchChange = ({ term }) => { console.log("search change"); writeStateTerm(term); updateSearchHits({ term, pageNumber: INITIAL_PAGE }); }; const onPageChange = (pageNumber) => { const algoliaPageNumber = pageNumber - 1; if (algoliaPageNumber === state.currentPage) { console.log(`Selected the same page number as before: ${pageNumber}`); return; } // Need to clear out old search data otherwise we'll get multiple entries // from the previous pages as well. Seems to be cache issue on near.social. State.update({ search: undefined, currentPage: algoliaPageNumber, }); updateSearchHits({ term: state.term, pageNumber: algoliaPageNumber }); }; const FACET_TO_CATEGORY = { Users: "profile", Apps: "app", Components: "widget", Posts: "post", }; const searchFilters = (facet) => { const category = FACET_TO_CATEGORY[facet]; let filters = category ? `categories:${category}` : undefined; if (category === "post") { filters = `(${filters} OR categories:comment)`; } if (category === "app") { filters = `(${filters} OR tags:app)`; } if (filters) { filters = `${filters} AND `; } filters = `${filters}NOT author:hypefairy.near AND NOT _tags:hidden`; return filters; }; const restrictSearchable = (facet) => { const category = FACET_TO_CATEGORY[facet]; let restrictSearchableAttrs = undefined; if (category === "post") { // Only the content should be searchable when the posts facet is selected. restrictSearchableAttrs = ["content"]; } return restrictSearchableAttrs; }; const configsPerFacet = (facet) => { return { filters: searchFilters(facet), restrictSearchableAttributes: restrictSearchable(facet), }; }; const onFacetClick = (facet) => { if (facet === state.facet) { console.log("Clicked the same facet"); return; } State.update({ facet, }); updateSearchHits({ term: state.term, configs: configsPerFacet(facet), }); }; const onSearchResultClick = ({ searchPosition, objectID, eventName }) => { const position = searchPosition + state.currentPage * state.paginate.hitsPerPage; const event = { type: "clickedObjectIDsAfterSearch", data: { eventName, userToken: userId.replace(".", "+"), queryID: state.queryID, objectIDs: [objectID], positions: [position], timestamp: Date.now(), }, }; setTimeout(() => { // This will trigger the Insights widget: State.update({ event }); }, 50); }; return ( <div> <Widget src="dorgon108.near/widget/SearchPopup" hi="hi" props={{ sayHi: sayHi, resetSearchHits: resetSearcheHits, writeStateTerm: writeStateTerm, profiles: profiles, posts: posts, components: components, categorizeSearchHits: categorizeSearchHits, debounce: debounce, fetchSearchHits: fetchSearchHits, updateSearchHits: updateSearchHits, onSearchChange: onSearchChange, onPageChange: onPageChange, searchFilters: searchFilters, restrictSearchable: restrictSearchable, configsPerFacet: configsPerFacet, onFacetClick: onFacetClick, onSearchResultClick: onSearchResultClick, facets: facets, }} /> </div> );