Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Feat: Scrolling knowledge base list #3695" #3708

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions api/apps/kb_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,15 @@ def detail():
@manager.route('/list', methods=['GET'])
@login_required
def list_kbs():
keywords = request.args.get("keywords", "")
page_number = int(request.args.get("page", 1))
items_per_page = int(request.args.get("page_size", 150))
orderby = request.args.get("orderby", "create_time")
desc = request.args.get("desc", True)
try:
tenants = TenantService.get_joined_tenants_by_user_id(current_user.id)
kbs, total = KnowledgebaseService.get_by_tenant_ids(
[m["tenant_id"] for m in tenants], current_user.id, page_number, items_per_page, orderby, desc, keywords)
return get_json_result(data={"kbs": kbs, "total": total})
kbs = KnowledgebaseService.get_by_tenant_ids(
[m["tenant_id"] for m in tenants], current_user.id, page_number, items_per_page, orderby, desc)
return get_json_result(data=kbs)
except Exception as e:
return server_error_response(e)

Expand Down
27 changes: 8 additions & 19 deletions api/db/services/knowledgebase_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def list_documents_by_ids(cls,kb_ids):
@classmethod
@DB.connection_context()
def get_by_tenant_ids(cls, joined_tenant_ids, user_id,
page_number, items_per_page, orderby, desc, keywords):
page_number, items_per_page, orderby, desc):
fields = [
cls.model.id,
cls.model.avatar,
Expand All @@ -51,31 +51,20 @@ def get_by_tenant_ids(cls, joined_tenant_ids, user_id,
User.avatar.alias('tenant_avatar'),
cls.model.update_time
]
if keywords:
kbs = cls.model.select(*fields).join(User, on=(cls.model.tenant_id == User.id)).where(
((cls.model.tenant_id.in_(joined_tenant_ids) & (cls.model.permission ==
TenantPermission.TEAM.value)) | (
cls.model.tenant_id == user_id))
& (cls.model.status == StatusEnum.VALID.value),
(fn.LOWER(cls.model.name).contains(keywords.lower()))
)
else:
kbs = cls.model.select(*fields).join(User, on=(cls.model.tenant_id == User.id)).where(
((cls.model.tenant_id.in_(joined_tenant_ids) & (cls.model.permission ==
TenantPermission.TEAM.value)) | (
cls.model.tenant_id == user_id))
& (cls.model.status == StatusEnum.VALID.value)
)
kbs = cls.model.select(*fields).join(User, on=(cls.model.tenant_id == User.id)).where(
((cls.model.tenant_id.in_(joined_tenant_ids) & (cls.model.permission ==
TenantPermission.TEAM.value)) | (
cls.model.tenant_id == user_id))
& (cls.model.status == StatusEnum.VALID.value)
)
if desc:
kbs = kbs.order_by(cls.model.getter_by(orderby).desc())
else:
kbs = kbs.order_by(cls.model.getter_by(orderby).asc())

count = kbs.count()

kbs = kbs.paginate(page_number, items_per_page)

return list(kbs.dicts()), count
return list(kbs.dicts())

@classmethod
@DB.connection_context()
Expand Down
2 changes: 1 addition & 1 deletion web/.umirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default defineConfig({
proxy: [
{
context: ['/api', '/v1'],
target: 'http://127.0.0.1:9380/',
target: 'http://127.0.0.1:9456/',
changeOrigin: true,
ws: true,
logger: console,
Expand Down
20 changes: 0 additions & 20 deletions web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
"react-force-graph": "^1.44.4",
"react-hook-form": "^7.53.1",
"react-i18next": "^14.0.0",
"react-infinite-scroll-component": "^6.1.0",
"react-markdown": "^9.0.1",
"react-pdf-highlighter": "^6.1.0",
"react-string-replace": "^1.1.1",
Expand Down
55 changes: 2 additions & 53 deletions web/src/hooks/knowledge-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ import { IKnowledge, ITestingResult } from '@/interfaces/database/knowledge';
import i18n from '@/locales/config';
import kbService from '@/services/knowledge-service';
import {
useInfiniteQuery,
useIsMutating,
useMutation,
useMutationState,
useQuery,
useQueryClient,
} from '@tanstack/react-query';
import { useDebounce } from 'ahooks';
import { message } from 'antd';
import { useSearchParams } from 'umi';
import { useHandleSearchChange } from './logic-hooks';
import { useSetPaginationParams } from './route-hook';

export const useKnowledgeBaseId = (): string => {
Expand Down Expand Up @@ -53,7 +50,7 @@ export const useNextFetchKnowledgeList = (
gcTime: 0, // https://tanstack.com/query/latest/docs/framework/react/guides/caching?from=reactQueryV3
queryFn: async () => {
const { data } = await kbService.getList();
const list = data?.data?.kbs ?? [];
const list = data?.data ?? [];
return shouldFilterListWithoutDocument
? list.filter((x: IKnowledge) => x.chunk_num > 0)
: list;
Expand All @@ -63,52 +60,6 @@ export const useNextFetchKnowledgeList = (
return { list: data, loading };
};

export const useInfiniteFetchKnowledgeList = () => {
const { searchString, handleInputChange } = useHandleSearchChange();
const debouncedSearchString = useDebounce(searchString, { wait: 500 });

const PageSize = 10;
const {
data,
error,
fetchNextPage,
hasNextPage,
isFetching,
isFetchingNextPage,
status,
} = useInfiniteQuery({
queryKey: ['infiniteFetchKnowledgeList', debouncedSearchString],
queryFn: async ({ pageParam }) => {
const { data } = await kbService.getList({
page: pageParam,
page_size: PageSize,
keywords: debouncedSearchString,
});
const list = data?.data ?? [];
return list;
},
initialPageParam: 1,
getNextPageParam: (lastPage, pages, lastPageParam) => {
if (lastPageParam * PageSize <= lastPage.total) {
return lastPageParam + 1;
}
return undefined;
},
});
return {
data,
loading: isFetching,
error,
fetchNextPage,
hasNextPage,
isFetching,
isFetchingNextPage,
status,
handleInputChange,
searchString,
};
};

export const useCreateKnowledge = () => {
const queryClient = useQueryClient();
const {
Expand Down Expand Up @@ -144,9 +95,7 @@ export const useDeleteKnowledge = () => {
const { data } = await kbService.rmKb({ kb_id: id });
if (data.code === 0) {
message.success(i18n.t(`message.deleted`));
queryClient.invalidateQueries({
queryKey: ['infiniteFetchKnowledgeList'],
});
queryClient.invalidateQueries({ queryKey: ['fetchKnowledgeList'] });
}
return data?.data ?? [];
},
Expand Down
1 change: 0 additions & 1 deletion web/src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ export default {
namePlaceholder: 'Please input name!',
doc: 'Docs',
searchKnowledgePlaceholder: 'Search',
noMoreData: 'It is all, nothing more',
},
knowledgeDetails: {
dataset: 'Dataset',
Expand Down
1 change: 0 additions & 1 deletion web/src/locales/zh-traditional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ export default {
namePlaceholder: '請輸入名稱',
doc: '文件',
searchKnowledgePlaceholder: '搜索',
noMoreData: 'It is all, nothing more',
},
knowledgeDetails: {
dataset: '數據集',
Expand Down
1 change: 0 additions & 1 deletion web/src/locales/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ export default {
namePlaceholder: '请输入名称',
doc: '文档',
searchKnowledgePlaceholder: '搜索',
noMoreData: '沒有更多的數據了',
},
knowledgeDetails: {
dataset: '数据集',
Expand Down
1 change: 0 additions & 1 deletion web/src/pages/knowledge/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

.knowledge {
padding: 48px 0;
overflow: auto;
}

.topWrapper {
Expand Down
75 changes: 22 additions & 53 deletions web/src/pages/knowledge/index.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
import { useInfiniteFetchKnowledgeList } from '@/hooks/knowledge-hooks';
import { useNextFetchKnowledgeList } from '@/hooks/knowledge-hooks';
import { useFetchUserInfo } from '@/hooks/user-setting-hooks';
import { PlusOutlined, SearchOutlined } from '@ant-design/icons';
import {
Button,
Divider,
Empty,
Flex,
Input,
Skeleton,
Space,
Spin,
} from 'antd';
import { useTranslation } from 'react-i18next';
import InfiniteScroll from 'react-infinite-scroll-component';
import { useSaveKnowledge } from './hooks';
import { Button, Empty, Flex, Input, Space, Spin } from 'antd';
import KnowledgeCard from './knowledge-card';
import KnowledgeCreatingModal from './knowledge-creating-modal';

import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { useSaveKnowledge, useSearchKnowledge } from './hooks';
import styles from './index.less';

const KnowledgeList = () => {
const { searchString, handleInputChange } = useSearchKnowledge();
const { loading, list: data } = useNextFetchKnowledgeList();
const list = data.filter((x) => x.name.includes(searchString));
const { data: userInfo } = useFetchUserInfo();
const { t } = useTranslation('translation', { keyPrefix: 'knowledgeList' });
const {
Expand All @@ -30,23 +22,9 @@ const KnowledgeList = () => {
onCreateOk,
loading: creatingLoading,
} = useSaveKnowledge();
const {
fetchNextPage,
data,
hasNextPage,
searchString,
handleInputChange,
loading,
} = useInfiniteFetchKnowledgeList();
console.log('🚀 ~ KnowledgeList ~ data:', data);
const nextList = data?.pages?.flatMap((x) => x.kbs) ?? [];

const total = useMemo(() => {
return data?.pages.at(-1).total ?? 0;
}, [data?.pages]);

return (
<Flex className={styles.knowledge} vertical flex={1} id="scrollableDiv">
<Flex className={styles.knowledge} vertical flex={1}>
<div className={styles.topWrapper}>
<div>
<span className={styles.title}>
Expand Down Expand Up @@ -75,30 +53,21 @@ const KnowledgeList = () => {
</Space>
</div>
<Spin spinning={loading}>
<InfiniteScroll
dataLength={nextList?.length ?? 0}
next={fetchNextPage}
hasMore={hasNextPage}
loader={<Skeleton avatar paragraph={{ rows: 1 }} active />}
endMessage={total && <Divider plain>{t('noMoreData')} 🤐</Divider>}
scrollableTarget="scrollableDiv"
<Flex
gap={'large'}
wrap="wrap"
className={styles.knowledgeCardContainer}
>
<Flex
gap={'large'}
wrap="wrap"
className={styles.knowledgeCardContainer}
>
{nextList?.length > 0 ? (
nextList.map((item: any) => {
return (
<KnowledgeCard item={item} key={item.name}></KnowledgeCard>
);
})
) : (
<Empty className={styles.knowledgeEmpty}></Empty>
)}
</Flex>
</InfiniteScroll>
{list.length > 0 ? (
list.map((item: any) => {
return (
<KnowledgeCard item={item} key={item.name}></KnowledgeCard>
);
})
) : (
<Empty className={styles.knowledgeEmpty}></Empty>
)}
</Flex>
</Spin>
<KnowledgeCreatingModal
loading={creatingLoading}
Expand Down