Skip to content

Commit

Permalink
✅ test: Add test and fix some code review problems
Browse files Browse the repository at this point in the history
  • Loading branch information
canisminor1990 authored and arvinxx committed Jan 21, 2024
1 parent 330a1d8 commit 45f3536
Show file tree
Hide file tree
Showing 24 changed files with 393 additions and 244 deletions.
16 changes: 11 additions & 5 deletions src/app/chat/(desktop)/features/ChatInput/Footer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { createStyles } from 'antd-style';
import {
ChevronUp,
CornerDownLeft,
Loader2,
LucideCheck,
LucideChevronDown,
LucideCommand,
LucidePlus,
StopCircle,
} from 'lucide-react';
import { rgba } from 'polished';
import { memo } from 'react';
Expand Down Expand Up @@ -42,7 +42,7 @@ const useStyles = createStyles(({ css, prefixCls, token }) => {
.${prefixCls}-btn.${prefixCls}-dropdown-trigger {
&::before {
background-color: ${rgba(token.colorBgLayout, 0.05)} !important;
background-color: ${rgba(token.colorBgLayout, 0.1)} !important;
}
}
`,
Expand All @@ -51,7 +51,7 @@ const useStyles = createStyles(({ css, prefixCls, token }) => {

const isMac = isMacOS();

const Footer = memo(() => {
const Footer = memo<{ setExpand?: (expand: boolean) => void }>(({ setExpand }) => {
const { t } = useTranslation('chat');

const { theme, styles } = useStyles();
Expand Down Expand Up @@ -111,12 +111,18 @@ const Footer = memo(() => {
<SaveTopic />
<Flexbox style={{ minWidth: 92 }}>
{loading ? (
<Button icon={loading && <Icon icon={Loader2} spin />} onClick={stopGenerateMessage}>
<Button icon={loading && <Icon icon={StopCircle} />} onClick={stopGenerateMessage}>
{t('input.stop')}
</Button>
) : (
<Space.Compact>
<Button onClick={() => sendMessage()} type={'primary'}>
<Button
onClick={() => {
sendMessage();
setExpand?.(false);
}}
type={'primary'}
>
{t('input.send')}
</Button>
<Dropdown
Expand Down
2 changes: 1 addition & 1 deletion src/app/chat/(desktop)/features/ChatInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const ChatInput = memo(() => {
>
<Head expand={expand} setExpand={setExpand} />
<TextArea setExpand={setExpand} />
<Footer />
<Footer setExpand={setExpand} />
</Flexbox>
</DraggablePanel>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { memo, useMemo } from 'react';
import { useTranslation } from 'react-i18next';

import { useGlobalStore } from '@/store/global';
import { preferenceSelectors } from '@/store/global/selectors';
import { groupHelpers } from '@/store/global/slices/common/helpers';
import { groupHelpers } from '@/store/global/helpers';
import { settingsSelectors } from '@/store/global/selectors';

const useStyles = createStyles(({ css }) => ({
modalRoot: css`
Expand All @@ -25,7 +25,7 @@ const Actions = memo<ActionsProps>(({ id, openRenameModal, openConfigModal, onOp
const { t } = useTranslation('common');
const { styles } = useStyles();
const { modal } = App.useApp();
const sessionCustomGroups = useGlobalStore(preferenceSelectors.sessionCustomGroups, isEqual);
const sessionCustomGroups = useGlobalStore(settingsSelectors.sessionCustomGroups, isEqual);
const updateCustomGroup = useGlobalStore((s) => s.updateCustomGroup);
const items: MenuProps['items'] = useMemo(
() => [
Expand Down
13 changes: 5 additions & 8 deletions src/app/chat/features/SessionListContent/DefaultMode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { memo, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';

import { useGlobalStore } from '@/store/global';
import { preferenceSelectors } from '@/store/global/selectors';
import { preferenceSelectors, settingsSelectors } from '@/store/global/selectors';
import { useSessionStore } from '@/store/session';
import { sessionSelectors } from '@/store/session/selectors';

Expand All @@ -20,19 +20,16 @@ const SessionListContent = memo(() => {
const [renameGroupModalOpen, setRenameGroupModalOpen] = useState(false);
const [configGroupModalOpen, setConfigGroupModalOpen] = useState(false);
const { t } = useTranslation('chat');

const [ useFetchSessions] = useSessionStore((s) => [
s.useFetchSessions,
]);

const sessionCustomGroups = useGlobalStore(preferenceSelectors.sessionCustomGroups, isEqual);
const sessionCustomGroups = useGlobalStore(settingsSelectors.sessionCustomGroups, isEqual);
const sessionList =
useSessionStore(sessionSelectors.sessionList(sessionCustomGroups), isEqual) || {};
const [sessionGroupKeys, updatePreference] = useGlobalStore((s) => [
preferenceSelectors.sessionGroupKeys(s),
s.updatePreference,
]);

const [ useFetchSessions] = useSessionStore((s) => [
s.useFetchSessions,
]);
useFetchSessions();


Expand Down
20 changes: 10 additions & 10 deletions src/app/chat/features/SessionListContent/List/Item/Actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { useTranslation } from 'react-i18next';

import { configService } from '@/services/config';
import { useGlobalStore } from '@/store/global';
import { preferenceSelectors } from '@/store/global/selectors';
import { settingsSelectors } from '@/store/global/selectors';
import { useSessionStore } from '@/store/session';
import { sessionHelpers } from '@/store/session/helpers';
import { sessionSelectors } from '@/store/session/selectors';
Expand All @@ -42,7 +42,7 @@ const Actions = memo<ActionProps>(({ group, id, openCreateGroupModal, setOpen })

const { styles } = useStyles();

const sessionCustomGroups = useGlobalStore(preferenceSelectors.sessionCustomGroups, isEqual);
const sessionCustomGroups = useGlobalStore(settingsSelectors.sessionCustomGroups, isEqual);
const [pin, removeSession, pinSession, duplicateSession, updateSessionGroup] = useSessionStore(
(s) => {
const session = sessionSelectors.getSessionById(id)(s);
Expand Down Expand Up @@ -86,14 +86,6 @@ const Actions = memo<ActionProps>(({ group, id, openCreateGroupModal, setOpen })
},
{
children: [
{
icon: isDefault ? <Icon icon={Check} /> : <div />,
key: 'defaultList',
label: t('defaultList'),
onClick: () => {
updateSessionGroup(id, SessionDefaultGroup.Default);
},
},
...sessionCustomGroups.map(({ id: groupId, name }) => ({
icon: group === groupId ? <Icon icon={Check} /> : <div />,
key: groupId,
Expand All @@ -102,6 +94,14 @@ const Actions = memo<ActionProps>(({ group, id, openCreateGroupModal, setOpen })
updateSessionGroup(id, groupId);
},
})),
{
icon: isDefault ? <Icon icon={Check} /> : <div />,
key: 'defaultList',
label: t('defaultList'),
onClick: () => {
updateSessionGroup(id, SessionDefaultGroup.Default);
},
},
{
type: 'divider',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { memo, useCallback, useState } from 'react';
import { useTranslation } from 'react-i18next';

import { useGlobalStore } from '@/store/global';
import { preferenceSelectors } from '@/store/global/selectors';
import { groupHelpers } from '@/store/global/slices/common/helpers';
import { groupHelpers } from '@/store/global/helpers';
import { settingsSelectors } from '@/store/global/selectors';
import { SessionGroupItem } from '@/types/session';

const useStyles = createStyles(({ css }) => ({
Expand All @@ -27,7 +27,7 @@ const useStyles = createStyles(({ css }) => ({

const GroupItem = memo<SessionGroupItem>(({ id, name }) => {
const [editing, setEditing] = useState(false);
const sessionCustomGroups = useGlobalStore(preferenceSelectors.sessionCustomGroups, isEqual);
const sessionCustomGroups = useGlobalStore(settingsSelectors.sessionCustomGroups, isEqual);
const updateCustomGroup = useGlobalStore((s) => s.updateCustomGroup);
const { t } = useTranslation('common');
const { styles } = useStyles();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useTranslation } from 'react-i18next';
import { Flexbox } from 'react-layout-kit';

import { useGlobalStore } from '@/store/global';
import { preferenceSelectors } from '@/store/global/selectors';
import { settingsSelectors } from '@/store/global/selectors';
import { SessionGroupItem } from '@/types/session';

import GroupItem from './GroupItem';
Expand All @@ -30,7 +30,7 @@ const useStyles = createStyles(({ css, token, stylish }) => ({
const ConfigGroupModal = memo<ModalProps>(({ open, onCancel }) => {
const { t } = useTranslation('common');
const { styles } = useStyles();
const sessionCustomGroups = useGlobalStore(preferenceSelectors.sessionCustomGroups, isEqual);
const sessionCustomGroups = useGlobalStore(settingsSelectors.sessionCustomGroups, isEqual);
const [addCustomGroup, updateCustomGroup] = useGlobalStore((s) => [
s.addCustomGroup,
s.updateCustomGroup,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { memo, useCallback, useState } from 'react';
import { useTranslation } from 'react-i18next';

import { useGlobalStore } from '@/store/global';
import { preferenceSelectors } from '@/store/global/selectors';
import { settingsSelectors } from '@/store/global/selectors';
import { useSessionStore } from '@/store/session';

interface CreateGroupModalProps extends ModalProps {
Expand All @@ -15,7 +15,7 @@ interface CreateGroupModalProps extends ModalProps {
const CreateGroupModal = memo<CreateGroupModalProps>(
({ id, open, onCancel }: CreateGroupModalProps) => {
const { t } = useTranslation('common');
const sessionCustomGroups = useGlobalStore(preferenceSelectors.sessionCustomGroups, isEqual);
const sessionCustomGroups = useGlobalStore(settingsSelectors.sessionCustomGroups, isEqual);
const addCustomGroup = useGlobalStore((s) => s.addCustomGroup);
const updateSessionGroup = useSessionStore((s) => s.updateSessionGroup);
const [input, setInput] = useState('');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import { memo, useCallback, useState } from 'react';
import { useTranslation } from 'react-i18next';

import { useGlobalStore } from '@/store/global';
import { preferenceSelectors } from '@/store/global/selectors';
import { groupHelpers } from '@/store/global/slices/common/helpers';
import { groupHelpers } from '@/store/global/helpers';
import { settingsSelectors } from '@/store/global/selectors';

interface RenameGroupModalProps extends ModalProps {
id: string;
}

const RenameGroupModal = memo<RenameGroupModalProps>(({ id, open, onCancel }) => {
const { t } = useTranslation('common');
const sessionCustomGroups = useGlobalStore(preferenceSelectors.sessionCustomGroups, isEqual);
const sessionCustomGroups = useGlobalStore(settingsSelectors.sessionCustomGroups, isEqual);
const updateCustomGroup = useGlobalStore((s) => s.updateCustomGroup);
const group = groupHelpers.getGroupById(id, sessionCustomGroups);
const [input, setInput] = useState<string>();
Expand Down
1 change: 1 addition & 0 deletions src/const/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const DEFAULT_BASE_SETTINGS: GlobalBaseSettings = {
fontSize: 14,
language: 'auto',
password: '',
sessionCustomGroups: [],
themeMode: 'auto',
};

Expand Down
4 changes: 2 additions & 2 deletions src/database/models/__tests__/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ describe('SessionModel', () => {

it('should update a session', async () => {
const createdSession = await SessionModel.create('agent', sessionData);
const updateData = { group: 'createGroup' };
const updateData = { group: 'newGroup' };

await SessionModel.update(createdSession.id, updateData);
const updatedSession = await SessionModel.findById(createdSession.id);

expect(updatedSession).toHaveProperty('group', 'createGroup');
expect(updatedSession).toHaveProperty('group', 'newGroup');
});

// 删除一个 session 时,也需要同步删除具有 sessionId 的 topic 和 message
Expand Down
2 changes: 1 addition & 1 deletion src/store/global/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ export const globalHelpers = {
getCurrentLanguage,
};

export * from './slices/common/helpers';
export * from './slices/settings/helpers';
13 changes: 4 additions & 9 deletions src/store/global/slices/common/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { SESSION_CHAT_URL } from '@/const/url';
import { CURRENT_VERSION } from '@/const/version';
import { globalService } from '@/services/global';
import type { GlobalStore } from '@/store/global';
import { SessionGroupItem } from '@/types/session';
import type { GlobalServerConfig } from '@/types/settings';
import { merge } from '@/utils/merge';
import { setNamespace } from '@/utils/storeDebug';
Expand All @@ -22,8 +21,8 @@ const n = setNamespace('settings');
* 设置操作
*/
export interface CommonAction {
switchBackToChat: (sessionId?: string) => void;
addCustomGroup: (name: string) => string;
switchBackToChat: (sessionId?: string) => void;
/**
* 切换侧边栏选项
* @param key - 选中的侧边栏选项
Expand All @@ -32,7 +31,6 @@ export interface CommonAction {
toggleChatSideBar: (visible?: boolean) => void;
toggleMobileTopic: (visible?: boolean) => void;
toggleSystemRole: (visible?: boolean) => void;
updateCustomGroup: (groups: SessionGroupItem[]) => void;
updateGuideState: (guide: Partial<Guide>) => void;
updatePreference: (preference: Partial<GlobalPreference>, action?: string) => void;
useCheckLatestVersion: () => SWRResponse<string>;
Expand All @@ -45,9 +43,6 @@ export const createCommonSlice: StateCreator<
[],
CommonAction
> = (set, get) => ({
switchBackToChat: (sessionId) => {
get().router?.push(SESSION_CHAT_URL(sessionId || INBOX_SESSION_ID, get().isMobile));
},
addCustomGroup: (name) => {
const sessionCustomGroups = get().preference.sessionCustomGroups || [];

Expand All @@ -63,6 +58,9 @@ export const createCommonSlice: StateCreator<

return groupId;
},
switchBackToChat: (sessionId) => {
get().router?.push(SESSION_CHAT_URL(sessionId || INBOX_SESSION_ID, get().isMobile));
},
switchSideBar: (key) => {
set({ sidebarKey: key }, false, n('switchSideBar', key));
},
Expand All @@ -84,9 +82,6 @@ export const createCommonSlice: StateCreator<

get().updatePreference({ showSystemRole }, n('toggleMobileTopic', newValue) as string);
},
updateCustomGroup: (groups) => {
get().updatePreference({ sessionCustomGroups: groups });
},
updateGuideState: (guide) => {
const { updatePreference } = get();
const nextGuide = merge(get().preference.guide, guide);
Expand Down
9 changes: 5 additions & 4 deletions src/store/global/slices/common/initialState.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SessionGroupItem } from '@/types/session';

import { AppRouterInstance } from 'next/dist/shared/lib/app-router-context.shared-runtime';

import { SessionDefaultGroup, SessionGroupId, SessionGroupItem } from '@/types/session';

export enum SidebarTabKey {
Chat = 'chat',
Market = 'market',
Expand All @@ -25,8 +25,9 @@ export interface GlobalPreference {
inputHeight: number;
mobileShowTopic?: boolean;
sessionCustomGroups: SessionGroupItem[];
sessionGroupKeys: string[];
sessionGroupKeys: SessionGroupId[];
sessionsWidth: number;

showChatSideBar?: boolean;
showSessionPanel?: boolean;
showSystemRole?: boolean;
Expand Down Expand Up @@ -57,7 +58,7 @@ export const initialCommonState: GlobalCommonState = {
inputHeight: 200,
mobileShowTopic: false,
sessionCustomGroups: [],
sessionGroupKeys: ['pinned', 'sessionList'],
sessionGroupKeys: [SessionDefaultGroup.Pinned, SessionDefaultGroup.Default],
sessionsWidth: 320,
showChatSideBar: true,
showSessionPanel: true,
Expand Down
3 changes: 0 additions & 3 deletions src/store/global/slices/common/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ const sessionGroupKeys = (s: GlobalStore): string[] =>

const useCmdEnterToSend = (s: GlobalStore): boolean => s.preference.useCmdEnterToSend || false;

const sessionCustomGroups = (s: GlobalStore) => s.preference.sessionCustomGroups || [];

export const preferenceSelectors = {
sessionCustomGroups,
sessionGroupKeys,
useCmdEnterToSend,
};
Loading

0 comments on commit 45f3536

Please sign in to comment.