2023-03-04 17:39:06 -05:00
|
|
|
import React, { useEffect } from 'react';
|
2023-02-13 15:58:35 -05:00
|
|
|
import { useSelector, useDispatch } from 'react-redux';
|
2023-03-04 17:39:06 -05:00
|
|
|
import { setModel, setDisabled } from '~/store/submitSlice';
|
|
|
|
|
import { setConversation } from '~/store/convoSlice';
|
|
|
|
|
import ModelDialog from './ModelDialog';
|
|
|
|
|
import MenuItems from './MenuItems';
|
|
|
|
|
// import useDidMountEffect from '~/hooks/useDidMountEffect';
|
|
|
|
|
// import { swr } from '~/utils/fetchers';
|
|
|
|
|
import manualSWR from '~/utils/fetchers';
|
|
|
|
|
// import { setMessages } from '~/store/messageSlice';
|
|
|
|
|
import { setModels } from '~/store/modelSlice';
|
|
|
|
|
// import ModelItem from './ModelItem';
|
2023-02-13 15:58:35 -05:00
|
|
|
import GPTIcon from '../svg/GPTIcon';
|
2023-02-15 12:29:56 -05:00
|
|
|
import BingIcon from '../svg/BingIcon';
|
2023-02-13 15:58:35 -05:00
|
|
|
import { Button } from '../ui/Button.tsx';
|
2023-03-03 15:52:06 -05:00
|
|
|
|
2023-02-13 15:58:35 -05:00
|
|
|
import {
|
|
|
|
|
DropdownMenu,
|
|
|
|
|
DropdownMenuContent,
|
|
|
|
|
DropdownMenuLabel,
|
|
|
|
|
DropdownMenuRadioGroup,
|
|
|
|
|
DropdownMenuSeparator,
|
|
|
|
|
DropdownMenuTrigger
|
|
|
|
|
} from '../ui/DropdownMenu.tsx';
|
|
|
|
|
|
2023-03-04 17:39:06 -05:00
|
|
|
import { Dialog } from '../ui/Dialog.tsx';
|
2023-03-03 15:52:06 -05:00
|
|
|
|
2023-02-13 15:58:35 -05:00
|
|
|
export default function ModelMenu() {
|
2023-02-13 21:15:28 -05:00
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
const { model } = useSelector((state) => state.submit);
|
2023-03-03 21:33:09 -05:00
|
|
|
const { models } = useSelector((state) => state.models);
|
2023-03-04 17:39:06 -05:00
|
|
|
const { trigger } = manualSWR('http://localhost:3050/customGpts', 'get', (res) => {
|
|
|
|
|
console.log('models data (response)', res);
|
|
|
|
|
if (models.length + res.length === models.length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const fetchedModels = res.map((modelItem) => ({
|
|
|
|
|
...modelItem,
|
|
|
|
|
name: modelItem.chatGptLabel
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
dispatch(setModels(fetchedModels));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// useDidMountEffect(() => mutate(), [chatGptLabel]);
|
2023-02-13 15:58:35 -05:00
|
|
|
|
2023-02-22 21:30:48 -05:00
|
|
|
useEffect(() => {
|
|
|
|
|
const lastSelectedModel = JSON.parse(localStorage.getItem('model'));
|
2023-03-03 15:52:06 -05:00
|
|
|
if (lastSelectedModel && lastSelectedModel !== 'chatgptCustom') {
|
2023-02-22 21:30:48 -05:00
|
|
|
dispatch(setModel(lastSelectedModel));
|
|
|
|
|
}
|
2023-03-04 17:39:06 -05:00
|
|
|
|
|
|
|
|
const cachedModels = JSON.parse(localStorage.getItem('models'));
|
|
|
|
|
if (cachedModels) {
|
|
|
|
|
dispatch(setModels(cachedModels));
|
|
|
|
|
}
|
2023-03-03 15:52:06 -05:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2023-02-22 21:30:48 -05:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
localStorage.setItem('model', JSON.stringify(model));
|
|
|
|
|
}, [model]);
|
|
|
|
|
|
2023-03-04 17:39:06 -05:00
|
|
|
useEffect(() => {
|
|
|
|
|
localStorage.setItem('models', JSON.stringify(models.slice(4)));
|
|
|
|
|
}, [models]);
|
|
|
|
|
|
2023-03-03 15:52:06 -05:00
|
|
|
const onChange = (value) => {
|
2023-03-04 17:39:06 -05:00
|
|
|
if (!value) {
|
|
|
|
|
return;
|
|
|
|
|
} else if (value === 'chatgptCustom') {
|
|
|
|
|
// dispatch(setMessages([]));
|
2023-03-03 15:52:06 -05:00
|
|
|
} else {
|
|
|
|
|
dispatch(setModel(value));
|
|
|
|
|
dispatch(setDisabled(false));
|
|
|
|
|
}
|
2023-03-04 17:39:06 -05:00
|
|
|
// Set new conversation
|
|
|
|
|
dispatch(
|
|
|
|
|
setConversation({
|
|
|
|
|
title: 'New Chat',
|
|
|
|
|
error: false,
|
|
|
|
|
conversationId: null,
|
|
|
|
|
parentMessageId: null
|
|
|
|
|
})
|
|
|
|
|
);
|
2023-03-03 15:52:06 -05:00
|
|
|
};
|
|
|
|
|
|
2023-02-13 15:58:35 -05:00
|
|
|
const defaultColorProps = [
|
|
|
|
|
'text-gray-500',
|
|
|
|
|
'hover:bg-gray-100',
|
|
|
|
|
'disabled:hover:bg-transparent',
|
2023-03-03 21:33:09 -05:00
|
|
|
'dark:hover:bg-opacity-20',
|
2023-02-13 15:58:35 -05:00
|
|
|
'dark:hover:bg-gray-900',
|
|
|
|
|
'dark:hover:text-gray-400',
|
|
|
|
|
'dark:disabled:hover:bg-transparent'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const chatgptColorProps = [
|
|
|
|
|
'text-green-700',
|
2023-02-13 20:13:59 -05:00
|
|
|
'dark:text-emerald-300',
|
2023-02-13 15:58:35 -05:00
|
|
|
'hover:bg-green-100',
|
|
|
|
|
'disabled:hover:bg-transparent',
|
|
|
|
|
'dark:hover:bg-opacity-50',
|
|
|
|
|
'dark:hover:bg-green-900',
|
|
|
|
|
'dark:hover:text-gray-100',
|
|
|
|
|
'dark:disabled:hover:bg-transparent'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const colorProps = model === 'chatgpt' ? chatgptColorProps : defaultColorProps;
|
2023-03-03 15:52:06 -05:00
|
|
|
const icon = model === 'bingai' ? <BingIcon button={true} /> : <GPTIcon button={true} />;
|
2023-02-13 15:58:35 -05:00
|
|
|
|
|
|
|
|
return (
|
2023-03-03 15:52:06 -05:00
|
|
|
<Dialog>
|
|
|
|
|
<DropdownMenu>
|
|
|
|
|
<DropdownMenuTrigger asChild>
|
|
|
|
|
<Button
|
|
|
|
|
variant="outline"
|
|
|
|
|
// style={{backgroundColor: 'rgb(16, 163, 127)'}}
|
|
|
|
|
className={`absolute bottom-0.5 rounded-md border-0 p-1 pl-2 outline-none ${colorProps.join(
|
|
|
|
|
' '
|
2023-03-03 21:33:09 -05:00
|
|
|
)} focus:ring-0 focus:ring-offset-0 disabled:bottom-0.5 dark:data-[state=open]:bg-gray-800 dark:data-[state=open]:bg-opacity-50 md:bottom-1 md:left-2 md:pl-1 md:disabled:bottom-1`}
|
2023-03-03 15:52:06 -05:00
|
|
|
>
|
|
|
|
|
{icon}
|
|
|
|
|
</Button>
|
|
|
|
|
</DropdownMenuTrigger>
|
2023-03-03 16:33:02 -05:00
|
|
|
<DropdownMenuContent className="w-56 dark:bg-gray-700">
|
2023-03-03 15:52:06 -05:00
|
|
|
<DropdownMenuLabel>Select a Model</DropdownMenuLabel>
|
|
|
|
|
<DropdownMenuSeparator />
|
|
|
|
|
<DropdownMenuRadioGroup
|
|
|
|
|
value={model}
|
|
|
|
|
onValueChange={onChange}
|
2023-03-04 17:39:06 -05:00
|
|
|
className="overflow-y-auto"
|
2023-03-03 15:52:06 -05:00
|
|
|
>
|
2023-03-04 17:39:06 -05:00
|
|
|
<MenuItems models={models} />
|
2023-03-03 15:52:06 -05:00
|
|
|
</DropdownMenuRadioGroup>
|
|
|
|
|
</DropdownMenuContent>
|
|
|
|
|
</DropdownMenu>
|
2023-03-04 17:39:06 -05:00
|
|
|
<ModelDialog mutate={trigger} />
|
2023-03-03 15:52:06 -05:00
|
|
|
</Dialog>
|
2023-02-13 15:58:35 -05:00
|
|
|
);
|
|
|
|
|
}
|