Skip to content

Commit

Permalink
Handle button visibility in one place
Browse files Browse the repository at this point in the history
  • Loading branch information
RisingOrange committed Jan 16, 2025
1 parent 599e06b commit e6b79d4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 43 deletions.
73 changes: 36 additions & 37 deletions ankihub/gui/reviewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,7 @@ def _inject_ankihub_features_and_setup_sidebar(
if not isinstance(context, Reviewer):
return

reviewer_button_js = get_reviewer_buttons_js(
theme=_ankihub_theme(),
enabled_buttons=_get_enabled_buttons_list(),
)
reviewer_button_js = get_reviewer_buttons_js(theme=_ankihub_theme())
web_content.body += f"<script>{reviewer_button_js}</script>"

global reviewer_sidebar
Expand All @@ -419,24 +416,6 @@ def _inject_ankihub_features_and_setup_sidebar(
reviewer_sidebar.set_on_auth_failure_hook(_handle_auth_failure)


def _get_enabled_buttons_list() -> List[str]:
result = []

feature_flags = config.get_feature_flags()

if feature_flags.get("chatbot"):
if config.public_config.get("ankihub_ai_chatbot"):
result.append("chatbot")

if feature_flags.get("mh_integration"):
if _get_enabled_steps_for_resource_type(ResourceType.BOARDS_AND_BEYOND):
result.append("b&b")
if _get_enabled_steps_for_resource_type(ResourceType.FIRST_AID):
result.append("fa4")

return result


def _related_ah_deck_has_note_embeddings(note: Note) -> bool:
ah_did_of_note = ankihub_db.ankihub_did_for_anki_nid(note.id)
ah_dids_of_note_type = ankihub_db.ankihub_dids_for_note_type(note.mid)
Expand Down Expand Up @@ -518,33 +497,53 @@ def _notify_reviewer_buttons_of_card_change(card: Card) -> None:
bb_count = len(_get_resources(note.tags, ResourceType.BOARDS_AND_BEYOND))
fa_count = len(_get_resources(note.tags, ResourceType.FIRST_AID))

visible_button_names = []

show_chatbot = _related_ah_deck_has_note_embeddings(card.note())
if show_chatbot:
visible_button_names.append("chatbot")

show_mh_buttons = _is_anking_deck(aqt.mw.reviewer.card)
if show_mh_buttons:
visible_button_names.extend(
[
ResourceType.BOARDS_AND_BEYOND.value,
ResourceType.FIRST_AID.value,
]
)
visible_buttons = _get_enabled_buttons() & _get_relevant_buttons_for_card(card)

js = _wrap_with_reviewer_buttons_check(
f"""
ankihubReviewerButtons.updateButtons(
{bb_count},
{fa_count},
{json.dumps(visible_button_names)}
{json.dumps(list(visible_buttons))}
);
"""
)
aqt.mw.reviewer.web.eval(js)


def _get_enabled_buttons() -> Set[str]:
result = set()
feature_flags = config.get_feature_flags()

if feature_flags.get("chatbot") and config.public_config.get("ankihub_ai_chatbot"):
result.add(SidebarPageType.CHATBOT.value)

if feature_flags.get("mh_integration"):
if _get_enabled_steps_for_resource_type(ResourceType.BOARDS_AND_BEYOND):
result.add(SidebarPageType.BOARDS_AND_BEYOND.value)
if _get_enabled_steps_for_resource_type(ResourceType.FIRST_AID):
result.add(SidebarPageType.FIRST_AID.value)

return result


def _get_relevant_buttons_for_card(card: Card) -> Set[str]:
result = set()

show_chatbot = _related_ah_deck_has_note_embeddings(card.note())
if show_chatbot:
result.add(SidebarPageType.CHATBOT.value)

show_mh_buttons = _is_anking_deck(aqt.mw.reviewer.card)
if show_mh_buttons:
result |= {
SidebarPageType.BOARDS_AND_BEYOND.value,
SidebarPageType.FIRST_AID.value,
}

return result


def _show_resources_for_current_card(resource_type: ResourceType) -> None:
tags = aqt.mw.reviewer.card.note().tags
resources = _get_resources(tags, resource_type)
Expand Down
4 changes: 1 addition & 3 deletions ankihub/gui/web/reviewer_buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class AnkiHubReviewerButtons {
this.isPremiumOrTrialing = "{{ IS_PREMIUM_OR_TRIALING }}" == "True";
this.bbCount = 0;
this.faCount = 0;
this.enabledButtons = "{{ ENABLED_BUTTONS }}".split(",");

this.colorButtonLight = "#F9FAFB";
this.colorButtonSelectedLight = "#C7D2FE";
Expand Down Expand Up @@ -45,8 +44,6 @@ class AnkiHubReviewerButtons {
},
]

this.buttonsData = this.buttonsData.filter(buttonData => this.enabledButtons.includes(buttonData.name));

this.setupButtons();
}

Expand Down Expand Up @@ -299,6 +296,7 @@ class AnkiHubReviewerButtons {
for (const buttonData of this.buttonsData) {
buttonData.visible = visibleButtons.includes(buttonData.name)
}

const visibleButtonElements = this.getVisibleButtons();

// Hide invisible buttons
Expand Down
4 changes: 1 addition & 3 deletions ankihub/gui/web/templates.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pathlib
from typing import List

from jinja2 import Environment, FileSystemLoader, select_autoescape

Expand All @@ -25,12 +24,11 @@ def get_header_webview_html(
)


def get_reviewer_buttons_js(theme: str, enabled_buttons: List[str]) -> str:
def get_reviewer_buttons_js(theme: str) -> str:
client = AnkiHubClient()
return env.get_template("reviewer_buttons.js").render(
{
"THEME": theme,
"ENABLED_BUTTONS": ",".join(enabled_buttons),
"IS_PREMIUM_OR_TRIALING": str(client.is_premium_or_trialing()),
}
)
Expand Down

0 comments on commit e6b79d4

Please sign in to comment.