Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TBD-21 partner list + TBD-26 darkmode #3

Merged
merged 16 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
},
"dependencies": {
"@google/generative-ai": "^0.21.0",
"@types/js-cookie": "^3.0.6",
"axios": "^1.7.9",
"js-cookie": "^3.0.5",
"next": "15.1.6",
"react": "^19.0.0",
"react-dom": "^19.0.0",
Expand Down
Binary file added public/images/Memoji-01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/Memoji-02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/Memoji-03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/Memoji-04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/Memoji-05.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/Memoji-06.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/Memoji-07.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/Memoji-08.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/Memoji-09.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/Memoji-10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/Memoji-11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/Memoji-12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/Memoji-13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/Memoji-14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/Memoji-15.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/Memoji-16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/Memoji-17.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/Memoji-18.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/Memoji-19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/Memoji-20.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/Memoji-21.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/Memoji-22.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/Memoji-23.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/Memoji-24.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/Memoji-25.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/Memoji-26.png
1 change: 1 addition & 0 deletions src/app/api/chat/ai/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const POST = async (request: Request): Promise<Response> => {
return chatService.getAiResponse({
message: requestBody?.message,
history: requestBody?.history,
characterName: requestBody?.character_name,
});
});
};
43 changes: 39 additions & 4 deletions src/app/chat/page.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,48 @@
import ChatLayout from '@/components/ChatLayout/ChatLayout';
import PartnerList from '@/components/PartnerList';
import ChatBox from '@/components/ChatBox';
import CurrentPartnerProvider from '@/contexts/CurrentPartnerProvider';
import ChatEnablementProvider from '@/contexts/ChatEnablementProvider';

import { getPartnerList } from '@/mocks/partner';
import { PartnerPreviewType } from '@/components/PartnerPreviewChat';

const formatPartnerPreview = ({
display_name,
avatar_url,
is_ai,
last_chat,
username,
}: {
display_name: string;
avatar_url: string;
is_ai: boolean;
last_chat: string;
username: string;
}): PartnerPreviewType => {
return {
displayName: display_name,
avatarSrc: avatar_url,
isAi: is_ai,
lastChat: last_chat,
username,
};
};

const Page = () => {
const partnerList = getPartnerList().map((entry) =>
formatPartnerPreview(entry)
);

return (
<ChatLayout>
<PartnerList />
<ChatBox />
</ChatLayout>
<CurrentPartnerProvider>
<ChatEnablementProvider>
<ChatLayout>
<PartnerList partners={partnerList} />
<ChatBox />
</ChatLayout>
</ChatEnablementProvider>
</CurrentPartnerProvider>
);
};

Expand Down
16 changes: 10 additions & 6 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import '@/app/globals.css';
import StyledComponentsRegistry from '@/styled-component-registry/registry';
import GlobalStyles from '@/styled-component-registry/global-styles';

// todo: create a dark-mode functionality
import { LIGHT_COLORS } from '@/const/COLORS';
import ThemeProvider from '@/contexts/ThemeProvider';

import { getThemePreference } from '@/utils/theme';
import { Funnel_Display } from 'next/font/google';
import type { Metadata } from 'next';

Expand All @@ -18,17 +18,21 @@ export const metadata: Metadata = {
title: 'mateify',
};

export default function RootLayout({
export default async function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
const [theme, themeColors] = await getThemePreference();

return (
<html lang="en" style={LIGHT_COLORS as React.CSSProperties}>
<html lang="en" style={themeColors as React.CSSProperties}>
<body className={defaultFont.className}>
<StyledComponentsRegistry>
{children}
<GlobalStyles />
<ThemeProvider initialTheme={theme}>
{children}
<GlobalStyles />
</ThemeProvider>
</StyledComponentsRegistry>
</body>
</html>
Expand Down
23 changes: 16 additions & 7 deletions src/components/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
'use client';
import React from 'react';
import styled from 'styled-components';
import Label from '@/components/Label';

import { Container } from '@/components/Avatar/styled-components';

type AvatarProps = {
src: string;
size?: 'large' | 'medium' | 'small';
isAi?: boolean;
};

const AVATAR_SIZES = {
Expand All @@ -14,20 +18,25 @@ const AVATAR_SIZES = {
};

const Img = styled.img`
height: var(--size);
aspect-ratio: 1;
display: block;
position: relative;
`;

const Avatar = ({ src, size = 'medium' }: AvatarProps) => {
const Avatar = ({
src,
size = 'medium',
isAi = false,
}: AvatarProps) => {
return (
<Img
src={src}
<Container
style={
{
'--size': AVATAR_SIZES[size],
} as React.CSSProperties
}
/>
}>
{isAi && <Label>AI</Label>}
<Img src={src} />
</Container>
);
};

Expand Down
10 changes: 10 additions & 0 deletions src/components/Avatar/styled-components.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use client';
import styled from 'styled-components';

export const Container = styled.div`
height: var(--size);
width: var(--size);

isolation: isolate;
position: relative;
`;
tsaqifammar marked this conversation as resolved.
Show resolved Hide resolved
156 changes: 60 additions & 96 deletions src/components/ChatBox/ChatBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,131 +5,95 @@ import styled from 'styled-components';
import Header from '@/components/ChatBox/Header';
import ChatHistory from '@/components/ChatBox/ChatHistory';
import Input from '@/components/ChatBox/Input';
import PATHS from '@/const/PATHS';
import useChatData from '@/hooks/useChatData';

import { post } from '@/network';
import { Chat } from '@/components/ChatBox/ChatHistory';
import { getChat } from '@/mocks/chat';
import { CurrentPartnerContext } from '@/contexts/CurrentPartnerProvider';
import { ChatEnablementContext } from '@/contexts/ChatEnablementProvider';
import { Section as ChatHistorySection } from '@/components/ChatBox/styled-components';

const Container = styled.div`
display: flex;
flex-direction: column;

& > *:nth-child(2) {
& > ${ChatHistorySection} {
flex: 1;
}

position: relative;
isolation: isolate;
`;

const formatHistory = ({
chat_id,
is_my_chat,
content,
timestamp,
}: {
chat_id: string;
content: string;
is_my_chat: boolean;
timestamp: number;
}): Chat => {
return {
chatId: chat_id,
content,
timestamp,
isMyChat: is_my_chat,
};
};
const Backdrop = styled.div`
position: absolute;
inset: 0;

const fetchChatData = (): {
avatarUrl: string;
name: string;
isAi: boolean;
history: Chat[];
} => {
const { avatar_url, display_name, is_ai, history } = getChat();

return {
avatarUrl: avatar_url,
name: display_name,
isAi: is_ai,
history: history.map((entry) => formatHistory(entry)),
};
};
z-index: 1;

const chatToDialog = (chatHistory: Chat[]): string[] => {
return chatHistory.map(({ isMyChat, content }) => {
return JSON.stringify({
them: isMyChat ? content : undefined,
you: !isMyChat ? content : undefined,
});
});
};
backdrop-filter: blur(2px);
`;

const ChatBox = () => {
const { avatarUrl, name, isAi, history } = fetchChatData();
const [chatHistory, setChatHistory] =
React.useState<Chat[]>(history);
const [isPartnerTyping, setIsPartnerTyping] = React.useState(false);

const addNewChat = (
content: string,
isMyChat: boolean = true
): void => {
setChatHistory((previousChatHistory) => {
const newChatHistory = [...previousChatHistory];
newChatHistory.push({
chatId: crypto.randomUUID(),
content,
timestamp: Date.now(),
isMyChat,
});

return newChatHistory;
});
};

const getPartnerResponse = (content: string): void => {
setIsPartnerTyping(true);

const data = {
message: content,
history: chatToDialog(
chatHistory.slice(
Math.max(0, chatHistory.length - 25),
chatHistory.length
)
),
};
post({
path: PATHS.chat_gemini,
data,
callback: (response) => {
addNewChat(response.data?.data, false);
setIsPartnerTyping(false);
},
});
};
const ActiveChatBox = ({ username }: { username: string }) => {
const { data, actions } = useChatData(username);
const { isChatActive } = React.useContext(ChatEnablementContext);

return (
<Container>
<Header avatarUrl={avatarUrl} name={name} isAi={isAi} />
{!isChatActive && <Backdrop />}
<Header
avatarUrl={data.avatarUrl}
name={data.name}
isAi={data.isAi}
/>

<ChatHistory
isPartnerTyping={isPartnerTyping}
chats={chatHistory}
isPartnerTyping={data.isPartnerTyping}
chats={data.history}
/>

<Input
onSubmit={(chat: string) => {
addNewChat(chat);
actions.addNewChat(chat);

// todo: in the future, below AI (incoming chat) listener should be placed in a backend server
if (!isAi) return;
if (!data.isAi) return;

getPartnerResponse(chat);
actions.getPartnerResponse(chat);
}}
/>
</Container>
);
};

const DesktopInactiveChatBox = styled.div`
overflow: hidden;

display: grid;
place-content: center;
width: 100%;

position: relative;
isolation: isolate;
`;

const InactiveChatBox = () => {
const { isChatActive } = React.useContext(ChatEnablementContext);

return (
<DesktopInactiveChatBox>
{!isChatActive && <Backdrop />}
start a chat with someone!
</DesktopInactiveChatBox>
);
};

const ChatBox = () => {
const { username } = React.useContext(CurrentPartnerContext);

if (!username) {
return <InactiveChatBox />;
}

return <ActiveChatBox key={username} username={username} />;
};

export default ChatBox;
Loading