discourse-translator/assets/javascripts/discourse/components/show-original-content.gjs
Natalie Tay 7ebee972ab
FIX: Show toggle button even when original content is displayed (#275)
This commit returns a proper attribute indicating whether the toggle button should be shown or not depending on whether the topic has translations for the user. This is independent of whether the user is seeing translated content or not.
2025-04-15 18:48:20 +08:00

59 lines
1.5 KiB
Text

import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { action } from "@ember/object";
import { service } from "@ember/service";
import DButton from "discourse/components/d-button";
import concatClass from "discourse/helpers/concat-class";
import cookie, { removeCookie } from "discourse/lib/cookie";
const SHOW_ORIGINAL_COOKIE = "discourse-translator-show-original";
const SHOW_ORIGINAL_COOKIE_EXPIRY = 30;
export default class ShowOriginalContent extends Component {
static shouldRender(args) {
return args.topic.show_translation_toggle;
}
@service router;
@tracked showingOriginal = false;
constructor() {
super(...arguments);
this.showingOriginal = cookie(SHOW_ORIGINAL_COOKIE);
}
@action
async showOriginal() {
if (this.showingOriginal) {
removeCookie(SHOW_ORIGINAL_COOKIE, { path: "/" });
} else {
cookie(SHOW_ORIGINAL_COOKIE, true, {
path: "/",
expires: SHOW_ORIGINAL_COOKIE_EXPIRY,
});
}
this.router.refresh();
}
get title() {
return this.showingOriginal
? "translator.content_not_translated"
: "translator.content_translated";
}
<template>
<div class="discourse-translator_toggle-original">
<DButton
@icon="language"
@title={{this.title}}
class={{concatClass
"btn btn-default"
(unless this.showingOriginal "active")
}}
@action={{this.showOriginal}}
/>
</div>
</template>
}