discourse-ai/assets/javascripts/discourse/components/ai-gist-toggle.gjs

94 lines
2.7 KiB
Plaintext
Raw Normal View History

import Component from "@glimmer/component";
import { concat, fn } from "@ember/helper";
import { action } from "@ember/object";
import { service } from "@ember/service";
import DButton from "discourse/components/d-button";
import DropdownMenu from "discourse/components/dropdown-menu";
2024-11-19 15:33:34 -05:00
import bodyClass from "discourse/helpers/body-class";
import icon from "discourse-common/helpers/d-icon";
import i18n from "discourse-common/helpers/i18n";
import DMenu from "float-kit/components/d-menu";
2024-11-19 15:33:34 -05:00
import eq from "truth-helpers/helpers/eq";
export default class AiGistToggle extends Component {
2024-11-19 15:33:34 -05:00
@service gists;
get buttons() {
return [
{
2024-11-19 15:33:34 -05:00
id: "table",
label: "discourse_ai.summarization.topic_list_layout.button.compact",
icon: "discourse-table",
},
{
2024-11-19 15:33:34 -05:00
id: "table-ai",
label: "discourse_ai.summarization.topic_list_layout.button.expanded",
icon: "discourse-table-sparkles",
description:
"discourse_ai.summarization.topic_list_layout.button.expanded_description",
},
];
}
2024-11-19 15:33:34 -05:00
get selectedOptionId() {
return this.gists.get("preference");
}
get currentButton() {
const buttonPreference = this.buttons.find(
(button) => button.id === this.selectedOptionId
);
return buttonPreference || this.buttons[0];
}
@action
onRegisterApi(api) {
this.dMenu = api;
}
@action
onSelect(optionId) {
2024-11-19 15:33:34 -05:00
this.gists.setPreference(optionId);
this.dMenu.close();
}
<template>
2024-11-19 15:33:34 -05:00
{{#if this.gists.shouldShow}}
{{bodyClass (concat "topic-list-layout-" this.gists.preference)}}
<DMenu
@modalForMobile={{true}}
@autofocus={{true}}
2024-11-19 15:33:34 -05:00
@identifier="topic-list-layout"
@onRegisterApi={{this.onRegisterApi}}
2024-11-19 15:33:34 -05:00
@triggerClass="btn-default btn-icon"
>
<:trigger>
2024-11-19 15:33:34 -05:00
{{icon this.currentButton.icon}}
</:trigger>
<:content>
<DropdownMenu as |dropdown|>
{{#each this.buttons as |button|}}
<dropdown.item>
<DButton
@label={{button.label}}
@icon={{button.icon}}
2024-11-19 15:33:34 -05:00
class="btn-transparent
{{if button.description '--with-description'}}
{{if (eq this.currentButton.id button.id) '--active'}}"
@action={{fn this.onSelect button.id}}
2024-11-19 15:33:34 -05:00
>
{{#if button.description}}
<div class="btn__description">
{{i18n button.description}}
</div>
{{/if}}
</DButton>
</dropdown.item>
{{/each}}
</DropdownMenu>
</:content>
</DMenu>
{{/if}}
</template>
}