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

feat: most used apps hook #747

Merged
merged 13 commits into from
May 15, 2024
18 changes: 17 additions & 1 deletion src/api/apps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { App, UUID } from '@graasp/sdk';

import { PartialQueryConfigForApi } from '../types.js';
import { verifyAuthentication } from './axios.js';
import { buildAppListRoute, buildGetApiAccessTokenRoute } from './routes.js';
import {
buildAppListRoute,
buildGetApiAccessTokenRoute,
buildMostUsedAppListRoute,
} from './routes.js';

export const getApps = async ({ API_HOST, axios }: PartialQueryConfigForApi) =>
verifyAuthentication(() =>
Expand All @@ -11,6 +15,18 @@ export const getApps = async ({ API_HOST, axios }: PartialQueryConfigForApi) =>
.then(({ data }) => data),
);

export const getMostUsedApps = async ({
API_HOST,
axios,
}: PartialQueryConfigForApi) =>
verifyAuthentication(() =>
axios
.get<
{ url: string; name: string; count: number }[]
>(`${API_HOST}/${buildMostUsedAppListRoute}`)
.then(({ data }) => data),
);

export const requestApiAccessToken = async (
args: {
id: UUID;
Expand Down
1 change: 1 addition & 0 deletions src/api/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const CATEGORIES_ROUTE = `${ITEMS_ROUTE}/categories`;
export const ETHERPAD_ROUTE = `${ITEMS_ROUTE}/etherpad`;
export const COLLECTIONS_ROUTE = `collections`;
export const buildAppListRoute = `${APPS_ROUTE}/list`;
export const buildMostUsedAppListRoute = `${APPS_ROUTE}/most-used`;
export const SHORT_LINKS_ROUTE = `${ITEMS_ROUTE}/short-links`;
export const SHORT_LINKS_LIST_ROUTE = `${SHORT_LINKS_ROUTE}/list`;
export const EMBEDDED_LINKS_ROUTE = `${ITEMS_ROUTE}/embedded-links/metadata`;
Expand Down
3 changes: 3 additions & 0 deletions src/config/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ export const memberKeys = {

// subscription plan for the current member
subscription: [...currentBaseKey, 'subscription'] as const,

// apps used mostly by the member
mostUsedApps: [...currentBaseKey, 'mostUsedApps'] as const,
};
},
};
Expand Down
8 changes: 7 additions & 1 deletion src/hooks/apps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useQuery } from '@tanstack/react-query';

import * as Api from '../api/apps.js';
import { CONSTANT_KEY_STALE_TIME_MILLISECONDS } from '../config/constants.js';
import { APPS_KEY } from '../config/keys.js';
import { APPS_KEY, memberKeys } from '../config/keys.js';
import { QueryClientConfig } from '../types.js';

export default (queryConfig: QueryClientConfig) => {
Expand All @@ -16,5 +16,11 @@ export default (queryConfig: QueryClientConfig) => {
...defaultQueryOptions,
staleTime: CONSTANT_KEY_STALE_TIME_MILLISECONDS,
}),
useMostUsedApps: () =>
useQuery({
queryKey: memberKeys.current().mostUsedApps,
queryFn: () => Api.getMostUsedApps(queryConfig),
...defaultQueryOptions,
}),
};
};