diff --git a/js/src/forum/SearchSources/DiscussionsSearchSource.tsx b/js/src/forum/SearchSources/DiscussionsSearchSource.tsx new file mode 100644 index 0000000..ca880e1 --- /dev/null +++ b/js/src/forum/SearchSources/DiscussionsSearchSource.tsx @@ -0,0 +1,77 @@ +import app from 'flarum/forum/app'; +import highlight from 'flarum/common/helpers/highlight'; +import LinkButton from 'flarum/common/components/LinkButton'; +import Link from 'flarum/common/components/Link'; +import { SearchSource } from 'flarum/forum/components/Search'; +import type Mithril from 'mithril'; + +/** + * The `DiscussionsSearchSource` finds and displays discussion search results in + * the search dropdown. + */ +export default class DiscussionsSearchSource implements SearchSource { + /** + * Map of lowercase search queries to their respective model results + */ + protected results = new Map(); + + /** + * Model name used for assembling API endpoint URL, and for frontend DOM. + */ + private type = 'discussions'; + + async search(query: string): Promise { + query = query.toLowerCase(); + + this.results.set(query, []); + + const params = { + filter: { q: query }, + page: { limit: 3 }, + }; + + // Construct API search URI + const url = `${app.forum.attribute('apiUrl')}/blomstra/search/${this.type}`; + + // Make API GET request + const results = await app.request({ params, url, method: 'GET' }); + + // Parse API response into models and push to store + const models = app.store.pushPayload(results); + + // Add models to results map + this.results.set(query, models); + } + + view(query: string): Mithril.Children { + query = query.toLowerCase(); + + // Get results from map + const queryResults = this.results.get(query) || []; + + const results = queryResults.map((discussion: any) => { + // const mostRelevantPost = discussion.mostRelevantPost(); + + return ( +
  • + +
    {highlight(discussion.title(), query)}
    + {/* {!!mostRelevantPost &&
    {highlight(mostRelevantPost.contentPlain(), query, 100)}
    } */} + +
  • + ); + }); + + return ( + <> +
  • {app.translator.trans('core.forum.search.discussions_heading')}
  • , +
  • + + {app.translator.trans('core.forum.search.all_discussions_button', { query })} + +
  • + {results} + + ); + } +} diff --git a/js/src/forum/Source.tsx b/js/src/forum/Source.tsx deleted file mode 100644 index d2d154f..0000000 --- a/js/src/forum/Source.tsx +++ /dev/null @@ -1,55 +0,0 @@ -import app from 'flarum/app'; -import highlight from 'flarum/common/helpers/highlight'; -import LinkButton from 'flarum/common/components/LinkButton'; -import Link from 'flarum/common/components/Link'; -import { SearchSource } from 'flarum/forum/components/Search'; -import type Mithril from 'mithril'; - -/** - * The `DiscussionsSearchSource` finds and displays discussion search results in - * the search dropdown. - */ -export default class Source implements SearchSource { - protected results = new Map(); - - search(query: string) { - query = query.toLowerCase(); - - this.results.set(query, []); - - const params = { - filter: { q: query }, - page: { limit: 3 }, - include: 'mostRelevantPost', - }; - - return app.request(app.forum.attribute('apiUrl') + '/blomstra/search', params).then((results) => this.results.set(query, results)); - } - - view(query: string): Array { - query = query.toLowerCase(); - - const results = (this.results.get(query) || []).map((discussion: unknown) => { - const mostRelevantPost = discussion.mostRelevantPost(); - - return ( -
  • - -
    {highlight(discussion.title(), query)}
    - {mostRelevantPost ?
    {highlight(mostRelevantPost.contentPlain(), query, 100)}
    : ''} - -
  • - ); - }) as Array; - - return [ -
  • {app.translator.trans('core.forum.search.discussions_heading')}
  • , -
  • - - {app.translator.trans('core.forum.search.all_discussions_button', { query })} - -
  • , - ...results, - ]; - } -}