Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEV: Update in preparation for core header changes #96

Merged
merged 3 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import didInsert from "@ember/render-modifiers/modifiers/did-insert";
import { Promise } from "rsvp";
import { h } from "virtual-dom";
import { apiInitializer } from "discourse/lib/api";
import loadScript from "discourse/lib/load-script";
import { withPluginApi } from "discourse/lib/plugin-api";
import DiscourseURL from "discourse/lib/url";
import { isDevelopment } from "discourse-common/config/environment";
import I18n from "I18n";
Expand Down Expand Up @@ -171,9 +171,9 @@ function initializeAutocomplete(options) {
<img
class="hit-user-avatar"
src="${options.imageBaseURL}${item.avatar_template.replace(
"{size}",
50
)}"
"{size}",
50
)}"
/>
</div>
<div class="hit-user-right">
Expand Down Expand Up @@ -281,59 +281,39 @@ function initializeAutocomplete(options) {
return autocompleteSearch;
}

export default {
name: "discourse-algolia",
export default apiInitializer("0.8", (api) => {
const siteSettings = api.container.lookup("service:site-settings");
const currentUser = api.getCurrentUser();
const shouldDisplay = () =>
siteSettings.algolia_enabled &&
siteSettings.algolia_autocomplete_enabled &&
(!siteSettings.login_required || currentUser);

initialize() {
withPluginApi("0.8.8", (api) => {
api.createWidget("algolia", {
tagName: "li.algolia-holder",
let search;

didRenderWidget() {
if (
this.siteSettings.algolia_enabled &&
this.siteSettings.algolia_autocomplete_enabled
) {
Promise.all([
loadScript(
"/plugins/discourse-algolia/javascripts/autocomplete.js"
),
loadScript(
"/plugins/discourse-algolia/javascripts/algoliasearch.js"
),
]).then(() => {
document.body.classList.add("algolia-enabled");
this._search = initializeAutocomplete({
algoliaApplicationId: this.siteSettings.algolia_application_id,
algoliaSearchApiKey: this.siteSettings.algolia_search_api_key,
imageBaseURL: "",
debug: isDevelopment(),
});
});
}
},

willRerenderWidget() {
if (this._search) {
this._search.destroy();
}
},
function renderAlgolia() {
search?.destroy();

html() {
return [h(".algolia-search"), h(".algolia-autocomplete")];
},
});

api.decorateWidget("header-icons:before", function (helper) {
if (
helper.widget.siteSettings.algolia_enabled &&
helper.widget.siteSettings.algolia_autocomplete_enabled &&
(!helper.widget.siteSettings.login_required ||
helper.widget.currentUser)
) {
return helper.attach("algolia");
}
Promise.all([
loadScript("/plugins/discourse-algolia/javascripts/autocomplete.js"),
loadScript("/plugins/discourse-algolia/javascripts/algoliasearch.js"),
]).then(() => {
document.body.classList.add("algolia-enabled");
search = initializeAutocomplete({
algoliaApplicationId: siteSettings.algolia_application_id,
algoliaSearchApiKey: siteSettings.algolia_search_api_key,
imageBaseURL: "",
debug: isDevelopment(),
});
});
},
};
}

api.headerIcons.add("algolia", <template>
{{#if (shouldDisplay)}}
<li class="algolia-holder" {{didInsert renderAlgolia}}>
<div class="algolia-search"></div>
<div class="algolia-autocomplete"></div>
</li>
{{/if}}
</template>);
});
Loading
Loading