| 3 | import { tracked } from '@glimmer/tracking'; |
| 4 | |
| 5 | export default class SearchService extends Service { |
| 6 | @service('algolia') _algoliaService; |
| 7 | @service('project') projectService; |
| 8 | |
| 9 | /** @type {?string} */ |
| 10 | #lastQueriedProjectVersion = null; |
| 11 | |
| 12 | @tracked results = []; |
| 13 | |
| 14 | get projectVersion() { |
| 15 | return this.projectService.version; |
| 16 | } |
| 17 | |
| 18 | search = restartableTask(async (query) => { |
| 19 | const projectVersion = this.projectVersion; |
| 20 | |
| 21 | const params = { |
| 22 | hitsPerPage: 15, |
| 23 | restrictSearchableAttributes: [ |
| 24 | 'hierarchy.lvl0', |
| 25 | 'hierarchy.lvl1', |
| 26 | 'hierarchy.lvl2', |
| 27 | ], |
| 28 | tagFilters: [`version:${projectVersion}`], |
| 29 | facetFilters: ['access:-private'], |
| 30 | }; |
| 31 | |
| 32 | const searchObj = { |
| 33 | indexName: 'methods', |
| 34 | query, |
| 35 | }; |
| 36 | |
| 37 | this.#lastQueriedProjectVersion = projectVersion; |
| 38 | |
| 39 | this.results = await this.doSearch(searchObj, params); |
| 40 | return this.results; |
| 41 | }); |
| 42 | |
| 43 | doSearch(searchObj, params) { |
| 44 | return this._algoliaService |
| 45 | .search(searchObj, params) |
| 46 | .then((results) => results.hits); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Whenever the version changes in service:project, the results in this |
| 51 | * service become stale. Presenting them any further could allow the user to |
| 52 | * undo their version change by clicking a stale link. |
| 53 | * @returns {boolean} |
| 54 | */ |
| 55 | hasStaleResults() { |
| 56 | return ( |
| 57 | this.#lastQueriedProjectVersion !== null && |
| 58 | this.projectVersion !== this.#lastQueriedProjectVersion |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | clearResults() { |