Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

feat: get top 20 popular icons from backend #9819

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions pages/api/icons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import logger from "@config/logger";
import connectMongo from "@config/mongo";
import Link from "@models/Link";

export default async function handler(req, res) {
if (req.method != "GET") {
return res
.status(400)
.json({ error: "Invalid request: GET request required" });
}

const icons = await getPopularIcons();
return res.status(200).json(icons);
}

export async function getPopularIcons() {
await connectMongo();
let icons = [];
try {
icons = await Link.aggregate([
{
$group: {
_id: "$icon",
count: { $count: {} },
},
},
{
$sort:{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the formatting of this code is not inline with the project, check you have Prettier VScode plugin installed, it will fix this on save

count: -1
}
},
{
$limit: 20
},
{
$project: { _id: 0, icon: "$_id"},
}
]).exec();
icons = icons.map( icon => icon.icon )
} catch (e) {
logger.error(e, "Failed to load popular icons");
icons = [];
}

return JSON.parse(JSON.stringify(icons));
}
34 changes: 10 additions & 24 deletions pages/icons.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import PageHead from "@components/PageHead";
import { PROJECT_NAME } from "@constants/index";
import { useRouter } from "next/router";
import Button from "@components/Button";
import { getPopularIcons } from "./api/icons";

export async function getServerSideProps(){
const popularIcons = await getPopularIcons();
return {
props: { popularIcons }
}
}

const icons = {};

Expand All @@ -20,30 +28,8 @@ Object.keys(SiIcons).forEach((key) => {
icons[key.toLocaleLowerCase()] = key;
});

const popularIcons = [
"FaGithub",
"FaTwitter",
"FaLinkedin",
"FaGit",
"FaXTwitter",
"FaInstagram",
"SiHashnode",
"FaLink",
"FaYoutube",
"FaGlobe",
"FaDev",
"FaDiscord",
"FaMedium",
"SiMedium",
"FaFacebook",
"FaGithubAlt",
"SiLinkedin",
"SiLeetcode",
"FaDollarSign",
"FaMastodon",
];

export default function Icons() {
export default function Icons({ popularIcons }) {
const [notFound, setNotFound] = useState();

const router = useRouter();
Expand Down Expand Up @@ -89,7 +75,7 @@ export default function Icons() {
}
return filteredIconNames;
},
[keyword],
[keyword, popularIcons],
);
return (
<>
Expand Down