Skip to content

Commit

Permalink
Keep milestones when switching repo (#311)
Browse files Browse the repository at this point in the history
Milestones are not saved even when filters are kept.

This is inconsistent with the meaning of keeping filters.

Let's implement keeping milestones across repos.
  • Loading branch information
Arif-Khalid authored Mar 27, 2024
1 parent 3420a73 commit b226977
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
31 changes: 31 additions & 0 deletions src/app/core/services/filters.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Injectable } from '@angular/core';
import { Sort } from '@angular/material/sort';
import { BehaviorSubject, pipe } from 'rxjs';
import { SimpleLabel } from '../models/label.model';
import { Milestone } from '../models/milestone.model';

export type Filter = {
title: string;
Expand Down Expand Up @@ -35,8 +36,12 @@ export const DEFAULT_FILTER: Filter = {
export class FiltersService {
public filter$ = new BehaviorSubject<Filter>(DEFAULT_FILTER);

// Helps in determining whether all milestones were selected from previous repo during sanitization of milestones
private previousMilestonesLength = 0;

clearFilters(): void {
this.filter$.next(DEFAULT_FILTER);
this.previousMilestonesLength = 0;
}

updateFilters(newFilters: Partial<Filter>): void {
Expand Down Expand Up @@ -68,4 +73,30 @@ export class FiltersService {

this.updateFilters({ labels: newLabels, hiddenLabels: newHiddenLabels, deselectedLabels: newDeselectedLabels });
}

sanitizeMilestones(allMilestones: Milestone[]) {
const allMilestonesSet = new Set(allMilestones.map((milestone) => milestone.title));

// All previous milestones were selected, reset to all new milestones selected
if (this.filter$.value.milestones.length === this.previousMilestonesLength) {
this.updateFilters({ milestones: [...allMilestonesSet] });
this.previousMilestonesLength = allMilestones.length;
return;
}

const newMilestones: string[] = [];
for (const milestone of this.filter$.value.milestones) {
if (allMilestonesSet.has(milestone)) {
newMilestones.push(milestone);
}
}

// No applicable milestones, reset to all milestones selected
if (newMilestones.length === 0) {
newMilestones.push(...allMilestonesSet);
}

this.updateFilters({ milestones: newMilestones });
this.previousMilestonesLength = allMilestones.length;
}
}
2 changes: 1 addition & 1 deletion src/app/shared/filter-bar/filter-bar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class FilterBarComponent implements OnInit, OnDestroy {
this.milestoneSubscription = this.milestoneService.fetchMilestones().subscribe(
(response) => {
this.logger.debug('IssuesViewerComponent: Fetched milestones from Github');
this.filtersService.updateFilters({ milestones: this.milestoneService.milestones.map((milestone) => milestone.title) });
this.filtersService.sanitizeMilestones(this.milestoneService.milestones);
},
(err) => {},
() => {}
Expand Down

0 comments on commit b226977

Please sign in to comment.