Skip to content

Commit

Permalink
sort collections list by name (#555)
Browse files Browse the repository at this point in the history
Signed-off-by: ryjiang <[email protected]>
  • Loading branch information
shanghaikid authored Jun 25, 2024
1 parent 199bcce commit f5ea764
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
26 changes: 19 additions & 7 deletions client/src/context/Data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ export const DataProvider = (props: { children: React.ReactNode }) => {
try {
// set loading true
setLoading(true);
// set collections
setCollections([]);
// fetch collections
const res = await CollectionService.getCollections();
// check state
Expand Down Expand Up @@ -343,6 +345,18 @@ export const DataProvider = (props: { children: React.ReactNode }) => {
};

useEffect(() => {
const clear = () => {
// clear collections
setCollections([]);
// clear database
setDatabases([]);
// set connected to false
setConnected(false);
// remove all listeners when component unmount
socket.current?.offAny();
socket.current?.disconnect();
};

if (isAuth) {
// update database get from auth
setDatabase(authReq.database);
Expand Down Expand Up @@ -370,14 +384,12 @@ export const DataProvider = (props: { children: React.ReactNode }) => {
socket.current?.disconnect();
});
} else {
socket.current?.disconnect();
// clear collections
setCollections([]);
// clear database
setDatabases([]);
// set connected to false
setConnected(false);
clear();
}

return () => {
clear();
};
}, [isAuth]);

useEffect(() => {
Expand Down
22 changes: 7 additions & 15 deletions server/src/collections/collections.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,12 +438,12 @@ export class CollectionsService {
// get all collections details
async getAllCollections(
clientId: string,
collectionName: string[] = []
collections: string[] = []
): Promise<CollectionObject[]> {
const cache = clientCache.get(clientId);

// clear collectionsQueue
if (collectionName.length === 0) {
// clear collectionsQueue if we fetch all collections
if (collections.length === 0) {
cache.collectionsQueue.stop();
cache.collectionsQueue = new SimpleQueue<string>();
}
Expand All @@ -460,12 +460,15 @@ export class CollectionsService {

// get target collections details
const targetCollections = allCollections.data.filter(
d => collectionName.indexOf(d.name) !== -1
d => collections.indexOf(d.name) !== -1
);

const targets =
targetCollections.length > 0 ? targetCollections : allCollections.data;

// sort targets by name
targets.sort((a, b) => a.name.localeCompare(b.name));

// get all collection details
for (let i = 0; i < targets.length; i++) {
const collection = targets[i];
Expand Down Expand Up @@ -496,17 +499,6 @@ export class CollectionsService {
}, 5);
}

// sort data by loadedPercentage and has index or not, then createdTime.
data.sort((a, b) => {
if (a.loadedPercentage === b.loadedPercentage && a.schema && b.schema) {
if (a.schema.hasVectorIndex === b.schema.hasVectorIndex) {
return b.createdTime - a.createdTime;
}
return a.schema.hasVectorIndex ? -1 : 1;
}
return (b.loadedPercentage || 0) - (a.loadedPercentage || 0);
});

// return data
return data;
}
Expand Down

0 comments on commit f5ea764

Please sign in to comment.