1
0
mirror of https://github.com/spacebarchat/client.git synced 2024-11-22 02:12:38 +01:00

channel and use mention context menus

This commit is contained in:
Puyodead1 2023-12-24 12:51:59 -05:00
parent 88dba4cdf0
commit d6e89d5d88
No known key found for this signature in database
GPG Key ID: A4FA4FEC0DD353FC
5 changed files with 78 additions and 5 deletions

View File

@ -16,6 +16,13 @@ function ChannelContextMenu({ channel }: MenuProps) {
navigator.clipboard.writeText(channel.id); navigator.clipboard.writeText(channel.id);
} }
/**
* Copy link to clipboard
*/
function copyLink() {
navigator.clipboard.writeText(`${window.location.origin}/channels/${channel.guildId}/${channel.id}`);
}
/** /**
* Open invite creation modal * Open invite creation modal
*/ */
@ -28,7 +35,12 @@ function ChannelContextMenu({ channel }: MenuProps) {
return ( return (
<ContextMenu> <ContextMenu>
<ContextMenuButton onClick={openInviteCreateModal}>Create Invite</ContextMenuButton> <ContextMenuButton icon="mdiAccountPlus" onClick={openInviteCreateModal}>
Create Invite
</ContextMenuButton>
<ContextMenuButton icon="mdiLink" onClick={copyLink}>
Copy Link
</ContextMenuButton>
<ContextMenuDivider /> <ContextMenuDivider />
{channel.hasPermission("MANAGE_CHANNELS") && ( {channel.hasPermission("MANAGE_CHANNELS") && (
<> <>

View File

@ -0,0 +1,48 @@
// loosely based on https://github.com/revoltchat/frontend/blob/master/components/app/menus/UserContextMenu.tsx
import Channel from "../../stores/objects/Channel";
import { ContextMenu, ContextMenuButton, ContextMenuDivider } from "./ContextMenu";
interface MenuProps {
channel: Channel;
}
function ChannelMentionContextMenu({ channel }: MenuProps) {
/**
* Copy id to clipboard
*/
function copyId() {
navigator.clipboard.writeText(channel.id);
}
/**
* Copy link to clipboard
*/
function copyLink() {
navigator.clipboard.writeText(`${window.location.origin}/channels/${channel.guildId}/${channel.id}`);
}
return (
<ContextMenu>
<ContextMenuButton icon="mdiLink" onClick={copyLink}>
Copy Link
</ContextMenuButton>
<ContextMenuDivider />
<ContextMenuButton
icon="mdiIdentifier"
iconProps={{
style: {
filter: "invert(100%)",
background: "black",
borderRadius: "4px",
},
}}
onClick={copyId}
>
Copy Channel ID
</ContextMenuButton>
</ContextMenu>
);
}
export default ChannelMentionContextMenu;

View File

@ -1,6 +1,7 @@
import React, { memo } from "react"; import React, { memo } from "react";
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
import styled from "styled-components"; import styled from "styled-components";
import { ContextMenuContext } from "../../contexts/ContextMenuContext";
import { useAppStore } from "../../stores/AppStore"; import { useAppStore } from "../../stores/AppStore";
import Channel from "../../stores/objects/Channel"; import Channel from "../../stores/objects/Channel";
import Role from "../../stores/objects/Role"; import Role from "../../stores/objects/Role";
@ -30,6 +31,7 @@ interface MentionProps {
function UserMention({ id }: MentionProps) { function UserMention({ id }: MentionProps) {
const app = useAppStore(); const app = useAppStore();
const [user, setUser] = React.useState<User | null>(null); const [user, setUser] = React.useState<User | null>(null);
const contextMenu = React.useContext(ContextMenuContext);
React.useEffect(() => { React.useEffect(() => {
const getUser = async () => { const getUser = async () => {
@ -41,8 +43,6 @@ function UserMention({ id }: MentionProps) {
getUser(); getUser();
}, [id]); }, [id]);
console.log(user);
if (!user) return <MentionText>@{id}</MentionText>; if (!user) return <MentionText>@{id}</MentionText>;
return ( return (
@ -54,7 +54,9 @@ function UserMention({ id }: MentionProps) {
}} }}
> >
<FloatingTrigger> <FloatingTrigger>
<MentionText withHover>@{user.username}</MentionText> <MentionText withHover onContextMenu={(e) => contextMenu.onContextMenu(e, { type: "user", user })}>
@{user.username}
</MentionText>
</FloatingTrigger> </FloatingTrigger>
</Floating> </Floating>
); );
@ -64,6 +66,7 @@ function ChannelMention({ id }: MentionProps) {
const app = useAppStore(); const app = useAppStore();
const [channel, setChannel] = React.useState<Channel | null>(null); const [channel, setChannel] = React.useState<Channel | null>(null);
const navigate = useNavigate(); const navigate = useNavigate();
const contextMenu = React.useContext(ContextMenuContext);
const onClick = () => { const onClick = () => {
if (!channel) return; if (!channel) return;
@ -79,7 +82,11 @@ function ChannelMention({ id }: MentionProps) {
if (!channel) return <MentionText>#{id}</MentionText>; if (!channel) return <MentionText>#{id}</MentionText>;
return ( return (
<MentionText withHover onClick={onClick}> <MentionText
withHover
onClick={onClick}
onContextMenu={(e) => contextMenu.onContextMenu(e, { type: "channelMention", channel })}
>
#{channel.name} #{channel.name}
</MentionText> </MentionText>
); );

View File

@ -22,6 +22,10 @@ export type ContextMenuProps =
type: "channel"; type: "channel";
channel: Channel; channel: Channel;
} }
| {
type: "channelMention";
channel: Channel;
}
| { | {
type: "guild"; type: "guild";
guild: Guild; guild: Guild;

View File

@ -1,6 +1,7 @@
import { autoUpdate, flip, offset, shift, useDismiss, useFloating, useInteractions, useRole } from "@floating-ui/react"; import { autoUpdate, flip, offset, shift, useDismiss, useFloating, useInteractions, useRole } from "@floating-ui/react";
import { useMemo, useState } from "react"; import { useMemo, useState } from "react";
import ChannelContextMenu from "../components/contextMenus/ChannelContextMenu"; import ChannelContextMenu from "../components/contextMenus/ChannelContextMenu";
import ChannelMentionContextMenu from "../components/contextMenus/ChannelMentionContextMenu";
import GuildContextMenu from "../components/contextMenus/GuildContextMenu"; import GuildContextMenu from "../components/contextMenus/GuildContextMenu";
import MessageContextMenu from "../components/contextMenus/MessageContextMenu"; import MessageContextMenu from "../components/contextMenus/MessageContextMenu";
import UserContextMenu from "../components/contextMenus/UserContextMenu"; import UserContextMenu from "../components/contextMenus/UserContextMenu";
@ -13,6 +14,7 @@ export const ContextMenuComponents: Components = {
user: UserContextMenu, user: UserContextMenu,
message: MessageContextMenu, message: MessageContextMenu,
channel: ChannelContextMenu, channel: ChannelContextMenu,
channelMention: ChannelMentionContextMenu,
guild: GuildContextMenu, guild: GuildContextMenu,
}; };