Skip to content

Commit

Permalink
Merge pull request #98 from jackschedel/2.0.8
Browse files Browse the repository at this point in the history
2.0.8
  • Loading branch information
jackschedel authored Jan 11, 2024
2 parents 5be890c + 9227e21 commit ded4562
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 72 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koala-client",
"private": true,
"version": "2.0.7",
"version": "2.0.8",
"type": "module",
"homepage": "./",
"main": "electron/index.cjs",
Expand Down
52 changes: 22 additions & 30 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react';
import React, { useEffect } from 'react';
import useStore from '@store/store';
import i18n from './i18n';

Expand All @@ -16,44 +16,38 @@ import { Theme } from '@type/theme';
import ApiPopup from '@components/ApiPopup';
import Toast from '@components/Toast';
import isElectron from '@utils/electron';
import GlobalContext from '@hooks/GlobalContext';

function App() {
const initialiseNewChat = useInitialiseNewChat();
const setChats = useStore((state) => state.setChats);
const setTheme = useStore((state) => state.setTheme);
const setApiKey = useStore((state) => state.setApiKey);
const currentChatIndex = useStore((state) => state.currentChatIndex);
const setCurrentChatIndex = useStore((state) => state.setCurrentChatIndex);
const setHideSideMenu = useStore((state) => state.setHideSideMenu);
const hideSideMenu = useStore((state) => state.hideSideMenu);
const bottomMessageRef = useStore((state) => state.bottomMessageRef);

const initialiseNewChat = useInitialiseNewChat();
const addChat = useAddChat();
const goBack = useGoBack();
const goForward = useGoForward();
const copyCodeBlock = useCopyCodeBlock();
const { handleSubmit } = useSubmit();

const [sharedTextareaRef, setSharedTextareaRef] =
useState<React.RefObject<HTMLTextAreaElement> | null>(null);

const setRef = (newRef: React.RefObject<HTMLTextAreaElement> | null) => {
setSharedTextareaRef(newRef);
};

const handleGenerate = () => {
if (useStore.getState().generating) return;
const updatedChats: ChatInterface[] = JSON.parse(
JSON.stringify(useStore.getState().chats)
);
const content = sharedTextareaRef?.current?.value;
const content = bottomMessageRef?.current?.value;
const updatedMessages = updatedChats[currentChatIndex].messages;
if (!content) {
return;
}

updatedMessages.push({ role: 'user', content: content });
if (sharedTextareaRef && sharedTextareaRef.current) {
sharedTextareaRef.current.value = '';
if (bottomMessageRef && bottomMessageRef?.current) {
bottomMessageRef.current.value = '';
}

setChats(updatedChats);
Expand All @@ -74,8 +68,8 @@ function App() {
}

updatedMessages.push({ role: 'user', content: text });
if (sharedTextareaRef && sharedTextareaRef.current) {
sharedTextareaRef.current.value = '';
if (bottomMessageRef && bottomMessageRef.current) {
bottomMessageRef.current.value = '';
}

setChats(updatedChats);
Expand All @@ -98,7 +92,7 @@ function App() {
if (e.ctrlKey && e.key === 'n') {
e.preventDefault();
addChat();
sharedTextareaRef?.current?.focus();
bottomMessageRef?.current?.focus();
}

// ctrl+o - Copy code block
Expand All @@ -110,7 +104,7 @@ function App() {
// ctrl+g - Focus textarea
if (e.ctrlKey && e.key === 'g') {
e.preventDefault();
sharedTextareaRef?.current?.focus();
bottomMessageRef?.current?.focus();
}

// ctrl+s - Save bottom message + generate
Expand Down Expand Up @@ -229,19 +223,17 @@ function App() {
}, []);

return (
<GlobalContext.Provider value={{ ref: sharedTextareaRef, setRef }}>
<div
tabIndex={0}
className='overflow-hidden w-full h-full relative'
onKeyDown={handleKeyDown}
onMouseUp={handleMouseUp}
>
<Menu />
<Chat />
<ApiPopup />
<Toast />
</div>
</GlobalContext.Provider>
<div
tabIndex={0}
className='overflow-hidden w-full h-full relative'
onKeyDown={handleKeyDown}
onMouseUp={handleMouseUp}
>
<Menu />
<Chat />
<ApiPopup />
<Toast />
</div>
);
}

Expand Down
16 changes: 12 additions & 4 deletions src/components/Chat/ChatContent/Message/NewMessageButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,25 @@ const NewMessageButton = React.memo(
const setChats = useStore((state) => state.setChats);
const currentChatIndex = useStore((state) => state.currentChatIndex);
const setCurrentChatIndex = useStore((state) => state.setCurrentChatIndex);
const autoTitle = useStore((state) => state.autoTitle);

const addChat = () => {
const chats = useStore.getState().chats;
if (chats) {
const updatedChats: ChatInterface[] = JSON.parse(JSON.stringify(chats));
let titleIndex = 1;
let title = `New Chat ${titleIndex}`;

while (chats.some((chat) => chat.title === title)) {
titleIndex += 1;
let title: string;
if (!autoTitle) {
title = `New Chat ${titleIndex}`;
} else {
title = `New Chat`;
}

if (!autoTitle) {
while (chats.some((chat) => chat.title === title)) {
titleIndex += 1;
title = `New Chat ${titleIndex}`;
}
}

updatedChats.unshift(generateDefaultChat(title));
Expand Down
20 changes: 8 additions & 12 deletions src/components/Chat/ChatContent/Message/View/EditView.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { memo, useContext, useEffect, useRef, useState } from 'react';
import React, { memo, useEffect, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import useStore from '@store/store';
import isElectron from '@utils/electron';
import useSubmit from '@hooks/useSubmit';

import { ChatInterface } from '@type/chat';
Expand All @@ -11,7 +10,6 @@ import TokenCount from '@components/TokenCount';
import CommandPrompt from '../CommandPrompt';

import WhisperRecord from '../WhisperRecord';
import GlobalContext from '@hooks/GlobalContext';

const EditView = ({
content,
Expand All @@ -35,12 +33,12 @@ const EditView = ({
const [_content, _setContent] = useState<string>(content);
const [isModalOpen, setIsModalOpen] = useState<boolean>(false);
const textareaRef = useRef<HTMLTextAreaElement>(null);
const { setRef } = useContext(GlobalContext);
const setBottomMessageRef = useStore((state) => state.setBottomMessageRef);
const { t } = useTranslation();

useEffect(() => {
if (sticky) {
setRef(textareaRef);
setBottomMessageRef(textareaRef);
}
}, [textareaRef]);

Expand Down Expand Up @@ -286,13 +284,11 @@ const EditViewButtons = memo(
</div>
<div className='flex-1 flex items-center justify-end'>
{sticky && <TokenCount />}
{isElectron() && (
<WhisperRecord
cursorPosition={cursorPosition}
_setContent={_setContent}
messageIndex={messageIndex}
/>
)}
<WhisperRecord
cursorPosition={cursorPosition}
_setContent={_setContent}
messageIndex={messageIndex}
/>
<CommandPrompt
cursorPosition={cursorPosition}
_setContent={_setContent}
Expand Down
7 changes: 3 additions & 4 deletions src/components/Menu/NewChat.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import React, { useContext } from 'react';
import React from 'react';
import { useTranslation } from 'react-i18next';
import useStore from '@store/store';

import PlusIcon from '@icon/PlusIcon';

import useAddChat from '@hooks/useAddChat';
import GlobalContext from '@hooks/GlobalContext';

const NewChat = ({ folder }: { folder?: string }) => {
const { t } = useTranslation();
const addChat = useAddChat();
const { ref } = useContext(GlobalContext);
const bottomMessageRef = useStore((state) => state.bottomMessageRef);
const generating = useStore((state) => state.generating);

return (
Expand All @@ -27,7 +26,7 @@ const NewChat = ({ folder }: { folder?: string }) => {
onClick={() => {
if (!generating) {
addChat(folder);
ref?.current?.focus();
bottomMessageRef?.current?.focus();
}
}}
title={String(t('newChat'))}
Expand Down
7 changes: 3 additions & 4 deletions src/components/MobileBar/MobileBar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext } from 'react';
import React from 'react';
import useGStore from '@store/cloud-auth-store';
import useStore from '@store/store';
import PlusIcon from '@icon/PlusIcon';
Expand All @@ -12,7 +12,6 @@ import ForwardIcon from '@icon/ForwardIcon';

import useGoBack from '@hooks/useGoBack';
import useGoForward from '@hooks/useGoForward';
import GlobalContext from '@hooks/GlobalContext';

const googleClientId = import.meta.env.VITE_GOOGLE_CLIENT_ID || undefined;

Expand All @@ -22,7 +21,7 @@ const MobileBar = () => {
const hideSideMenu = useStore((state) => state.hideSideMenu);
const currentChatIndex = useStore((state) => state.currentChatIndex);
const chats = useStore((state) => state.chats);
const { ref } = useContext(GlobalContext);
const bottomMessageRef = useStore((state) => state.bottomMessageRef);
const cloudSync = useGStore((state) => state.cloudSync);
const syncStatus = useGStore((state) => state.syncStatus);

Expand Down Expand Up @@ -89,7 +88,7 @@ const MobileBar = () => {
onClick={() => {
if (!generating) {
addChat(chats?.[currentChatIndex]?.folder);
ref?.current?.focus();
bottomMessageRef?.current?.focus();
}
}}
>
Expand Down
13 changes: 0 additions & 13 deletions src/hooks/GlobalContext.tsx

This file was deleted.

16 changes: 12 additions & 4 deletions src/hooks/useAddChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@ import { ChatInterface } from '@type/chat';
const useAddChat = () => {
const setChats = useStore((state) => state.setChats);
const setCurrentChatIndex = useStore((state) => state.setCurrentChatIndex);
const autoTitle = useStore((state) => state.autoTitle);

const addChat = (folder?: string) => {
const chats = useStore.getState().chats;
if (chats) {
const updatedChats: ChatInterface[] = JSON.parse(JSON.stringify(chats));
let titleIndex = 1;
let title = `New Chat ${titleIndex}`;

while (chats.some((chat) => chat.title === title)) {
titleIndex += 1;
let title: string;
if (!autoTitle) {
title = `New Chat ${titleIndex}`;
} else {
title = `New Chat`;
}

if (!autoTitle) {
while (chats.some((chat) => chat.title === title)) {
titleIndex += 1;
title = `New Chat ${titleIndex}`;
}
}

updatedChats.unshift(generateDefaultChat(title, folder));
Expand Down
14 changes: 14 additions & 0 deletions src/store/chat-slice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { StoreSlice } from './store';
import { ChatInterface, FolderCollection, MessageInterface } from '@type/chat';
import { RefObject } from 'react';

export interface ChatSlice {
messages: MessageInterface[];
Expand All @@ -8,6 +9,10 @@ export interface ChatSlice {
generating: boolean;
error: string;
folders: FolderCollection;
bottomMessageRef: RefObject<HTMLTextAreaElement> | null;
setBottomMessageRef: (
bottomMessageRef: RefObject<HTMLTextAreaElement> | null
) => void;
setMessages: (messages: MessageInterface[]) => void;
setChats: (chats: ChatInterface[]) => void;
setCurrentChatIndex: (currentChatIndex: number) => void;
Expand All @@ -23,6 +28,15 @@ export const createChatSlice: StoreSlice<ChatSlice> = (set) => ({
generating: false,
error: '',
folders: {},
bottomMessageRef: null,
setBottomMessageRef: (
bottomMessageRef: RefObject<HTMLTextAreaElement> | null
) => {
set((prev: ChatSlice) => ({
...prev,
bottomMessageRef: bottomMessageRef,
}));
},
setMessages: (messages: MessageInterface[]) => {
set((prev: ChatSlice) => ({
...prev,
Expand Down
3 changes: 3 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ export default defineConfig({
},
},
base: './',
optimizeDeps: {
include: ['@chengsokdara/use-whisper > lamejs'],
},
});

0 comments on commit ded4562

Please sign in to comment.