This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/plugins/chat/assets/javascripts/discourse/components/chat-selection-manager.js
Roman Rizzi 5c699e4384
DEV: Pass messageId as a dynamic segment instead of a query param (#20013)
* DEV: Rnemae channel path to just c

Also swap the channel id and channel slug params to be consistent with core.

* linting

* channel_path

* Drop slugify helper and channel route without slug

* Request slug and route models through the channel model if possible

* DEV: Pass messageId as a dynamic segment instead of a query param

* Ensure change is backwards-compatible

* drop query param from oneboxes

* Correctly extract channelId from routes

* Better route organization using siblings for regular and near-message

* Ensures sessions are unique even when using parallelism

* prevents didReceiveAttrs to clear input mid test

* we disable animations in capybara so sometimes the message was barely showing

* adds wait

* ensures finished loading

* is it causing more harm than good?

* this check is slowing things for no reason

* actually target the button

* more resilient select chat message

* apply similar fix to bookmark

* fix

---------

Co-authored-by: Joffrey JAFFEUX <j.jaffeux@gmail.com>
2023-02-01 12:39:23 -03:00

129 lines
3.6 KiB
JavaScript

import Component from "@ember/component";
import { action, computed } from "@ember/object";
import showModal from "discourse/lib/show-modal";
import { clipboardCopyAsync } from "discourse/lib/utilities";
import { getOwner } from "discourse-common/lib/get-owner";
import { ajax } from "discourse/lib/ajax";
import { isTesting } from "discourse-common/config/environment";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { schedule } from "@ember/runloop";
import { inject as service } from "@ember/service";
import getURL from "discourse-common/lib/get-url";
import { bind } from "discourse-common/utils/decorators";
export default class AdminCustomizeColorsShowController extends Component {
@service router;
tagName = "";
chatChannel = null;
selectedMessageIds = null;
chatCopySuccess = false;
showChatCopySuccess = false;
cancelSelecting = null;
canModerate = false;
@computed("selectedMessageIds.length")
get anyMessagesSelected() {
return this.selectedMessageIds.length > 0;
}
@computed("chatChannel.isDirectMessageChannel", "canModerate")
get showMoveMessageButton() {
return !this.chatChannel.isDirectMessageChannel && this.canModerate;
}
@bind
async generateQuote() {
const response = await ajax(
getURL(`/chat/${this.chatChannel.id}/quote.json`),
{
data: { message_ids: this.selectedMessageIds },
type: "POST",
}
);
return new Blob([response.markdown], {
type: "text/plain",
});
}
@action
openMoveMessageModal() {
showModal("chat-message-move-to-channel-modal").setProperties({
sourceChannel: this.chatChannel,
selectedMessageIds: this.selectedMessageIds,
});
}
@action
async quoteMessages() {
let quoteMarkdown;
try {
const quoteMarkdownBlob = await this.generateQuote();
quoteMarkdown = await quoteMarkdownBlob.text();
} catch (error) {
popupAjaxError(error);
}
const container = getOwner(this);
const composer = container.lookup("controller:composer");
const openOpts = {};
if (this.chatChannel.isCategoryChannel) {
openOpts.categoryId = this.chatChannel.chatable_id;
}
if (this.site.mobileView) {
// go to the relevant chatable (e.g. category) and open the
// composer to insert text
if (this.chatChannel.chatable_url) {
this.router.transitionTo(this.chatChannel.chatable_url);
}
await composer.focusComposer({
fallbackToNewTopic: true,
insertText: quoteMarkdown,
openOpts,
});
} else {
// open the composer and insert text, reply to the current
// topic if there is one, use the active draft if there is one
const topic = container.lookup("controller:topic");
await composer.focusComposer({
fallbackToNewTopic: true,
topic: topic?.model,
insertText: quoteMarkdown,
openOpts,
});
}
}
@action
async copyMessages() {
try {
this.set("chatCopySuccess", false);
if (!isTesting()) {
// clipboard API throws errors in tests
await clipboardCopyAsync(this.generateQuote);
this.set("chatCopySuccess", true);
}
this.set("showChatCopySuccess", true);
schedule("afterRender", () => {
const element = document.querySelector(".chat-selection-message");
element?.addEventListener("animationend", () => {
if (this.isDestroying || this.isDestroyed) {
return;
}
this.set("showChatCopySuccess", false);
});
});
} catch (error) {
popupAjaxError(error);
}
}
}