Skip to content

Commit

Permalink
feat: implement multiple featured bounties support
Browse files Browse the repository at this point in the history
  • Loading branch information
Shoaibdev7 authored Dec 30, 2024
1 parent 7b99bc0 commit b2a0ba2
Showing 1 changed file with 49 additions and 14 deletions.
63 changes: 49 additions & 14 deletions src/store/bountyStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import { makeAutoObservable } from 'mobx';
interface FeaturedBounty {
bountyId: string;
url: string;
addedAt: number;
title?: string;
}

class BountyStore {
featuredBounty: FeaturedBounty | null = null;
featuredBounties: FeaturedBounty[] = [];
maxFeaturedBounties = 3;

constructor() {
makeAutoObservable(this);
Expand All @@ -15,9 +18,23 @@ class BountyStore {

private loadFromStorage(): void {
try {
const saved = localStorage.getItem('featuredBounty');
const saved = localStorage.getItem('featuredBounties');
if (saved) {
this.featuredBounty = JSON.parse(saved);
this.featuredBounties = JSON.parse(saved);
return;
}

const oldSaved = localStorage.getItem('featuredBounty');
if (oldSaved) {
const oldBounty = JSON.parse(oldSaved);
this.featuredBounties = [
{
...oldBounty,
addedAt: Date.now()
}
];
this.saveToStorage();
localStorage.removeItem('featuredBounty');
}
} catch (error) {
console.error('Error loading from storage:', error);
Expand All @@ -26,7 +43,7 @@ class BountyStore {

private saveToStorage(): void {
try {
localStorage.setItem('featuredBounty', JSON.stringify(this.featuredBounty));
localStorage.setItem('featuredBounties', JSON.stringify(this.featuredBounties));
} catch (error) {
console.error('Error saving to storage:', error);
}
Expand All @@ -37,25 +54,43 @@ class BountyStore {
return match ? match[1] : null;
}

addFeaturedBounty(url: string): void {
addFeaturedBounty(url: string, title?: string): void {
const bountyId = this.getBountyIdFromURL(url);
if (bountyId) {
this.featuredBounty = { bountyId, url };
this.saveToStorage();
}
if (!bountyId || this.hasBounty(bountyId)) return;

const newBounty: FeaturedBounty = {
bountyId,
url,
title,
addedAt: Date.now()
};

this.featuredBounties = [newBounty, ...this.featuredBounties].slice(
0,
this.maxFeaturedBounties
);

this.saveToStorage();
}

removeFeaturedBounty(): void {
this.featuredBounty = null;
removeFeaturedBounty(bountyId: string): void {
this.featuredBounties = this.featuredBounties.filter(
(b: FeaturedBounty) => b.bountyId !== bountyId
);
this.saveToStorage();
}

hasBounty(bountyId: string): boolean {
return this.featuredBounty?.bountyId === bountyId;
return this.featuredBounties.some((b: FeaturedBounty) => b.bountyId === bountyId);
}

getFeaturedBounty(): FeaturedBounty | null {
return this.featuredBounty;
getFeaturedBounties(): FeaturedBounty[] {
return this.featuredBounties;
}

clearFeaturedBounties(): void {
this.featuredBounties = [];
this.saveToStorage();
}
}

Expand Down

0 comments on commit b2a0ba2

Please sign in to comment.