-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from ZickZenni/direct-messages
feat: direct messages
- Loading branch information
Showing
24 changed files
with
364 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/renderer/components/directmessage/DirectMessageButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { ChannelType } from '@/discord/structures/channel/BaseChannel'; | ||
import RendererChannel from '@/discord/structures/channel/RendererChannel'; | ||
import useUser from '@/hooks/useUser'; | ||
import useUsers from '@/hooks/useUsers'; | ||
import { useLocation, useNavigate } from 'react-router-dom'; | ||
|
||
type DirectMessageButtonProps = { | ||
channel: RendererChannel; | ||
}; | ||
|
||
export default function DirectMessageButton({ | ||
channel, | ||
}: DirectMessageButtonProps) { | ||
const location = useLocation(); | ||
const navigate = useNavigate(); | ||
|
||
const user = useUser(undefined); | ||
const members = useUsers(channel.recipientIds ?? []); | ||
|
||
if (user === null) return null; | ||
|
||
const onClick = () => { | ||
navigate(`/channel/${channel.id}`); | ||
}; | ||
|
||
const getName = () => { | ||
if (channel.type === ChannelType.GroupDM) { | ||
if (channel.name.length > 0) return channel.name; | ||
|
||
const membs = [user, ...members]; | ||
return membs | ||
.map((v) => (v.globalName ? v.globalName : v.username)) | ||
.join(', '); | ||
} | ||
|
||
if (members.length === 0) return ''; | ||
|
||
const member = members[0]; | ||
|
||
if (member.globalName === null || member.globalName.length === 0) | ||
return member.username; | ||
|
||
return member.globalName; | ||
}; | ||
|
||
const getIcon = () => { | ||
if (members.length === 0) return ''; | ||
|
||
if (channel.type === ChannelType.GroupDM) | ||
return channel.getChannelIcon() ?? members[0].getAvatarUrl(); | ||
|
||
return members[0].getAvatarUrl(); | ||
}; | ||
|
||
const isSelected = location.pathname.includes(`/channel/${channel.id}`); | ||
|
||
return ( | ||
<div | ||
className={`DirectMessage ${isSelected && 'DirectMessage--selected'}`} | ||
role="presentation" | ||
onClick={() => onClick()} | ||
> | ||
<img | ||
className="DirectMessage--icon-img" | ||
src={getIcon()} | ||
alt="Direct Message Icon" | ||
/> | ||
<p className="DirectMessage--name">{getName()}</p> | ||
</div> | ||
); | ||
} |
31 changes: 31 additions & 0 deletions
31
src/renderer/components/directmessage/DirectMessageList.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import usePrivateChannels from '@/hooks/usePrivateChannels'; | ||
import DirectMessageButton from './DirectMessageButton'; | ||
|
||
export default function DirectMessageList() { | ||
const channels = usePrivateChannels(); | ||
|
||
return ( | ||
<div className="DirectMessageList hidden-scrollbar"> | ||
{ | ||
/* Sort private channels by their last message id */ | ||
channels | ||
.sort((a, b) => { | ||
if (a.lastMessageId === null) return -1; | ||
|
||
if (b.lastMessageId === null) return 1; | ||
|
||
return Number(a.lastMessageId) - Number(b.lastMessageId); | ||
}) | ||
.reverse() | ||
.map((channel) => { | ||
return ( | ||
<DirectMessageButton | ||
key={`Channel:home:${channel.id}`} | ||
channel={channel} | ||
/> | ||
); | ||
}) | ||
} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { IChannelData } from '@/discord/structures/channel/BaseChannel'; | ||
import RendererChannel from '@/discord/structures/channel/RendererChannel'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
/** | ||
* Fetches all private channels from the user | ||
*/ | ||
export default function usePrivateChannels() { | ||
const [channels, setChannels] = useState<RendererChannel[]>([]); | ||
|
||
useEffect(() => { | ||
window.electron.ipcRenderer | ||
.invoke('discord:private-channels') | ||
.then((data: IChannelData[]) => { | ||
setChannels(data.map((v) => new RendererChannel(v))); | ||
return true; | ||
}) | ||
.catch((err) => console.error(err)); | ||
}, []); | ||
|
||
return channels; | ||
} |
Oops, something went wrong.