Skip to content

Commit f652feb

Browse files
committed
temp
1 parent 13e031d commit f652feb

File tree

5 files changed

+100
-37
lines changed

5 files changed

+100
-37
lines changed

front/components/InfiniteScroll.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const InfiniteScroll = ({
1515
showLoader,
1616
loader,
1717
}: InfiniteScrollProps) => {
18+
1819
const { ref, inView } = useInView();
1920
useEffect(() => {
2021
if (inView && hasMore) {

front/components/assistant/conversation/input_bar/InputBarAttachmentsPicker.tsx

+16-37
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
getLocationForDataSourceViewContentNode,
2626
getVisualForDataSourceViewContentNode,
2727
} from "@app/lib/content_nodes";
28-
import { useSpaces, useSpacesSearch } from "@app/lib/swr/spaces";
28+
import { useSpaces, useSpaceSearchWithInfiniteScroll, useSpacesSearch } from "@app/lib/swr/spaces";
2929
import type { DataSourceViewContentNode, LightWorkspaceType } from "@app/types";
3030
import { MIN_SEARCH_QUERY_SIZE } from "@app/types";
3131

@@ -67,9 +67,7 @@ export const InputBarAttachmentsPicker = ({
6767
const fileInputRef = useRef<HTMLInputElement>(null);
6868
const itemsContainerRef = useRef<HTMLDivElement>(null);
6969
const [isOpen, setIsOpen] = useState(false);
70-
const [currentResultNodes, setCurrentResultNodes] = useState<
71-
DataSourceViewContentNode[]
72-
>([]);
70+
7371

7472
const {
7573
inputValue: search,
@@ -81,23 +79,15 @@ export const InputBarAttachmentsPicker = ({
8179
minLength: MIN_SEARCH_QUERY_SIZE,
8280
});
8381

84-
const {
85-
cursorPagination,
86-
reset: resetPagination,
87-
handleLoadNext,
88-
pageIndex,
89-
} = useCursorPagination(PAGE_SIZE);
90-
9182
const { spaces, isSpacesLoading } = useSpaces({ workspaceId: owner.sId });
92-
const { searchResultNodes, isSearchLoading, nextPageCursor } =
93-
useSpacesSearch({
83+
84+
85+
const { searchResultNodes, isSearchLoading, hasMore, nextPage } =
86+
useSpaceSearchWithInfiniteScroll({
9487
includeDataSources: true,
9588
owner,
9689
search: searchQuery,
9790
viewType: "all",
98-
disabled: isSpacesLoading || !searchQuery,
99-
spaceIds: spaces.map((s) => s.sId),
100-
pagination: cursorPagination,
10191
});
10292

10393
const attachedNodeIds = useMemo(() => {
@@ -115,22 +105,11 @@ export const InputBarAttachmentsPicker = ({
115105
}
116106
};
117107

118-
useEffect(() => {
119-
resetPagination();
120-
}, [searchQuery, resetPagination]);
121-
122-
useEffect(() => {
123-
if (searchResultNodes && !isSearchLoading) {
124-
const unfoldedNodes = getUnfoldedNodes(searchResultNodes);
125-
setCurrentResultNodes((prevResultNodes) => {
126-
if (pageIndex === 0) {
127-
return unfoldedNodes;
128-
} else {
129-
return [...prevResultNodes, ...unfoldedNodes];
130-
}
131-
});
132-
}
133-
}, [searchResultNodes, isSearchLoading, pageIndex]);
108+
// useEffect(() => {
109+
// resetPagination();
110+
// }, [searchQuery, resetPagination]);
111+
112+
const unfoldedNodes = getUnfoldedNodes(searchResultNodes);
134113

135114
return (
136115
<DropdownMenu
@@ -197,8 +176,8 @@ export const InputBarAttachmentsPicker = ({
197176
<DropdownMenuSeparator />
198177
<ScrollArea className="flex max-h-96 flex-col" hideScrollBar>
199178
<div className="pt-0" ref={itemsContainerRef}>
200-
{currentResultNodes.length > 0 ? (
201-
currentResultNodes.map((item, index) => (
179+
{unfoldedNodes.length > 0 ? (
180+
unfoldedNodes.map((item, index) => (
202181
<DropdownMenuItem
203182
key={index}
204183
label={item.title}
@@ -232,9 +211,9 @@ export const InputBarAttachmentsPicker = ({
232211
)}
233212
</div>
234213
<InfiniteScroll
235-
nextPage={() => handleLoadNext(nextPageCursor)}
236-
hasMore={!!nextPageCursor}
237-
showLoader={currentResultNodes.length > 0 && isSearchLoading}
214+
nextPage={nextPage}
215+
hasMore={hasMore}
216+
showLoader={unfoldedNodes.length > 0 && isSearchLoading}
238217
loader={<Loader />}
239218
/>
240219
<ScrollBar className="py-0" />

front/lib/api/search.ts

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export type DataSourceContentNode = ContentNodeWithParent & {
3131
export type SearchResult = {
3232
nodes: DataSourceContentNode[];
3333
warningCode: SearchWarningCode | null;
34+
nextPageCursor: string | null;
3435
};
3536

3637
type SearchError = {

front/lib/swr/spaces.ts

+81
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { PostWorkspaceSearchResponseBodySchema, PostWorkspaceSearchResponseBodyType } from "@dust-tt/client";
12
import { useSendNotification } from "@dust-tt/sparkle";
23
import { useMemo } from "react";
34
import type { Fetcher, KeyedMutator } from "swr";
@@ -6,9 +7,11 @@ import type { CursorPaginationParams } from "@app/lib/api/pagination";
67
import { getDisplayNameForDataSource } from "@app/lib/data_sources";
78
import { getSpaceName } from "@app/lib/spaces";
89
import {
10+
appendPaginationParams,
911
fetcher,
1012
fetcherWithBody,
1113
getErrorFromResponse,
14+
useSWRInfiniteWithDefaults,
1215
useSWRWithDefaults,
1316
} from "@app/lib/swr/swr";
1417
import type {
@@ -778,3 +781,81 @@ export function useSpacesSearch({
778781
nextPageCursor: data?.nextPageCursor || null,
779782
};
780783
}
784+
785+
export function useSpaceSearchWithInfiniteScroll({
786+
includeDataSources = false,
787+
nodeIds,
788+
owner,
789+
search,
790+
spaceIds,
791+
viewType,
792+
pageSize = 25,
793+
}: SpacesSearchParams & { pageSize?: number }): {
794+
isSearchLoading: boolean;
795+
isSearchError: boolean;
796+
isSearchValidating: boolean;
797+
searchResultNodes: DataSourceContentNode[];
798+
totalNodesCount: number;
799+
// warningCode: SearchWarningCode | null;
800+
nextPage: () => Promise<void>;
801+
hasMore: boolean;
802+
} {
803+
804+
const body = {
805+
query: search,
806+
viewType,
807+
nodeIds,
808+
spaceIds,
809+
includeDataSources,
810+
limit: pageSize,
811+
};
812+
813+
// Only perform a query if we have a valid search
814+
const url =
815+
(search && search.length >= MIN_SEARCH_QUERY_SIZE) || nodeIds
816+
? `/api/w/${owner.sId}/search`
817+
: null;
818+
819+
820+
const nodesFetcher: Fetcher<PostWorkspaceSearchResponseBodyType> = fetcherWithBody;
821+
822+
const { data, error, setSize, size, isValidating, isLoading } = useSWRInfiniteWithDefaults(
823+
(pageIndex, previousPageData) => {
824+
if (!url) {
825+
return null;
826+
}
827+
828+
const params = new URLSearchParams();
829+
830+
params.append('limit', pageSize.toString());
831+
832+
if (previousPageData) {
833+
params.append("cursor", previousPageData?.nextPageCursor);
834+
}
835+
836+
return JSON.stringify([url + "?" + params.toString(), body]);
837+
},
838+
nodesFetcher,
839+
{
840+
revalidateOnFocus: false,
841+
revalidateOnReconnect: false,
842+
}
843+
);
844+
845+
return {
846+
searchResultNodes: useMemo(
847+
() => (data ? data.flatMap((d) => (d ? d.nodes : [])) : []),
848+
[data]
849+
),
850+
isSearchLoading: isLoading,
851+
isSearchError: error,
852+
isSearchValidating: isValidating,
853+
totalNodesCount: data?.[0] ? data[0].total : 0,
854+
// warningCode: data?.warningCode,
855+
hasMore: data?.[size - 1] ? data[size - 1].nextPageCursor !== null : false,
856+
nextPage: async () => {
857+
await setSize((size) => size + 1);
858+
},
859+
};
860+
}
861+

front/pages/api/w/[wId]/search.ts

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export type DataSourceContentNode = ContentNodeWithParent & {
2222
export type PostWorkspaceSearchResponseBody = {
2323
nodes: DataSourceContentNode[];
2424
warningCode: SearchWarningCode | null;
25+
nextPageCursor: string | null;
2526
};
2627

2728
async function handler(

0 commit comments

Comments
 (0)