aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/libs/recent-history.jsx
blob: 7a7bb8ef938b34f85c8fe40168ff7e852b47770a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import _ from 'underscore';

const MAX_ITEMS = 10;
const STORAGE_KEY = 'sonar_recent_history';

class RecentHistory {
  static getRecentHistory () {
    let sonarHistory = localStorage.getItem(STORAGE_KEY);
    if (sonarHistory == null) {
      sonarHistory = [];
    } else {
      sonarHistory = JSON.parse(sonarHistory);
    }
    return sonarHistory;
  }

  static clear () {
    localStorage.removeItem(STORAGE_KEY);
  }

  static add (resourceKey, resourceName, icon) {
    if (resourceKey !== '') {
      let newEntry = { key: resourceKey, name: resourceName, icon: icon };

      let newHistory = this.getRecentHistory()
          .filter(item => item.key !== newEntry.key)
          .slice(0, MAX_ITEMS - 1);
      newHistory.unshift(newEntry);

      localStorage.setItem(STORAGE_KEY, JSON.stringify(newHistory));
    }
  }
}

export default RecentHistory;