Spaces:
Running
Running
import { SpaceProps } from "./type"; | |
export const fetchAllPages = async (sort = "trending") => { | |
const filter_key = "zero-a10g"; | |
const pageNumbers = Array.from({ length: 55 }, (_, i) => i); | |
const urls = pageNumbers.map(async (pageNumber) => { | |
const url = `https://huggingface.co/spaces-json?p=${pageNumber}&sort=${sort}&runtime.hardware.current=${filter_key}`; | |
const response = await fetch(url); | |
const json = await response.json(); | |
return json?.spaces ?? []; | |
}); | |
const jsonResponses = await Promise.all(urls); | |
const spaces = jsonResponses.flat(); | |
return spaces | |
}; | |
export const sortByCreatedAt = (spaces: SpaceProps[]) => { | |
return spaces.sort((a, b) => { | |
return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime(); | |
}); | |
} | |
export const sortByLikes = (spaces: SpaceProps[]) => { | |
return spaces.sort((a, b) => { | |
return b.likes - a.likes; | |
}); | |
} |