Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ahqsoftwares committed Sep 27, 2024
1 parent 30544aa commit b5dd281
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/hooks/user/helpers/User/ClientUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IGunInstance, ISEAPair } from 'gun';
import { type GunUserInstance } from '../../useMainUser';
import { createAvatar } from '@/lib/utils/Avatar/createAvatar';
import { PeerUser } from '../Base/PeerUser';
import { getPeerCache } from '@/lib/structs/cache/PeerCache';
import { getPeerCache } from '@/lib/structs/Cache/PeerCache';
import { Util } from '@/lib/utils/Utils/Util';

type ClientUserOptions = {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Structs/DMChannel/DMChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { formatDataStores } from '@/lib/Constants';
import { Util } from '@/lib/utils/Utils/Util';
import type { IGunInstance } from 'gun';
import { Message } from '@/lib/structs/Message/Message';
import { getPeerCache } from '../cache/PeerCache';
import { getPeerCache } from '../Cache/PeerCache';

export class DMChannel {
client: ClientUser;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Structs/Message/Message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getPeerCache } from '../cache/PeerCache';
import { getPeerCache } from '../Cache/PeerCache';

export type MessageStructure = {
content: string;
Expand Down
17 changes: 13 additions & 4 deletions src/lib/utils/Chats/chatData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@
import { PeerUser } from '@/hooks/user/helpers/Base/PeerUser';
import { ClientUser } from '@/hooks/user/helpers/User/ClientUser';
import { db, UserContextValues } from '@/hooks/user/useMainUser';
import { getPeerCache } from '@/lib/structs/cache/PeerCache';
import { getPeerCache } from '@/lib/structs/Cache/PeerCache';
import { DMChannel } from '@/lib/structs/DMChannel/DMChannel';
import { Message } from '@/lib/structs/Message/Message';

export interface ResolvedMessage {
author: PeerUser | ClientUser;
msg: Message;
}

export class ChatData {
user: UserContextValues;
public chats: PeerUser[] = [];
public peerCache = getPeerCache();

public messages: Message[] = [];
public messages: ResolvedMessage[] = [];

public currentChannelCancel = () => { };

Expand All @@ -35,7 +40,7 @@ export class ChatData {
// OUT OF DATE
refreshCache() { }

async getChannel(uid: string, update: (msg: Message[]) => void): Promise<DMChannel> {
async getChannel(uid: string, update: (msg: ResolvedMessage[]) => void): Promise<DMChannel> {
this.currentChannelCancel();

this.messages = [];
Expand All @@ -47,7 +52,11 @@ export class ChatData {
peer,
db,
async (msg) => {
this.messages.push(msg);
let author: ClientUser | PeerUser = this.user.userInfo as ClientUser;
if (channel.peer.pub == msg.author) {
author = channel.peer as PeerUser;
}
this.messages.push({ author, msg });
update(this.messages);
},
);
Expand Down
10 changes: 5 additions & 5 deletions src/routes/chat/components/chat/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import {
ContextMenuItem,
ContextMenuTrigger,
} from '@/components/ui/context-menu';
import { Message } from '@/lib/structs/Message/Message';
import { toast } from 'sonner';
import { useMessages } from '../../messagesProvider';
import { ResolvedMessage } from '@/lib/utils/Chats/chatData';

function ChatEntry({ msg }: { msg: Message }) {
function ChatEntry({ msg: { msg, author } }: { msg: ResolvedMessage }) {
const [data, setData] = useState(false);
const skeleton = useRef<HTMLDivElement>(null);

Expand Down Expand Up @@ -47,13 +47,13 @@ function ChatEntry({ msg }: { msg: Message }) {
<ContextMenuTrigger>
<div className="flex space-x-2 transition-colors md:hover:bg-secondary p-3 rounded-xl">
<img
src="/favicon.png"
src={author.info?.avatar}
className="rounded-full w-16 h-16 cursor-pointer mt-1"
/>
<div className="w-full flex flex-col">
<h1 className="flex w-full">
<span className="cursor-pointer hover:underline mr-3 text-red-500">
AHQ Softwares
{author.info?.displayName || author.info?.username}
</span>

<span className="text-xs my-auto text-accent-foreground">
Expand Down Expand Up @@ -133,7 +133,7 @@ export default function ChatBubbles() {
</div>

{msg.messages.map((msg, i) => (
<ChatEntry key={i + msg.content} msg={msg} />
<ChatEntry key={i + msg.msg.content} msg={msg} />
))}
</>
) : (
Expand Down
8 changes: 4 additions & 4 deletions src/routes/chat/messagesProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { DMChannel } from '@/lib/structs/DMChannel/DMChannel';
import { Message } from '@/lib/structs/Message/Message';
import { createContext, useContext, useEffect, useState } from 'react';
import { useChats } from './chatsProvider';
import { ResolvedMessage } from '@/lib/utils/Chats/chatData';

interface MessageContext {
channel: DMChannel | null;
messages: Message[];
messages: ResolvedMessage[];
}

const MessageProvider = createContext<MessageContext | null>(null);
Expand All @@ -17,7 +17,7 @@ export function useMessages() {
export function Messages({ children }: { children: JSX.Element }) {
const { room, data } = useChats();

const [messages, setMessage] = useState<Message[]>([]);
const [messages, setMessage] = useState<ResolvedMessage[]>([]);
const [channel, setChannel] = useState<DMChannel | null>(null);

useEffect(() => {
Expand All @@ -26,7 +26,7 @@ export function Messages({ children }: { children: JSX.Element }) {
(async () => {
console.log(`Room ${room}`);
const channel = await data.getChannel(`@${room}`, (msg) => {
setMessage(msg.sort((a, b) => a.timestamp.getTime() - b.timestamp.getTime()));
setMessage(msg.sort((a, b) => a.msg.timestamp.getTime() - b.msg.timestamp.getTime()));
});

setChannel(channel);
Expand Down

0 comments on commit b5dd281

Please sign in to comment.