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:
parent
04ea68d94e
commit
566e1192cd
5 changed files with 21 additions and 9 deletions
|
@ -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/
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
Loading…
Add table
Add a link
Reference in a new issue