From 838825d74758caaf0221844fb04bf4fc464c8fad Mon Sep 17 00:00:00 2001 From: Joel Date: Wed, 28 Jun 2023 17:53:23 +0800 Subject: [PATCH] feat: optimize conversation operation (#479) --- .../explore/sidebar/app-nav-item/index.tsx | 18 +++++------- web/app/components/share/chat/index.tsx | 29 ++++++++++++++++--- .../components/share/chat/sidebar/index.tsx | 17 +++++++---- .../share/chat/sidebar/list/index.tsx | 28 +++++++++--------- 4 files changed, 59 insertions(+), 33 deletions(-) diff --git a/web/app/components/explore/sidebar/app-nav-item/index.tsx b/web/app/components/explore/sidebar/app-nav-item/index.tsx index 7dd2542015..8d57098722 100644 --- a/web/app/components/explore/sidebar/app-nav-item/index.tsx +++ b/web/app/components/explore/sidebar/app-nav-item/index.tsx @@ -56,16 +56,14 @@ export default function AppNavItem({
{name}
{ - !isSelected && ( -
e.stopPropagation()}> - onDelete(id)} - /> -
- ) +
e.stopPropagation()}> + onDelete(id)} + /> +
} ) diff --git a/web/app/components/share/chat/index.tsx b/web/app/components/share/chat/index.tsx index d131801247..a3b18afb1b 100644 --- a/web/app/components/share/chat/index.tsx +++ b/web/app/components/share/chat/index.tsx @@ -67,6 +67,8 @@ const Main: FC = ({ * conversation info */ const [allConversationList, setAllConversationList] = useState([]) + const [isClearConversationList, { setTrue: clearConversationListTrue, setFalse: clearConversationListFalse }] = useBoolean(false) + const [isClearPinnedConversationList, { setTrue: clearPinnedConversationListTrue, setFalse: clearPinnedConversationListFalse }] = useBoolean(false) const { conversationList, setConversationList, @@ -89,18 +91,32 @@ const Main: FC = ({ const [hasPinnedMore, setHasPinnedMore] = useState(true) const onMoreLoaded = ({ data: conversations, has_more }: any) => { setHasMore(has_more) - setConversationList([...conversationList, ...conversations]) + if (isClearConversationList) { + setConversationList(conversations) + clearConversationListFalse() + } + else { + setConversationList([...conversationList, ...conversations]) + } } const onPinnedMoreLoaded = ({ data: conversations, has_more }: any) => { setHasPinnedMore(has_more) - setPinnedConversationList([...pinnedConversationList, ...conversations]) + if (isClearPinnedConversationList) { + setPinnedConversationList(conversations) + clearPinnedConversationListFalse() + } + else { + setPinnedConversationList([...pinnedConversationList, ...conversations]) + } } const [controlUpdateConversationList, setControlUpdateConversationList] = useState(0) const noticeUpdateList = () => { - setConversationList([]) setHasMore(true) - setPinnedConversationList([]) + clearConversationListTrue() + setHasPinnedMore(true) + clearPinnedConversationListTrue() + setControlUpdateConversationList(Date.now()) } const handlePin = async (id: string) => { @@ -126,6 +142,9 @@ const Main: FC = ({ await delConversation(isInstalledApp, installedAppInfo?.id, toDeleteConversationId) notify({ type: 'success', message: t('common.api.success') }) hideConfirm() + if (currConversationId === toDeleteConversationId) + handleConversationIdChange('-1') + noticeUpdateList() } @@ -496,7 +515,9 @@ const Main: FC = ({ return ( void list: ConversationItem[] + isClearConversationList: boolean pinnedList: ConversationItem[] + isClearPinnedConversationList: boolean isInstalledApp: boolean installedAppId?: string siteInfo: SiteInfo @@ -36,7 +38,9 @@ const Sidebar: FC = ({ currentId, onCurrentIdChange, list, + isClearConversationList, pinnedList, + isClearPinnedConversationList, isInstalledApp, installedAppId, siteInfo, @@ -92,16 +96,17 @@ const Sidebar: FC = ({ {t('share.chat.newChat')} -
+
{/* pinned list */} {hasPinned && ( -
+
{t('share.chat.pinnedTitle')}
0 ? maxListHeight : 'flex-grow')} currentId={currentId} onCurrentIdChange={onCurrentIdChange} list={pinnedList} + isClearConversationList={isClearPinnedConversationList} isInstalledApp={isInstalledApp} installedAppId={installedAppId} onMoreLoaded={onPinnedMoreLoaded} @@ -114,8 +119,8 @@ const Sidebar: FC = ({
)} {/* unpinned list */} -
- {hasPinned && ( +
+ {(hasPinned && list.length > 0) && (
{t('share.chat.unpinnedTitle')}
)} = ({ currentId={currentId} onCurrentIdChange={onCurrentIdChange} list={list} + isClearConversationList={isClearConversationList} isInstalledApp={isInstalledApp} installedAppId={installedAppId} onMoreLoaded={onMoreLoaded} @@ -133,6 +139,7 @@ const Sidebar: FC = ({ onDelete={onDelete} />
+
© {copyRight} {(new Date()).getFullYear()}
diff --git a/web/app/components/share/chat/sidebar/list/index.tsx b/web/app/components/share/chat/sidebar/list/index.tsx index 756f89061e..e7796f3d4c 100644 --- a/web/app/components/share/chat/sidebar/list/index.tsx +++ b/web/app/components/share/chat/sidebar/list/index.tsx @@ -17,6 +17,7 @@ export type IListProps = { currentId: string onCurrentIdChange: (id: string) => void list: ConversationItem[] + isClearConversationList: boolean isInstalledApp: boolean installedAppId?: string onMoreLoaded: (res: { data: ConversationItem[]; has_more: boolean }) => void @@ -32,6 +33,7 @@ const List: FC = ({ currentId, onCurrentIdChange, list, + isClearConversationList, isInstalledApp, installedAppId, onMoreLoaded, @@ -46,7 +48,7 @@ const List: FC = ({ useInfiniteScroll( async () => { if (!isNoMore) { - const lastId = list[list.length - 1]?.id + const lastId = !isClearConversationList ? list[list.length - 1]?.id : undefined const { data: conversations, has_more }: any = await fetchConversations(isInstalledApp, installedAppId, lastId, isPinned) onMoreLoaded({ data: conversations, has_more }) } @@ -63,7 +65,7 @@ const List: FC = ({ return (
- { - !isCurrent && ( -
e.stopPropagation()}> - onPinChanged(item.id)} - isShowDelete - onDelete={() => onDelete(item.id)} - /> -
- ) - } + {item.id !== '-1' && ( +
e.stopPropagation()}> + onPinChanged(item.id)} + isShowDelete + onDelete={() => onDelete(item.id)} + /> +
+ )}
) })}