Add logging and fix channel merging and removing

This commit is contained in:
2024-08-24 00:21:54 +02:00
parent 7055713d4b
commit 3575c910f4
14 changed files with 163 additions and 7 deletions

View File

@@ -85,7 +85,7 @@ export const API = {
},
async mergeChannels(channelId: string, targetChannelId: string) {
await API.request("POST", "merge-channels", { channelId, targetChannelId });
await API.request("PUT", `channels/${channelId}/merge`, { targetChannelId });
},
async search(query: string, channelId?: string) {

View File

@@ -3,8 +3,10 @@ import { showToast } from "../speech";
import { state } from "../state";
import { Button, TextInput } from "../ui";
import { Dialog } from "../ui/dialog";
import { MergeDialog } from "./merge-dialog";
import { RemoveDialog } from "./remove-dialog";
export class ChannelDialog extends Dialog<IChannel> {
export class ChannelDialog extends Dialog<IChannel | null> {
private channel: IChannel;
private nameField: TextInput;
private makeDefault: Button;
@@ -26,12 +28,15 @@ export class ChannelDialog extends Dialog<IChannel> {
this.mergeButton = new Button("Merge");
this.mergeButton.setPosition(40, 70, 10, 10);
this.mergeButton.onClick(() => {
showToast("Merge not implemented.");
this.mergeChannel();
});
if (state.channelList.channels.length === 1) {
this.mergeButton.setDisabled(true);
}
this.deleteButton = new Button("Delete");
this.deleteButton.setPosition(60, 70, 10, 10);
this.deleteButton.onClick(() => {
showToast("Delete not implemented.");
this.deleteChannel();
});
this.add(this.nameField);
this.add(this.makeDefault);
@@ -42,4 +47,22 @@ export class ChannelDialog extends Dialog<IChannel> {
return this.channel;
});
}
private async mergeChannel() {
const res = await new MergeDialog().open();
if (res) {
this.choose(this.channel);
} else {
return;
}
}
private async deleteChannel() {
const res = await new RemoveDialog(this.channel.id.toString()).open();
if (res) {
this.choose(null);
} else {
return;
}
}
}

View File

@@ -13,5 +13,10 @@ export class CreateChannelDialog extends Dialog<string> {
this.setOkAction(() => {
return this.nameField.getValue();
});
this.nameField.onKeyDown((key) => {
if (key === "Enter") {
this.choose(this.nameField.getValue());
}
});
}
}

View File

@@ -0,0 +1,51 @@
import { Button } from "../ui";
import { Dialog } from "../ui/dialog";
import { API } from "../api";
import { Dropdown } from "../ui/dropdown";
import { state } from "../state";
import { showToast } from "../speech";
export class MergeDialog extends Dialog<boolean> {
private channelList: Dropdown;
private mergeButton: Button;
private cancelButton: Button;
public constructor() {
super("Merge channels", false);
this.channelList = new Dropdown("Target channel", []);
this.channelList.setPosition(10, 10, 80, 20);
this.mergeButton = new Button("Merge");
this.mergeButton.setPosition(30, 30, 40, 30);
this.mergeButton.onClick(() => this.merge());
this.cancelButton = new Button("Cancel");
this.cancelButton.setPosition(30, 70, 40, 30);
this.cancelButton.onClick(() => this.cancel());
this.add(this.channelList);
this.add(this.mergeButton);
this.add(this.cancelButton);
this.setupChannelList();
}
private setupChannelList() {
this.channelList.clearOptions();
state.channelList.getChannels().forEach((channel) => {
if (channel.id !== state.currentChannel!.id) this.channelList.addOption(channel.id.toString(), channel.name);
})
}
private async merge() {
const currentChannel = state.currentChannel;
const target = this.channelList.getSelectedValue();
const targetChannel = state.getChannelById(parseInt(target));
console.log(currentChannel, targetChannel);
if (!targetChannel || !currentChannel) this.cancel();
try {
const res = await API.mergeChannels(currentChannel!.id.toString(), target);
currentChannel!.messages = [];
showToast("Channels were merged.");
this.choose(true);
} catch (e) {
showToast("Failed to merge channels: " + e);
this.choose(false);
}
}
}

View File

@@ -0,0 +1,39 @@
import { Button } from "../ui";
import { Dialog } from "../ui/dialog";
import { Text } from "../ui";
import { API } from "../api";
import { state } from "../state";
import { showToast } from "../speech";
export class RemoveDialog extends Dialog<boolean> {
private content: Text;
private confirmButton: Button;
private cancelButton: Button;
public constructor(channelId: string) {
super("Remove channel", false);
this.content = new Text("Are you sure you want to remove this channel?");
this.confirmButton = new Button("Remove");
this.confirmButton.setPosition(30, 30, 40, 30);
this.confirmButton.onClick(() => this.remove());
this.cancelButton = new Button("Cancel");
this.cancelButton.setPosition(30, 70, 40, 30);
this.cancelButton.onClick(() => this.cancel());
this.add(this.content);
this.add(this.confirmButton);
this.add(this.cancelButton);
}
private async remove() {
try {
const res = await API.deleteChannel(state.currentChannel!.id.toString());
state.removeChannel(state.currentChannel!);
showToast("Channel was removed.");
this.choose(true);
} catch (e) {
showToast("Failed to remove channel: " + e);
this.choose(false);
}
}
}

View File

@@ -13,6 +13,11 @@ export class SearchDialog extends Dialog<{channelId: number, messageId: number}>
super("Search for message", false);
this.searchField = new TextInput("Search query");
this.searchField.setPosition(5, 5, 80, 20);
this.searchField.onKeyDown((key) => {
if (key === "Enter") {
this.searchButton.click();
}
});
this.searchButton = new Button("Search");
this.searchButton.setPosition(85, 5, 10, 20);
this.searchButton.onClick(async () => {
@@ -38,5 +43,6 @@ export class SearchDialog extends Dialog<{channelId: number, messageId: number}>
itm.onClick(() => this.choose({ messageId: message.id, channelId: message.channelId! }));
this.resultsList.add(itm);
});
this.resultsList.focus();
}
}

View File

@@ -140,6 +140,21 @@ export class MainView extends View {
}
const d = new ChannelDialog(state.currentChannel!);
d.open().then((chan) => {
if (!chan) {
state.removeChannel(state.currentChannel!);
state.currentChannel = null;
this.updateChannelList();
state.save();
if (state.channelList.channels.length > 0) {
return this.switchChannel(state.channelList.channels[0].id.toString());
} else {
return this.createNewChannel();
}
}
if (chan.messages.length < 1) {
this.renderInitialMessageList(true);
this.syncMessages();
}
state.save();
this.updateChannelList();
});