1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-24 20:36:40 +07:00

added optional sorting to lab

This commit is contained in:
Fedor Katurov 2022-03-23 17:08:41 +07:00
parent 04ea68d94e
commit 566e1192cd
5 changed files with 21 additions and 9 deletions

View file

@ -1,2 +1,4 @@
NEXT_PUBLIC_API_HOST=https://pig.staging.vault48.org/
NEXT_PUBLIC_REMOTE_CURRENT=https://pig.staging.vault48.org/static/
#NEXT_PUBLIC_API_HOST=https://pig.staging.vault48.org/
#NEXT_PUBLIC_REMOTE_CURRENT=https://pig.staging.vault48.org/static/
NEXT_PUBLIC_API_HOST=http://localhost:8888/
NEXT_PUBLIC_REMOTE_CURRENT=http://localhost:8888/static/

View file

@ -7,9 +7,9 @@ import {
} from '~/types/lab';
import { api, cleanResult } from '~/utils/api';
export const getLabNodes = ({ after }: GetLabNodesRequest) =>
export const getLabNodes = ({ after, sort }: GetLabNodesRequest) =>
api
.get<GetLabNodesResult>(API.LAB.NODES, { params: { after } })
.get<GetLabNodesResult>(API.LAB.NODES, { params: { after, sort } })
.then(cleanResult);
export const getLabStats = () => api.get<GetLabStatsResult>(API.LAB.STATS).then(cleanResult);

View file

@ -6,10 +6,13 @@ import { getLabNodes } from '~/api/lab';
import { useAuth } from '~/hooks/auth/useAuth';
import { useLabStore } from '~/store/lab/useLabStore';
import { INode } from '~/types';
import { GetLabNodesRequest, ILabNode } from '~/types/lab';
import { GetLabNodesRequest, ILabNode, LabNodesSort } from '~/types/lab';
import { flatten, last, uniqBy } from '~/utils/ramda';
const getKey: (isUser: boolean) => SWRInfiniteKeyLoader = isUser => (index, prev: ILabNode[]) => {
const getKey: (isUser: boolean, sort?: LabNodesSort) => SWRInfiniteKeyLoader = (isUser, sort) => (
index,
prev: ILabNode[]
) => {
if (!isUser) return null;
if (index > 0 && !prev?.length) return null;
@ -20,6 +23,7 @@ const getKey: (isUser: boolean) => SWRInfiniteKeyLoader = isUser => (index, prev
const props: GetLabNodesRequest = {
after: lastNode?.node.commented_at || lastNode?.node.created_at,
sort: sort || LabNodesSort.New,
};
return JSON.stringify(props);
@ -33,12 +37,12 @@ const parseKey = (key: string): GetLabNodesRequest => {
}
};
export const useGetLabNodes = () => {
export const useGetLabNodes = (sort?: LabNodesSort) => {
const labStore = useLabStore();
const { isUser } = useAuth();
const { data, isValidating, size, setSize, mutate } = useSWRInfinite(
getKey(isUser),
getKey(isUser, sort),
async (key: string) => {
const result = await getLabNodes(parseKey(key));
return result.nodes;

View file

@ -19,8 +19,14 @@ export type ILabState = Readonly<{
};
}>;
export enum LabNodesSort {
New = 'new',
Hot = 'hot',
}
export type GetLabNodesRequest = {
after?: string;
sort?: LabNodesSort;
};
export interface ILabNode {

File diff suppressed because one or more lines are too long