Files
csgowtf/src/utils/LocalStorage.ts
2022-03-22 10:09:57 +01:00

34 lines
996 B
TypeScript

import type { Player } from "@/api";
export const SaveLastVisitedToLocalStorage = (data: Player): void => {
const a = JSON.parse(localStorage.getItem("recent-visited") || "") || [];
if (a.length === 0) {
a.unshift(data);
} else if (a.length === 9) {
if (a.find((p: Player) => p.steamid64 === data.steamid64)) {
a.shift();
a.splice(
a.findIndex((p: Player) => p.steamid64 === data.steamid64),
1
);
a.unshift(data);
} else if (!a.find((p: Player) => p.steamid64 === data.steamid64)) {
a.shift();
a.unshift(data);
}
} else if (a.length > 0 && a.length < 9) {
if (a.find((p: Player) => p.steamid64 === data.steamid64)) {
a.splice(
a.findIndex((p: Player) => p.steamid64 === data.steamid64),
1
);
a.unshift(data);
} else if (!a.find((p: Player) => p.steamid64 === data.steamid64)) {
a.unshift(data);
}
}
localStorage.setItem("recent-visited", JSON.stringify(a));
};