wip: Add ban and kick to context menu #69

Draft
sauceyred wants to merge 14 commits from context-menu-ban-kick into main
9 changed files with 117 additions and 40 deletions

20
app.vue
View file

@ -12,22 +12,20 @@ import type { ContextMenuInterface } from './types/interfaces';
const banner = useState("banner", () => false); const banner = useState("banner", () => false);
const contextMenu = useState<ContextMenuInterface>("contextMenu"); const contextMenu = useState<ContextMenuInterface>("contextMenu", () => ({ show: false, pointerX: 0, pointerY: 0, items: [] }));
onMounted(() => { onMounted(() => {
loadPreferredThemes() loadPreferredThemes()
document.removeEventListener("contextmenu", contextMenuHandler);
document.addEventListener("contextmenu", (e) => {
if (e.target instanceof Element && e.target.classList.contains("default-contextmenu")) return;
contextMenuHandler(e);
});
document.addEventListener("mousedown", (e) => { document.addEventListener("mousedown", (e) => {
if (e.target instanceof HTMLElement && e.target.closest("#context-menu")) return; if (e.target instanceof HTMLElement && e.target.classList.contains("context-menu-item")) return;
console.log("click"); console.log("click");
console.log("target:", e.target); console.log("target:", e.target);
console.log(e.target instanceof HTMLDivElement); console.log(e.target instanceof HTMLDivElement);
if (contextMenu.value.show) {
console.log("context menu is shown, hiding");
removeContextMenu(contextMenu); removeContextMenu(contextMenu);
}
if (e.target instanceof HTMLElement && e.target.classList.contains("message-text") && e.target.contentEditable) { if (e.target instanceof HTMLElement && e.target.classList.contains("message-text") && e.target.contentEditable) {
e.target.contentEditable = "false"; e.target.contentEditable = "false";
} }
@ -52,14 +50,6 @@ onMounted(() => {
}); });
}); });
function contextMenuHandler(e: MouseEvent) {
e.preventDefault();
//console.log("Opened context menu");
//createContextMenu(e, [
// { name: "Wah", callback: () => { return } }
//]);
}
</script> </script>
<style> <style>

View file

@ -1,5 +1,5 @@
<template> <template>
<div class="member-item" @click.prevent="showModalPopup" tabindex="0"> <div class="member-item" @click.prevent="showModalPopup" tabindex="0" @contextmenu="showContextMenu($event, contextMenu, menuItems)">
<Avatar :profile="props.member" class="member-avatar"/> <Avatar :profile="props.member" class="member-avatar"/>
<span class="member-display-name" :style="`color: ${generateIrcColor(props.member.user.uuid)}`"> <span class="member-display-name" :style="`color: ${generateIrcColor(props.member.user.uuid)}`">
{{ getDisplayName(props.member) }} {{ getDisplayName(props.member) }}
@ -11,14 +11,18 @@
<script lang="ts" setup> <script lang="ts" setup>
import { ModalProfilePopup } from '#components'; import { ModalProfilePopup } from '#components';
import type { GuildMemberResponse } from '~/types/interfaces'; import type { ContextMenuInterface, GuildMemberResponse } from '~/types/interfaces';
const { getDisplayName } = useProfile() const { getDisplayName } = useProfile()
const contextMenu = useState<ContextMenuInterface>("contextMenu");
const props = defineProps<{ const props = defineProps<{
member: GuildMemberResponse member: GuildMemberResponse
}>(); }>();
const menuItems = await createMemberContextMenuItems(props.member, props.member.guild_uuid);
const modalPopupVisible = ref<boolean>(false); const modalPopupVisible = ref<boolean>(false);
function showModalPopup() { function showModalPopup() {
@ -31,6 +35,7 @@ function hideModalPopup() {
</script> </script>
<style> <style>
.member-item { .member-item {
position: relative; position: relative;
} }
@ -41,4 +46,9 @@ function hideModalPopup() {
min-width: 2.3em; min-width: 2.3em;
max-width: 2.3em; max-width: 2.3em;
} }
.member-display-name {
cursor: pointer;
}
</style> </style>

View file

@ -1,5 +1,5 @@
<template> <template>
<div v-if="props.type == 'normal' || props.replyMessage" ref="messageElement" @contextmenu="showContextMenu($event, contextMenu, menuItems)" :id="props.last ? 'last-message' : undefined" <div v-if="props.type == 'normal' || props.replyMessage" ref="messageElement" @contextmenu="showContextMenu($event, contextMenu, messageMenuItems)" :id="props.last ? 'last-message' : undefined"
class="message normal-message" :class="{ 'mentioned': (props.replyMessage || props.isMentioned) && props.message.member.user.uuid != props.me.uuid && props.replyMessage?.member.user.uuid == props.me.uuid }" :data-message-id="props.messageId" class="message normal-message" :class="{ 'mentioned': (props.replyMessage || props.isMentioned) && props.message.member.user.uuid != props.me.uuid && props.replyMessage?.member.user.uuid == props.me.uuid }" :data-message-id="props.messageId"
:editing.sync="props.editing" :replying-to.sync="props.replyingTo"> :editing.sync="props.editing" :replying-to.sync="props.replyingTo">
<div v-if="props.replyMessage" class="message-reply-svg"> <div v-if="props.replyMessage" class="message-reply-svg">
@ -29,11 +29,11 @@
:text="props.replyMessage?.message" :text="props.replyMessage?.message"
:reply-id="props.replyMessage.uuid" max-width="reply" /> :reply-id="props.replyMessage.uuid" max-width="reply" />
<div class="left-column"> <div class="left-column">
<Avatar :profile="props.author" class="message-author-avatar"/> <Avatar :profile="props.author" class="message-author-avatar" @contextmenu="showContextMenu($event, contextMenu, memberMenuItems)" />
</div> </div>
<div class="message-data"> <div class="message-data">
<div class="message-metadata"> <div class="message-metadata">
<span class="message-author-username" tabindex="0" :style="`color: ${props.authorColor}`"> <span class="message-author-username" tabindex="0" :style="`color: ${props.authorColor}`" @contextmenu="showContextMenu($event, contextMenu, memberMenuItems)">
{{ getDisplayName(props.author) }} {{ getDisplayName(props.author) }}
</span> </span>
<span class="message-date" :title="date.toString()"> <span class="message-date" :title="date.toString()">
@ -47,7 +47,7 @@
</div> </div>
<MessageMedia v-if="mediaLinks.length" :links="mediaLinks" /> <MessageMedia v-if="mediaLinks.length" :links="mediaLinks" />
</div> </div>
<div v-else ref="messageElement" @contextmenu="showContextMenu($event, contextMenu, menuItems)" :id="props.last ? 'last-message' : undefined" <div v-else ref="messageElement" @contextmenu="showContextMenu($event, contextMenu, messageMenuItems)" :id="props.last ? 'last-message' : undefined"
class="message grouped-message" :class="{ 'message-margin-bottom': props.marginBottom, 'mentioned': props.replyMessage || props.isMentioned }" class="message grouped-message" :class="{ 'message-margin-bottom': props.marginBottom, 'mentioned': props.replyMessage || props.isMentioned }"
:data-message-id="props.messageId" :editing.sync="props.editing" :replying-to.sync="props.replyingTo"> :data-message-id="props.messageId" :editing.sync="props.editing" :replying-to.sync="props.replyingTo">
<div class="left-column"> <div class="left-column">
@ -72,9 +72,11 @@ import type { ContextMenuInterface, ContextMenuItem } from '~/types/interfaces';
const { getDisplayName } = useProfile() const { getDisplayName } = useProfile()
const route = useRoute();
const props = defineProps<MessageProps>(); const props = defineProps<MessageProps>();
const contextMenu = useState<ContextMenuInterface>("contextMenu", () => ({ show: false, pointerX: 0, pointerY: 0, items: [] })); const contextMenu = useState<ContextMenuInterface>("contextMenu");
const messageElement = ref<HTMLDivElement>(); const messageElement = ref<HTMLDivElement>();
@ -144,21 +146,23 @@ console.log("media links:", mediaLinks);
// showHover.value = !showHover.value; // showHover.value = !showHover.value;
//} //}
const menuItems: ContextMenuItem[] = [ const messageMenuItems: ContextMenuItem[] = [
{ name: "Reply", icon: "lucide:reply", type: "normal", callback: () => { if (messageElement.value) replyToMessage(messageElement.value, props) } } { name: "Reply", icon: "lucide:reply", type: "normal", callback: () => { if (messageElement.value) replyToMessage(messageElement.value, props) } }
] ]
console.log("me:", props.me); console.log("me:", props.me);
if (props.author?.user.uuid == props.me.uuid) { if (props.author.user.uuid == props.me.uuid) {
// Inserts "edit" option at index 1 (below the "reply" option) // Inserts "edit" option at index 1 (below the "reply" option)
menuItems.splice(1, 0, { name: "Edit (WIP)", icon: "lucide:square-pen", type: "normal", callback: () => { /* if (messageElement.value) editMessage(messageElement.value, props) */ } }); messageMenuItems.splice(Math.min(1, messageMenuItems.length), 0, { name: "Edit (WIP)", icon: "lucide:square-pen", type: "normal", callback: () => { /* if (messageElement.value) editMessage(messageElement.value, props) */ } });
} }
if (props.author?.user.uuid == props.me.uuid /* || check message delete permission*/) { if (props.author.user.uuid == props.me.uuid /* || check message delete permission*/) {
// Inserts "edit" option at index 2 (below the "edit" option) // Inserts "edit" option at index 2 (below the "edit" option)
menuItems.splice(2, 0, { name: "Delete (WIP)", icon: "lucide:trash", type: "danger", callback: () => {} }); messageMenuItems.splice(Math.min(2, messageMenuItems.length), 0, { name: "Delete (WIP)", icon: "lucide:trash", type: "danger", callback: () => {} });
} }
const memberMenuItems = await createMemberContextMenuItems(props.author, route.params.serverId as string);
function getDayDifference(date1: Date, date2: Date) { function getDayDifference(date1: Date, date2: Date) {
const midnight1 = new Date(date1.getFullYear(), date1.getMonth(), date1.getDate()); const midnight1 = new Date(date1.getFullYear(), date1.getMonth(), date1.getDate());
const midnight2 = new Date(date2.getFullYear(), date2.getMonth(), date2.getDate()); const midnight2 = new Date(date2.getFullYear(), date2.getMonth(), date2.getDate());
@ -232,6 +236,11 @@ function getDayDifference(date1: Date, date2: Date) {
max-height: 2em; max-height: 2em;
min-width: 2em; min-width: 2em;
max-width: 2em; max-width: 2em;
cursor: pointer;
}
.message-author-username {
cursor: pointer;
} }
.left-column { .left-column {

View file

@ -18,6 +18,10 @@ onMounted(() => {
if (contextMenu) { if (contextMenu) {
contextMenu.style.left = props.pointerX.toString() + "px"; contextMenu.style.left = props.pointerX.toString() + "px";
contextMenu.style.top = props.pointerY.toString() + "px"; contextMenu.style.top = props.pointerY.toString() + "px";
const rect = contextMenu.getBoundingClientRect();
if (rect.right > (window.innerWidth || document.documentElement.clientWidth)) {
contextMenu.style.left = (props.pointerX - contextMenu.clientWidth).toString() + "px";
}
} }
}); });

View file

@ -1,5 +1,5 @@
<template> <template>
<div ref="resizableSidebar" class="resizable-sidebar" <div ref="resizableSidebar" class="resizable-sidebar" @contextmenu="showContextMenu($event, contextMenu, menuItems)"
:style="{ :style="{
'width': storedWidth ? storedWidth : props.width, 'width': storedWidth ? storedWidth : props.width,
'min-width': props.minWidth, 'min-width': props.minWidth,
@ -49,10 +49,6 @@ onMounted(() => {
if (resizableSidebar.value && widthResizer.value) { if (resizableSidebar.value && widthResizer.value) {
widthResizer.value.addEventListener("pointerdown", (e) => { widthResizer.value.addEventListener("pointerdown", (e) => {
e.preventDefault(); e.preventDefault();
if (e.button == 2) {
showContextMenu(e, contextMenu.value, menuItems);
return
};
document.body.style.cursor = "ew-resize"; document.body.style.cursor = "ew-resize";
function handleMove(pointer: PointerEvent) { function handleMove(pointer: PointerEvent) {
if (resizableSidebar.value) { if (resizableSidebar.value) {

View file

@ -25,6 +25,17 @@ export const useApi = () => {
return await fetchWithApi("/me") return await fetchWithApi("/me")
} }
async function fetchMeMember(guildId: string): Promise<GuildMemberResponse | undefined> {
const { getUser } = useAuth();
const me = await getUser();
if (me) {
const members = await fetchMembers(guildId);
const meMember = members.objects.find(member => member.user.uuid == me.uuid);
return meMember;
}
}
async function fetchChannels(guildId: string): Promise<ChannelResponse[]> { async function fetchChannels(guildId: string): Promise<ChannelResponse[]> {
return ensureIsArray(await fetchWithApi(`/guilds/${guildId}/channels`)); return ensureIsArray(await fetchWithApi(`/guilds/${guildId}/channels`));
} }
@ -48,6 +59,14 @@ export const useApi = () => {
return await fetchWithApi(`/guilds/${guildId}/members/${memberId}`); return await fetchWithApi(`/guilds/${guildId}/members/${memberId}`);
} }
async function kickMember(memberId: string) {
return await fetchWithApi(`/members/${memberId}`, { method: "DELETE" });
}
async function banMember(guildId: string, memberId: string) {
//
}
async function fetchUsers() { async function fetchUsers() {
return await fetchWithApi(`/users`); return await fetchWithApi(`/users`);
} }
@ -114,10 +133,13 @@ export const useApi = () => {
fetchGuild, fetchGuild,
fetchMyGuilds, fetchMyGuilds,
fetchMe, fetchMe,
fetchMeMember,
fetchChannels, fetchChannels,
fetchChannel, fetchChannel,
fetchMembers, fetchMembers,
fetchMember, fetchMember,
kickMember,
banMember,
fetchUsers, fetchUsers,
fetchUser, fetchUser,
fetchFriends, fetchFriends,

View file

@ -56,7 +56,7 @@ const showGuildSettings = ref(false);
//const servers = await fetchWithApi("/servers") as { uuid: string, name: string, description: string }[]; //const servers = await fetchWithApi("/servers") as { uuid: string, name: string, description: string }[];
//console.log("channelid: servers:", servers); //console.log("channelid: servers:", servers);
const { fetchMembers } = useApi(); const { fetchMembers, fetchMeMember } = useApi();
onMounted(async () => { onMounted(async () => {
console.log("mounting"); console.log("mounting");
@ -84,6 +84,14 @@ async function setArrayVariables() {
console.log("channels:", channels.value); console.log("channels:", channels.value);
channel.value = await fetchWithApi(`/channels/${route.params.channelId}`); channel.value = await fetchWithApi(`/channels/${route.params.channelId}`);
console.log("channel:", channel.value); console.log("channel:", channel.value);
const meMember = useState<GuildMemberResponse | undefined>("meMember");
console.log("[CHANNEL] meMember:", meMember.value);
if (!meMember.value) {
console.log("[CHANNEL] meMember is uninitialized, initializing");
const fetchedMeMember = await fetchMeMember(route.params.serverId as string);
meMember.value = fetchedMeMember;
console.log("[CHANNEL] meMember set to:", fetchedMeMember);
}
} }
function toggleGuildSettings(e: Event) { function toggleGuildSettings(e: Event) {

View file

@ -0,0 +1,33 @@
import { Permission } from "~/types/enums";
import type { ContextMenuItem, GuildMemberResponse } from "~/types/interfaces";
export default async (member: GuildMemberResponse, guildId: string) => {
const menuItems: ContextMenuItem[] = [];
const { fetchMeMember } = useApi();
const me = useState<GuildMemberResponse | undefined>("me");
if (!me.value) {
const fetchedMe = await fetchMeMember(member.guild_uuid);
me.value = fetchedMe;
}
const { banMember, kickMember } = useApi();
console.log("[MENUITEM] hi");
console.log("[MENUITEM] member:", member.user.username);
console.log("[MENUITEM] me:", me.value?.user.username);
if (me.value && member.uuid != me.value.uuid) {
console.log("[MENUITEM] member is not me");
if (hasPermission(me.value, Permission.KickMember)) {
console.log("[MENUITEM] has kick member permission");
menuItems.splice(Math.min(3, menuItems.length), 0, { name: "Kick", icon: "lucide:user-x", type: "danger", callback: async () => await kickMember(member.uuid) });
}
if (hasPermission(me.value, Permission.BanMember)) {
console.log("[MENUITEM] has ban permission");
menuItems.splice(Math.min(4, menuItems.length), 0, { name: "Ban (WIP)", icon: "lucide:ban", type: "danger", callback: async () => await banMember(guildId, member.uuid) });
}
}
console.log("[MENUITEM] returning menu items:", menuItems);
return menuItems;
}

View file

@ -3,10 +3,15 @@ import ContextMenu from "~/components/UserInterface/ContextMenu.vue";
import type { ContextMenuInterface, ContextMenuItem } from "~/types/interfaces"; import type { ContextMenuInterface, ContextMenuItem } from "~/types/interfaces";
export default (e: MouseEvent | PointerEvent, contextMenu: ContextMenuInterface, menuItems: ContextMenuItem[]) => { export default (e: MouseEvent | PointerEvent, contextMenu: ContextMenuInterface, menuItems: ContextMenuItem[]) => {
e.preventDefault();
e.stopPropagation();
console.log("Menu items:", menuItems);
if (menuItems.length) {
console.log("Showing context menu"); console.log("Showing context menu");
contextMenu.show = true; contextMenu.show = true;
contextMenu.pointerX = e.clientX; contextMenu.pointerX = e.clientX;
contextMenu.pointerY = e.clientY; contextMenu.pointerY = e.clientY;
contextMenu.items = menuItems; contextMenu.items = menuItems;
console.log("Showed"); console.log("Showed");
}
} }