File size: 1,981 Bytes
0c45221
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
class Store {
    constructor() {
        this.state = {
            user: null,
            projects: [],
            currentProject: null,
            buckets: [],
            currentBucket: null,
            files: [],
            apiKeys: [],
            notifications: []
        };
    }

    setUser(user) {
        this.state.user = user;
        this.save();
    }

    addProject(project) {
        this.state.projects.push(project);
        this.save();
    }

    setCurrentProject(projectId) {
        this.state.currentProject = this.state.projects.find(p => p.id === projectId);
        this.save();
    }

    addBucket(bucket) {
        this.state.buckets.push(bucket);
        this.save();
    }

    setCurrentBucket(bucketId) {
        this.state.currentBucket = this.state.buckets.find(b => b.id === bucketId);
        this.save();
    }

    addFile(file) {
        this.state.files.push(file);
        this.save();
    }

    removeFile(fileId) {
        this.state.files = this.state.files.filter(f => f.id !== fileId);
        this.save();
    }

    addApiKey(apiKey) {
        this.state.apiKeys.push(apiKey);
        this.save();
    }

    removeApiKey(keyId) {
        this.state.apiKeys = this.state.apiKeys.filter(k => k.id !== keyId);
        this.save();
    }

    addNotification(notification) {
        this.state.notifications.push(notification);
        setTimeout(() => {
            this.removeNotification(notification.id);
        }, 3000);
        this.save();
    }

    removeNotification(id) {
        this.state.notifications = this.state.notifications.filter(n => n.id !== id);
        this.save();
    }

    save() {
        localStorage.setItem('bucketmaster-state', JSON.stringify(this.state));
    }

    load() {
        const saved = localStorage.getItem('bucketmaster-state');
        if (saved) {
            this.state = JSON.parse(saved);
        }
    }
}

const store = new Store();
store.load();

export default store;