@@ -2,6 +2,10 @@ import { Role, ServerUser, User } from '@/types/dbtypes';
22import { Tooltip } from 'react-tooltip' ;
33import { MiniProfile } from '../forms/MiniProfile' ;
44import UserIcon from '../icons/UserIcon' ;
5+ import * as ContextMenu from '@radix-ui/react-context-menu' ;
6+ import { useSupabaseClient , useUser } from '@supabase/auth-helpers-react' ;
7+ import { acceptFriendRequest , removeFriendOrRequest , sendFriendRequest } from '@/services/friends.service' ;
8+ import { useRelations } from '@/lib/store' ;
59
610export function MessageHeader ( {
711 profile,
@@ -20,38 +24,83 @@ export function MessageHeader({
2024 edited : boolean ;
2125 roles : Role [ ] ;
2226} ) {
27+ const currentUser = useUser ( ) ;
28+ const supabase = useSupabaseClient ( ) ;
29+ const relation = useRelations ( ) . find ( ( relation ) => relation . target_profile . id === profile . id ) ;
30+
2331 return (
24- < div className = "flex-grow flex flex-row" >
25- < Tooltip
26- id = { profile . id }
27- className = "z-20 !w-12 !opacity-100 "
28- style = { { backgroundColor : '#21282b' , borderRadius : '0.5rem' } }
29- clickable
30- noArrow
31- >
32- < MiniProfile
33- profile = { profile }
34- server_user = { server_user }
35- roles = { roles }
36- />
37- </ Tooltip >
38- < UserIcon user = { profile } />
39- < div className = "flex-grow flex items-center" >
40- < div
41- className = "text-xl font-semibold tracking-wider mr-2"
42- data-tooltip-id = { profile . id }
43- style = { {
44- // Check for the first role that has a non-null color and use that
45- color : message_color ,
46- } }
47- >
48- { server_user . nickname || profile . username }
49- </ div >
50- < div className = "text-xs tracking-wider text-grey-300 mt-1" >
51- { display_time } { ' ' }
52- { edited && < span className = "text-gray-400" > (edited)</ span > }
32+ < ContextMenu . Root >
33+ < ContextMenu . Trigger asChild >
34+ < div className = "flex-grow flex flex-row" >
35+ < Tooltip
36+ id = { profile . id }
37+ className = "z-20 !w-12 !opacity-100 "
38+ style = { { backgroundColor : '#21282b' , borderRadius : '0.5rem' } }
39+ clickable
40+ noArrow
41+ >
42+ < MiniProfile
43+ profile = { profile }
44+ server_user = { server_user }
45+ roles = { roles }
46+ />
47+ </ Tooltip >
48+ < UserIcon user = { profile } />
49+ < div className = "flex-grow flex items-center" >
50+ < div
51+ className = "text-xl font-semibold tracking-wider mr-2"
52+ data-tooltip-id = { profile . id }
53+ style = { {
54+ // Check for the first role that has a non-null color and use that
55+ color : message_color ,
56+ } }
57+ >
58+ { server_user . nickname || profile . username }
59+ </ div >
60+ < div className = "text-xs tracking-wider text-grey-300 mt-1" >
61+ { display_time } { ' ' }
62+ { edited && < span className = "text-gray-400" > (edited)</ span > }
63+ </ div >
64+ </ div >
5365 </ div >
54- </ div >
55- </ div >
66+ </ ContextMenu . Trigger >
67+ { currentUser ?. id !== profile . id && (
68+ < ContextMenu . Content className = 'ContextMenuContent' >
69+ < ContextMenu . Item
70+ className = 'ContextMenuItem'
71+ onClick = { ( ) => {
72+ sendFriendRequest ( supabase , profile . id ) ;
73+ } }
74+ hidden = { ! ! relation }
75+ >
76+ Send friend request
77+ </ ContextMenu . Item >
78+ < ContextMenu . Item
79+ className = 'ContextMenuItem'
80+ onClick = { ( ) => {
81+ // NOTE: Since this is only shown if a relation exists, therefore we can assume relation is not undefined
82+ acceptFriendRequest ( supabase , relation ! . id ) ;
83+ } }
84+ hidden = { ! relation || relation . relationship !== 'friend_requested' || relation . initiator_profile_id === currentUser ?. id }
85+ >
86+ Accept friend request
87+ </ ContextMenu . Item >
88+ < ContextMenu . Item
89+ className = 'ContextMenuItem'
90+ onClick = { ( ) => {
91+ // NOTE: Since this is only shown if a relation exists, therefore we can assume relation is not undefined
92+ removeFriendOrRequest ( supabase , relation ! . id ) ;
93+ } }
94+ hidden = { ! relation }
95+ >
96+ {
97+ relation && relation . relationship === 'friend_requested' ?
98+ ( relation . initiator_profile_id === currentUser ?. id ? 'Cancel friend request' : 'Reject friend request' )
99+ : 'Remove friend'
100+ }
101+ </ ContextMenu . Item >
102+ </ ContextMenu . Content >
103+ ) }
104+ </ ContextMenu . Root >
56105 ) ;
57106}
0 commit comments