Skip to content
Open
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
52 changes: 27 additions & 25 deletions src/components/TopRepos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -324,39 +324,41 @@ export default function TopRepos() {
);
}, []);

const togglePin = async (repoFullName: string) => {
const isPinned = pinnedRepos.includes(repoFullName);
let newPinsArray: string[];

if (isPinned) {
newPinsArray = pinnedRepos.filter(name => name !== repoFullName);
} else {
if (pinnedRepos.length >= 3) {
setPinError("Maximum 3 pins allowed");
return;
}
newPinsArray = [...pinnedRepos, repoFullName];
}

const togglePin = useCallback((repoFullName: string) => {
setPinError(null);

const prevPins = [...pinnedRepos];
setPinnedRepos(newPinsArray);
setPinnedRepos((prevPins) => {
const isPinned = prevPins.includes(repoFullName);
let newPinsArray: string[];

if (isPinned) {
newPinsArray = prevPins.filter((name) => name !== repoFullName);
} else {
if (prevPins.length >= 3) {
setPinError("Maximum 3 pins allowed");
return prevPins;
}
newPinsArray = [...prevPins, repoFullName];
}

try {
const res = await fetch("/api/user/settings", {
fetch("/api/user/settings", {
method: "PATCH",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ pinned_repos: newPinsArray }),
});
if (!res.ok) throw new Error("Failed to update pins");
} catch (err) {
console.error(err);
setPinnedRepos(prevPins);
}
};
})
.then((res) => {
if (!res.ok) throw new Error("Failed to update pins");
})
.catch((err) => {
console.error(err);
setPinnedRepos(prevPins);
});

return newPinsArray;
});
}, []);

const fetchRepos = useCallback(() => {
setLoading(true);
Expand Down