Skip to content

Commit

Permalink
feat: settings dialog and moved theme settings to dialog from sidebar (
Browse files Browse the repository at this point in the history
…mckaywrigley#570)

* feat: settings dialog and moved theme settings to dialog from sidebar.

* chore(locale): move some labels to settings from sidebar
  • Loading branch information
dotneet authored Apr 18, 2023
1 parent 836c246 commit ba1dacb
Show file tree
Hide file tree
Showing 42 changed files with 259 additions and 69 deletions.
32 changes: 20 additions & 12 deletions components/Chatbar/components/ChatbarSettings.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { IconFileExport, IconMoon, IconSun } from '@tabler/icons-react';
import { useContext } from 'react';
import {
IconFileExport,
IconMoon,
IconSettings,
IconSun,
} from '@tabler/icons-react';
import { useContext, useState } from 'react';

import { useTranslation } from 'next-i18next';

import HomeContext from '@/pages/api/home/home.context';

import { SettingDialog } from '@/components/Settings/SettingDialog';

import { Import } from '../../Settings/Import';
import { Key } from '../../Settings/Key';
import { SidebarButton } from '../../Sidebar/SidebarButton';
Expand All @@ -14,6 +21,7 @@ import { PluginKeys } from './PluginKeys';

export const ChatbarSettings = () => {
const { t } = useTranslation('sidebar');
const [isSettingDialogOpen, setIsSettingDialog] = useState<boolean>(false);

const {
state: {
Expand Down Expand Up @@ -49,23 +57,23 @@ export const ChatbarSettings = () => {
/>

<SidebarButton
text={lightMode === 'light' ? t('Dark mode') : t('Light mode')}
icon={
lightMode === 'light' ? <IconMoon size={18} /> : <IconSun size={18} />
}
onClick={() =>
homeDispatch({
field: 'lightMode',
value: lightMode === 'light' ? 'dark' : 'light',
})
}
text={t('Settings')}
icon={<IconSettings size={18} />}
onClick={() => setIsSettingDialog(true)}
/>

{!serverSideApiKeyIsSet ? (
<Key apiKey={apiKey} onApiKeyChange={handleApiKeyChange} />
) : null}

{!serverSidePluginKeysSet ? <PluginKeys /> : null}

<SettingDialog
open={isSettingDialogOpen}
onClose={() => {
setIsSettingDialog(false);
}}
/>
</div>
);
};
105 changes: 105 additions & 0 deletions components/Settings/SettingDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { FC, useContext, useEffect, useReducer, useRef } from 'react';

import { useTranslation } from 'next-i18next';

import { useCreateReducer } from '@/hooks/useCreateReducer';

import { getSettings, saveSettings } from '@/utils/app/settings';

import { Settings } from '@/types/settings';

import HomeContext from '@/pages/api/home/home.context';

interface Props {
open: boolean;
onClose: () => void;
}

export const SettingDialog: FC<Props> = ({ open, onClose }) => {
const { t } = useTranslation('settings');
const settings: Settings = getSettings();
const { state, dispatch } = useCreateReducer<Settings>({
initialState: settings,
});
const { dispatch: homeDispatch } = useContext(HomeContext);
const modalRef = useRef<HTMLDivElement>(null);

useEffect(() => {
const handleMouseDown = (e: MouseEvent) => {
if (modalRef.current && !modalRef.current.contains(e.target as Node)) {
window.addEventListener('mouseup', handleMouseUp);
}
};

const handleMouseUp = (e: MouseEvent) => {
window.removeEventListener('mouseup', handleMouseUp);
onClose();
};

window.addEventListener('mousedown', handleMouseDown);

return () => {
window.removeEventListener('mousedown', handleMouseDown);
};
}, [onClose]);

const handleSave = () => {
homeDispatch({ field: 'lightMode', value: state.theme });
saveSettings(state);
};

// Render nothing if the dialog is not open.
if (!open) {
return <></>;
}

// Render the dialog.
return (
<div className="fixed inset-0 flex items-center justify-center bg-black bg-opacity-50 z-50">
<div className="fixed inset-0 z-10 overflow-hidden">
<div className="flex items-center justify-center min-h-screen px-4 pt-4 pb-20 text-center sm:block sm:p-0">
<div
className="hidden sm:inline-block sm:h-screen sm:align-middle"
aria-hidden="true"
/>

<div
ref={modalRef}
className="dark:border-netural-400 inline-block max-h-[400px] transform overflow-y-auto rounded-lg border border-gray-300 bg-white px-4 pt-5 pb-4 text-left align-bottom shadow-xl transition-all dark:bg-[#202123] sm:my-8 sm:max-h-[600px] sm:w-full sm:max-w-lg sm:p-6 sm:align-middle"
role="dialog"
>
<div className="text-lg pb-4 font-bold text-black dark:text-neutral-200">
{t('Settings')}
</div>

<div className="text-sm font-bold mb-2 text-black dark:text-neutral-200">
{t('Theme')}
</div>

<select
className="w-full cursor-pointer bg-transparent p-2 text-neutral-700 dark:text-neutral-200"
value={state.theme}
onChange={(event) =>
dispatch({ field: 'theme', value: event.target.value })
}
>
<option value="dark">{t('Dark mode')}</option>
<option value="light">{t('Light mode')}</option>
</select>

<button
type="button"
className="w-full px-4 py-2 mt-6 border rounded-lg shadow border-neutral-500 text-neutral-900 hover:bg-neutral-100 focus:outline-none dark:border-neutral-800 dark:border-opacity-50 dark:bg-white dark:text-black dark:hover:bg-neutral-300"
onClick={() => {
handleSave();
onClose();
}}
>
{t('Save')}
</button>
</div>
</div>
</div>
</div>
);
};
13 changes: 9 additions & 4 deletions pages/api/home/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from '@/utils/app/conversation';
import { saveFolders } from '@/utils/app/folders';
import { savePrompts } from '@/utils/app/prompts';
import { getSettings } from '@/utils/app/settings';

import { Conversation } from '@/types/chat';
import { KeyValuePair } from '@/types/data';
Expand Down Expand Up @@ -68,7 +69,7 @@ const Home = ({
conversations,
selectedConversation,
prompts,
temperature
temperature,
},
dispatch,
} = contextValue;
Expand Down Expand Up @@ -250,9 +251,12 @@ const Home = ({
// ON LOAD --------------------------------------------

useEffect(() => {
const theme = localStorage.getItem('theme');
if (theme) {
dispatch({ field: 'lightMode', value: theme as 'dark' | 'light' });
const settings = getSettings();
if (settings.theme) {
dispatch({
field: 'lightMode',
value: settings.theme,
});
}

const apiKey = localStorage.getItem('apiKey');
Expand Down Expand Up @@ -419,6 +423,7 @@ export const getServerSideProps: GetServerSideProps = async ({ locale }) => {
'sidebar',
'markdown',
'promptbar',
'settings',
])),
},
};
Expand Down
4 changes: 4 additions & 0 deletions public/locales/ar/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"Dark mode": "الوضع الداكن",
"Light mode": "الوضع الفاتح"
}
4 changes: 4 additions & 0 deletions public/locales/bn/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"Dark mode": "ডার্ক মোড",
"Light mode": "লাইট মোড"
}
4 changes: 1 addition & 3 deletions public/locales/bn/sidebar.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@
"Import data": "আলাপচারিতা ইমপোর্ট",
"Are you sure?": "আপনি কি নিশ্চিত?",
"Clear conversations": "কথোপকথন পরিষ্কার করুন",
"Export data": "আলাপচারিতা এক্সপোর্ট",
"Dark mode": "ডার্ক মোড",
"Light mode": "লাইট মোড"
"Export data": "আলাপচারিতা এক্সপোর্ট"
}
4 changes: 4 additions & 0 deletions public/locales/de/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"Dark mode": "Dark Mode",
"Light mode": "Light Mode"
}
4 changes: 1 addition & 3 deletions public/locales/de/sidebar.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@
"Import data": "Konversationen importieren",
"Are you sure?": "Bist du sicher?",
"Clear conversations": "Konversationen löschen",
"Export data": "Konversationen exportieren",
"Dark mode": "Dark Mode",
"Light mode": "Light Mode"
"Export data": "Konversationen exportieren"
}
4 changes: 4 additions & 0 deletions public/locales/es/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"Dark mode": "Modo oscuro",
"Light mode": "Modo claro"
}
4 changes: 1 addition & 3 deletions public/locales/es/sidebar.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@
"Import data": "Importar conversaciones",
"Are you sure?": "¿Estás seguro?",
"Clear conversations": "Borrar conversaciones",
"Export data": "Exportar conversaciones",
"Dark mode": "Modo oscuro",
"Light mode": "Modo claro"
"Export data": "Exportar conversaciones"
}
4 changes: 4 additions & 0 deletions public/locales/fr/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"Dark mode": "Mode sombre",
"Light mode": "Mode clair"
}
4 changes: 1 addition & 3 deletions public/locales/fr/sidebar.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@
"Import data": "Importer des conversations",
"Are you sure?": "Êtes-vous sûr ?",
"Clear conversations": "Effacer les conversations",
"Export data": "Exporter les conversations",
"Dark mode": "Mode sombre",
"Light mode": "Mode clair"
"Export data": "Exporter les conversations"
}
4 changes: 4 additions & 0 deletions public/locales/he/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"Dark mode": "מצב כהה",
"Light mode": "מצב בהיר"
}
4 changes: 1 addition & 3 deletions public/locales/he/sidebar.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@
"Import data": "ייבוא שיחות",
"Are you sure?": "אתה בטוח?",
"Clear conversations": "ניקוי שיחות",
"Export data": "ייצוא שיחות",
"Dark mode": "מצב כהה",
"Light mode": "מצב בהיר"
"Export data": "ייצוא שיחות"
}
4 changes: 4 additions & 0 deletions public/locales/id/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"Dark mode": "Mode gelap",
"Light mode": "Mode terang"
}
4 changes: 1 addition & 3 deletions public/locales/id/sidebar.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@
"Import data": "Impor percakapan",
"Are you sure?": "Apakah Anda yakin?",
"Clear conversations": "Hapus percakapan",
"Export data": "Ekspor percakapan",
"Dark mode": "Mode gelap",
"Light mode": "Mode terang"
"Export data": "Ekspor percakapan"
}
4 changes: 4 additions & 0 deletions public/locales/it/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"Dark mode": "Modalità scura",
"Light mode": "Modalità chiara"
}
4 changes: 1 addition & 3 deletions public/locales/it/sidebar.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@
"Import data": "Importa dati",
"Are you sure?": "Sei sicuro?",
"Clear conversations": "Elimina conversazioni",
"Export data": "Esporta dati",
"Dark mode": "Modalità scura",
"Light mode": "Modalità chiara"
"Export data": "Esporta dati"
}
7 changes: 7 additions & 0 deletions public/locales/ja/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"Settings": "設定",
"Theme": "テーマ",
"Save": "保存",
"Dark mode": "ダークモード",
"Light mode": "ライトモード"
}
3 changes: 2 additions & 1 deletion public/locales/ja/sidebar.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"Clear conversations": " 会話をクリア",
"Export data": "会話履歴をエクスポート",
"Dark mode": "ダークモード",
"Light mode": "ライトモード"
"Light mode": "ライトモード",
"Settings": "設定"
}
4 changes: 4 additions & 0 deletions public/locales/ko/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"Dark mode": "다크 모드",
"Light mode": "라이트 모드"
}
4 changes: 1 addition & 3 deletions public/locales/ko/sidebar.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@
"Import data": "대화 가져오기",
"Are you sure?": "확실합니까?",
"Clear conversations": "대화 지우기",
"Export data": "대화 내보내기",
"Dark mode": "다크 모드",
"Light mode": "라이트 모드"
"Export data": "대화 내보내기"
}
4 changes: 4 additions & 0 deletions public/locales/pl/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"Dark mode": "Tryb ciemny",
"Light mode": "Tryb jasny"
}
4 changes: 1 addition & 3 deletions public/locales/pl/sidebar.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@
"Import data": "Import rozmów",
"Are you sure?": "Czy jesteś pewien?",
"Clear conversations": "Usuń rozmowy",
"Export data": "Eksport rozmów",
"Dark mode": "Tryb ciemny",
"Light mode": "Tryb jasny"
"Export data": "Eksport rozmów"
}
4 changes: 4 additions & 0 deletions public/locales/pt/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"Dark mode": "Modo escuro",
"Light mode": "Modo claro"
}
4 changes: 1 addition & 3 deletions public/locales/pt/sidebar.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@
"Import data": "Importar conversas",
"Are you sure?": "Tem certeza?",
"Clear conversations": "Apagar conversas",
"Export data": "Exportar conversas",
"Dark mode": "Modo escuro",
"Light mode": "Modo claro"
"Export data": "Exportar conversas"
}
4 changes: 4 additions & 0 deletions public/locales/ro/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"Dark mode": "Modul întunecat",
"Light mode": "Modul de golire"
}
6 changes: 2 additions & 4 deletions public/locales/ro/sidebar.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@
"Import data": "Importați conversații",
"Are you sure?": "Esti sigur?",
"Clear conversations": "Ștergeți conversațiile",
"Export data": "Exportați conversații",
"Dark mode": "Modul întunecat",
"Light mode": "Modul de golire"
}
"Export data": "Exportați conversații"
}
4 changes: 4 additions & 0 deletions public/locales/ru/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"Dark mode": "Темный режим",
"Light mode": "Светлый режим"
}
4 changes: 1 addition & 3 deletions public/locales/ru/sidebar.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@
"Import data": "Импортировать чаты",
"Are you sure?": "Вы уверены?",
"Clear conversations": "Удалить чаты",
"Export data": "Экспортировать чаты",
"Dark mode": "Темный режим",
"Light mode": "Светлый режим"
"Export data": "Экспортировать чаты"
}
Loading

0 comments on commit ba1dacb

Please sign in to comment.