Skip to content

Commit

Permalink
fix: load offchain spaces sorted by popularity when loaded without fi…
Browse files Browse the repository at this point in the history
…lters
  • Loading branch information
wa0x6e committed May 30, 2024
1 parent 87ecbca commit 8eddcca
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
30 changes: 28 additions & 2 deletions apps/ui/src/networks/offchain/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ApolloClient, createHttpLink, InMemoryCache } from '@apollo/client/core';
import {
SPACES_RANKING_QUERY,
RANKING_QUERY,
SPACE_QUERY,
PROPOSALS_QUERY,
PROPOSAL_QUERY,
Expand All @@ -25,6 +26,7 @@ import {
} from '@/types';
import { ApiSpace, ApiProposal, ApiVote } from './types';
import { DEFAULT_VOTING_DELAY } from '../constants';
import { clone } from '@/helpers/utils';

const DEFAULT_AUTHENTICATOR = 'OffchainAuthenticator';

Expand Down Expand Up @@ -329,18 +331,42 @@ export function createApi(uri: string, networkId: NetworkID): NetworkApi {
{ limit, skip = 0 }: PaginationOpts,
filter?: SpacesFilter
): Promise<Space[]> => {
let _filters = clone(filter || {});

if (!_filters?.id_in) {
const { data } = await apollo.query({
query: RANKING_QUERY,
variables: {
first: limit,
skip,
where: {
..._filters
}
}
});

_filters = { id_in: data.ranking.items.map(space => space.id) };
skip = 0;
}

const { data } = await apollo.query({
query: SPACES_RANKING_QUERY,
variables: {
first: limit,
skip,
where: {
...filter
..._filters
}
}
});

return data.spaces.map(space => formatSpace(space, networkId));
const spaces: Space[] = data.spaces.map(space => formatSpace(space, networkId));

if (_filters.id_in) {
spaces.sort((a, b) => _filters.id_in!.indexOf(a.id) - _filters.id_in!.indexOf(b.id));
}

return spaces;
},
loadSpace: async (id: string): Promise<Space | null> => {
const { data } = await apollo.query({
Expand Down
11 changes: 11 additions & 0 deletions apps/ui/src/networks/offchain/api/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ export const SPACES_RANKING_QUERY = gql`
${SPACE_FRAGMENT}
`;

export const RANKING_QUERY = gql`
query ($first: Int, $skip: Int, $where: RankingWhere) {
ranking(first: $first, skip: $skip, where: $where) {
items {
...offchainSpaceFragment
}
}
}
${SPACE_FRAGMENT}
`;

export const SPACE_QUERY = gql`
query ($id: String!) {
space(id: $id) {
Expand Down

0 comments on commit 8eddcca

Please sign in to comment.