forked from MatiasDesuu/ThinkDashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.go
More file actions
840 lines (713 loc) · 24.8 KB
/
models.go
File metadata and controls
840 lines (713 loc) · 24.8 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
"sync"
)
type Bookmark struct {
Name string `json:"name"`
URL string `json:"url"`
Shortcut string `json:"shortcut"`
Category string `json:"category"`
CheckStatus bool `json:"checkStatus"`
Icon string `json:"icon"`
}
type Finder struct {
Name string `json:"name"`
SearchUrl string `json:"searchUrl"`
Shortcut string `json:"shortcut"`
}
type Category struct {
ID string `json:"id"`
Name string `json:"name"`
OriginalID string `json:"originalId,omitempty"` // Track original ID for renames
}
type Page struct {
ID int `json:"id"` // Numeric ID matching the file number (bookmarks-1.json = id: 1)
Name string `json:"name"` // Editable page name
}
type PageWithBookmarks struct {
Page Page `json:"page"`
Categories []Category `json:"categories,omitempty"`
Bookmarks []Bookmark `json:"bookmarks"`
}
type PageOrder struct {
Order []int `json:"order"` // Array of page IDs in display order
}
type Settings struct {
CurrentPage int `json:"currentPage"` // Numeric ID of the current page
Theme string `json:"theme"` // "light" or "dark"
OpenInNewTab bool `json:"openInNewTab"`
ColumnsPerRow int `json:"columnsPerRow"`
FontSize string `json:"fontSize"` // "small", "medium", or "large"
ShowBackgroundDots bool `json:"showBackgroundDots"`
ShowTitle bool `json:"showTitle"`
ShowDate bool `json:"showDate"`
ShowConfigButton bool `json:"showConfigButton"`
ShowSearchButton bool `json:"showSearchButton"`
ShowFindersButton bool `json:"showFindersButton"`
ShowCommandsButton bool `json:"showCommandsButton"`
ShowSearchButtonText bool `json:"showSearchButtonText"`
ShowFindersButtonText bool `json:"showFindersButtonText"`
ShowCommandsButtonText bool `json:"showCommandsButtonText"`
ShowStatus bool `json:"showStatus"`
ShowPing bool `json:"showPing"`
ShowStatusLoading bool `json:"showStatusLoading"`
SkipFastPing bool `json:"skipFastPing"`
GlobalShortcuts bool `json:"globalShortcuts"` // Use shortcuts from all pages
HyprMode bool `json:"hyprMode"` // Launcher mode for PWA usage
AnimationsEnabled bool `json:"animationsEnabled"` // Enable or disable animations globally
EnableCustomTitle bool `json:"enableCustomTitle"` // Enable custom page title
CustomTitle string `json:"customTitle"` // Custom page title
ShowPageInTitle bool `json:"showPageInTitle"` // Show current page name in title
ShowPageNamesInTabs bool `json:"showPageNamesInTabs"` // Show page names in tabs instead of numbers
EnableCustomFavicon bool `json:"enableCustomFavicon"` // Enable custom favicon
CustomFaviconPath string `json:"customFaviconPath"` // Path to custom favicon file
EnableCustomFont bool `json:"enableCustomFont"` // Enable custom font
CustomFontPath string `json:"customFontPath"` // Path to custom font file
Language string `json:"language"` // Language code, e.g., "en" or "es"
InterleaveMode bool `json:"interleaveMode"` // Interleave mode for search (/ for shortcuts, direct input for fuzzy)
ShowPageTabs bool `json:"showPageTabs"` // Show page navigation tabs
AlwaysCollapseCategories bool `json:"alwaysCollapseCategories"` // Always collapse categories on load
EnableFuzzySuggestions bool `json:"enableFuzzySuggestions"` // Enable fuzzy suggestions in shortcut search
FuzzySuggestionsStartWith bool `json:"fuzzySuggestionsStartWith"` // Fuzzy suggestions start with query instead of contains
KeepSearchOpenWhenEmpty bool `json:"keepSearchOpenWhenEmpty"` // Keep search interface open when query is empty
ShowIcons bool `json:"showIcons"` // Show bookmark icons
IncludeFindersInSearch bool `json:"includeFindersInSearch"` // Include finders in normal search
}
type ColorTheme struct {
Light ThemeColors `json:"light"`
Dark ThemeColors `json:"dark"`
Custom map[string]ThemeColors `json:"custom"` // Custom themes with dynamic keys
}
type ThemeColors struct {
Name string `json:"name,omitempty"` // Optional name for custom themes
TextPrimary string `json:"textPrimary"`
TextSecondary string `json:"textSecondary"`
TextTertiary string `json:"textTertiary"`
BackgroundPrimary string `json:"backgroundPrimary"`
BackgroundSecondary string `json:"backgroundSecondary"`
BackgroundDots string `json:"backgroundDots"`
BackgroundModal string `json:"backgroundModal"`
BorderPrimary string `json:"borderPrimary"`
BorderSecondary string `json:"borderSecondary"`
AccentSuccess string `json:"accentSuccess"`
AccentWarning string `json:"accentWarning"`
AccentError string `json:"accentError"`
}
type Store interface {
// Bookmarks - per page only
GetBookmarksByPage(pageID int) []Bookmark
GetAllBookmarks() []Bookmark
SaveBookmarksByPage(pageID int, bookmarks []Bookmark)
AddBookmarkToPage(pageID int, bookmark Bookmark)
DeleteBookmarkFromPage(pageID int, bookmark Bookmark) error
// Categories - per page only
GetCategoriesByPage(pageID int) []Category
SaveCategoriesByPage(pageID int, categories []Category)
// Finders
GetFinders() []Finder
SaveFinders(finders []Finder)
// Pages
GetPages() []Page
SavePage(page Page, bookmarks []Bookmark)
DeletePage(pageID int) error
GetPageOrder() []int
SavePageOrder(order []int)
// Settings
GetSettings() Settings
SaveSettings(settings Settings)
// Colors
GetColors() ColorTheme
SaveColors(colors ColorTheme)
}
type FileStore struct {
settingsFile string
colorsFile string
pageOrderFile string
dataDir string
mutex sync.RWMutex
}
func NewStore() Store {
store := &FileStore{
settingsFile: "data/settings.json",
colorsFile: "data/colors.json",
pageOrderFile: "data/pages.json",
dataDir: "data",
}
// Initialize default files if they don't exist
store.initializeDefaultFiles()
return store
}
func (fs *FileStore) initializeDefaultFiles() {
fs.ensureDataDir()
// Initialize bookmarks for main page if file doesn't exist
mainPageBookmarksFile := "data/bookmarks-1.json"
if _, err := os.Stat(mainPageBookmarksFile); os.IsNotExist(err) {
defaultPageWithBookmarks := PageWithBookmarks{
Page: Page{
ID: 1,
Name: "main",
},
Categories: []Category{
{ID: "development", Name: "Development"},
{ID: "media", Name: "Media"},
{ID: "social", Name: "Social"},
{ID: "search", Name: "Search"},
{ID: "utilities", Name: "Utilities"},
},
Bookmarks: []Bookmark{
{Name: "GitHub", URL: "https://github.com", Shortcut: "G", Category: "development", CheckStatus: false},
{Name: "GitHub Issues", URL: "https://github.com/issues", Shortcut: "GI", Category: "development", CheckStatus: false},
{Name: "GitHub Pull Requests", URL: "https://github.com/pulls", Shortcut: "GP", Category: "development", CheckStatus: false},
{Name: "YouTube", URL: "https://youtube.com", Shortcut: "Y", Category: "media", CheckStatus: false},
{Name: "YouTube Studio", URL: "https://studio.youtube.com", Shortcut: "YS", Category: "media", CheckStatus: false},
{Name: "Twitter", URL: "https://twitter.com", Shortcut: "T", Category: "social", CheckStatus: false},
{Name: "TikTok", URL: "https://tiktok.com", Shortcut: "TT", Category: "social", CheckStatus: false},
{Name: "Google", URL: "https://google.com", Shortcut: "", Category: "search", CheckStatus: false},
},
}
data, _ := json.MarshalIndent(defaultPageWithBookmarks, "", " ")
os.WriteFile(mainPageBookmarksFile, data, 0644)
}
// Initialize settings if file doesn't exist
if _, err := os.Stat(fs.settingsFile); os.IsNotExist(err) {
defaultSettings := Settings{
CurrentPage: 1,
Theme: "dark",
OpenInNewTab: true,
ColumnsPerRow: 3,
FontSize: "medium",
ShowBackgroundDots: true,
ShowTitle: true,
ShowDate: true,
ShowConfigButton: true,
ShowSearchButton: true,
ShowFindersButton: false,
ShowCommandsButton: false,
ShowSearchButtonText: true,
ShowFindersButtonText: true,
ShowCommandsButtonText: true,
ShowStatus: false,
ShowPing: false,
ShowStatusLoading: false,
SkipFastPing: false,
GlobalShortcuts: true,
HyprMode: false,
AnimationsEnabled: true,
EnableCustomTitle: false,
CustomTitle: "",
ShowPageInTitle: false,
ShowPageNamesInTabs: false,
EnableCustomFavicon: false,
CustomFaviconPath: "",
EnableCustomFont: false,
CustomFontPath: "",
Language: "en",
InterleaveMode: false,
ShowPageTabs: true,
AlwaysCollapseCategories: false,
EnableFuzzySuggestions: false,
FuzzySuggestionsStartWith: false,
KeepSearchOpenWhenEmpty: false,
ShowIcons: false,
IncludeFindersInSearch: false,
}
data, _ := json.MarshalIndent(defaultSettings, "", " ")
os.WriteFile(fs.settingsFile, data, 0644)
}
// Initialize colors if file doesn't exist
if _, err := os.Stat(fs.colorsFile); os.IsNotExist(err) {
defaultColors := getDefaultColors()
data, _ := json.MarshalIndent(defaultColors, "", " ")
os.WriteFile(fs.colorsFile, data, 0644)
}
}
func (fs *FileStore) ensureDataDir() {
os.MkdirAll("data", 0755)
}
// getDefaultNewPageCategories returns the default categories for a newly created page
func getDefaultNewPageCategories() []Category {
return []Category{
{ID: "others", Name: "dashboard.others"},
}
}
func (fs *FileStore) GetBookmarksByPage(pageID int) []Bookmark {
fs.mutex.RLock()
defer fs.mutex.RUnlock()
fs.ensureDataDir()
// Read directly from bookmarks-{pageID}.json
filePath := fmt.Sprintf("%s/bookmarks-%d.json", fs.dataDir, pageID)
data, err := os.ReadFile(filePath)
if err != nil {
return []Bookmark{}
}
var pageWithBookmarks PageWithBookmarks
if err := json.Unmarshal(data, &pageWithBookmarks); err != nil {
return []Bookmark{}
}
return pageWithBookmarks.Bookmarks
}
func (fs *FileStore) SaveBookmarksByPage(pageID int, bookmarks []Bookmark) {
fs.mutex.Lock()
defer fs.mutex.Unlock()
fs.ensureDataDir()
// Read the existing page data
filePath := fmt.Sprintf("%s/bookmarks-%d.json", fs.dataDir, pageID)
data, err := os.ReadFile(filePath)
if err != nil {
// If file doesn't exist, create new page with this ID and default categories
pageWithBookmarks := PageWithBookmarks{
Page: Page{
ID: pageID,
Name: fmt.Sprintf("Page %d", pageID),
},
Categories: getDefaultNewPageCategories(),
Bookmarks: bookmarks,
}
newData, _ := json.MarshalIndent(pageWithBookmarks, "", " ")
os.WriteFile(filePath, newData, 0644)
return
}
var pageWithBookmarks PageWithBookmarks
if err := json.Unmarshal(data, &pageWithBookmarks); err != nil {
return
}
// Update only bookmarks, preserve page metadata and categories
pageWithBookmarks.Bookmarks = bookmarks
newData, _ := json.MarshalIndent(pageWithBookmarks, "", " ")
os.WriteFile(filePath, newData, 0644)
}
func (fs *FileStore) AddBookmarkToPage(pageID int, bookmark Bookmark) {
fs.mutex.Lock()
defer fs.mutex.Unlock()
fs.ensureDataDir()
// Read the existing page data
filePath := fmt.Sprintf("%s/bookmarks-%d.json", fs.dataDir, pageID)
data, err := os.ReadFile(filePath)
if err != nil {
// If file doesn't exist, create new page with this ID and default categories
pageWithBookmarks := PageWithBookmarks{
Page: Page{
ID: pageID,
Name: fmt.Sprintf("Page %d", pageID),
},
Categories: getDefaultNewPageCategories(),
Bookmarks: []Bookmark{bookmark},
}
newData, _ := json.MarshalIndent(pageWithBookmarks, "", " ")
os.WriteFile(filePath, newData, 0644)
return
}
var pageWithBookmarks PageWithBookmarks
if err := json.Unmarshal(data, &pageWithBookmarks); err != nil {
return
}
// Add the new bookmark to existing bookmarks
pageWithBookmarks.Bookmarks = append(pageWithBookmarks.Bookmarks, bookmark)
newData, _ := json.MarshalIndent(pageWithBookmarks, "", " ")
os.WriteFile(filePath, newData, 0644)
}
func (fs *FileStore) DeleteBookmarkFromPage(pageID int, bookmarkToDelete Bookmark) error {
fs.mutex.Lock()
defer fs.mutex.Unlock()
fs.ensureDataDir()
// Read the existing page data
filePath := fmt.Sprintf("%s/bookmarks-%d.json", fs.dataDir, pageID)
data, err := os.ReadFile(filePath)
if err != nil {
return err
}
var pageWithBookmarks PageWithBookmarks
if err := json.Unmarshal(data, &pageWithBookmarks); err != nil {
return err
}
// Find and remove the bookmark
originalLength := len(pageWithBookmarks.Bookmarks)
pageWithBookmarks.Bookmarks = fs.removeBookmarkFromSlice(pageWithBookmarks.Bookmarks, bookmarkToDelete)
// If no bookmark was removed, return error
if len(pageWithBookmarks.Bookmarks) == originalLength {
return fmt.Errorf("bookmark not found")
}
// Save the updated data
newData, err := json.MarshalIndent(pageWithBookmarks, "", " ")
if err != nil {
return err
}
return os.WriteFile(filePath, newData, 0644)
}
func (fs *FileStore) removeBookmarkFromSlice(bookmarks []Bookmark, toDelete Bookmark) []Bookmark {
result := make([]Bookmark, 0)
removed := false
for _, b := range bookmarks {
if !removed && b.Name == toDelete.Name && b.URL == toDelete.URL {
removed = true
// Skip this bookmark (remove only the first match)
} else {
result = append(result, b)
}
}
return result
}
func (fs *FileStore) GetAllBookmarks() []Bookmark {
fs.mutex.RLock()
defer fs.mutex.RUnlock()
fs.ensureDataDir()
// Get all pages
pages := fs.GetPages()
var allBookmarks []Bookmark
// Collect bookmarks from all pages
for _, page := range pages {
pageBookmarks := fs.GetBookmarksByPage(page.ID)
allBookmarks = append(allBookmarks, pageBookmarks...)
}
return allBookmarks
}
func (fs *FileStore) GetFinders() []Finder {
fs.mutex.RLock()
defer fs.mutex.RUnlock()
fs.ensureDataDir()
filePath := fmt.Sprintf("%s/finders.json", fs.dataDir)
data, err := os.ReadFile(filePath)
if err != nil {
return []Finder{}
}
var finders []Finder
if err := json.Unmarshal(data, &finders); err != nil {
return []Finder{}
}
return finders
}
func (fs *FileStore) SaveFinders(finders []Finder) {
fs.mutex.Lock()
defer fs.mutex.Unlock()
fs.ensureDataDir()
filePath := fmt.Sprintf("%s/finders.json", fs.dataDir)
data, err := json.MarshalIndent(finders, "", " ")
if err != nil {
return
}
os.WriteFile(filePath, data, 0644)
}
// GetCategoriesByPage returns categories stored inside bookmarks-{pageID}.json if present
func (fs *FileStore) GetCategoriesByPage(pageID int) []Category {
fs.mutex.RLock()
defer fs.mutex.RUnlock()
fs.ensureDataDir()
filePath := fmt.Sprintf("%s/bookmarks-%d.json", fs.dataDir, pageID)
data, err := os.ReadFile(filePath)
if err != nil {
return []Category{}
}
var pageWithBookmarks PageWithBookmarks
if err := json.Unmarshal(data, &pageWithBookmarks); err != nil {
return []Category{}
}
return pageWithBookmarks.Categories
}
// SaveCategoriesByPage saves categories inside bookmarks-{pageID}.json, creating the file if needed
// It also updates bookmarks to use the new category IDs when category names change
func (fs *FileStore) SaveCategoriesByPage(pageID int, categories []Category) {
fs.mutex.Lock()
defer fs.mutex.Unlock()
fs.ensureDataDir()
filePath := fmt.Sprintf("%s/bookmarks-%d.json", fs.dataDir, pageID)
data, err := os.ReadFile(filePath)
if err != nil {
// Create new page file with provided categories and empty bookmarks
// Note: This is called when explicitly saving categories for a page
pageWithBookmarks := PageWithBookmarks{
Page: Page{
ID: pageID,
Name: fmt.Sprintf("Page %d", pageID),
},
Categories: categories,
Bookmarks: []Bookmark{},
}
newData, _ := json.MarshalIndent(pageWithBookmarks, "", " ")
os.WriteFile(filePath, newData, 0644)
return
}
var pageWithBookmarks PageWithBookmarks
if err := json.Unmarshal(data, &pageWithBookmarks); err != nil {
return
}
// Create a mapping from old category IDs to new category IDs
// This allows us to update bookmarks when category names (and thus IDs) change
oldToNewCategoryMap := make(map[string]string)
// Build the mapping using originalId if available, otherwise try to match by position or name
for i, newCat := range categories {
// If originalId is set, use it to find the old category
if newCat.OriginalID != "" {
oldToNewCategoryMap[newCat.OriginalID] = newCat.ID
// Also map from current ID to new ID in case they're different
if newCat.OriginalID != newCat.ID {
oldToNewCategoryMap[newCat.OriginalID] = newCat.ID
}
} else if i < len(pageWithBookmarks.Categories) {
// Fallback: map by position if originalId is not available
oldCat := pageWithBookmarks.Categories[i]
oldToNewCategoryMap[oldCat.ID] = newCat.ID
}
}
// Update bookmarks to use new category IDs
for i := range pageWithBookmarks.Bookmarks {
oldCategoryID := pageWithBookmarks.Bookmarks[i].Category
if newCategoryID, exists := oldToNewCategoryMap[oldCategoryID]; exists {
pageWithBookmarks.Bookmarks[i].Category = newCategoryID
}
}
pageWithBookmarks.Categories = categories
newData, _ := json.MarshalIndent(pageWithBookmarks, "", " ")
os.WriteFile(filePath, newData, 0644)
}
func (fs *FileStore) GetPages() []Page {
fs.mutex.RLock()
defer fs.mutex.RUnlock()
return fs.getPages()
}
func (fs *FileStore) getPages() []Page {
fs.ensureDataDir()
var pages []Page
// Read all bookmarks files in data directory
files, err := os.ReadDir(fs.dataDir)
if err != nil {
return []Page{{ID: 1, Name: "main"}}
}
// First, collect all pages from bookmark files
pageMap := make(map[int]Page)
for _, file := range files {
if file.IsDir() || !strings.HasPrefix(file.Name(), "bookmarks-") || !strings.HasSuffix(file.Name(), ".json") {
continue
}
filePath := fmt.Sprintf("%s/%s", fs.dataDir, file.Name())
data, err := os.ReadFile(filePath)
if err != nil {
continue
}
var pageWithBookmarks PageWithBookmarks
if err := json.Unmarshal(data, &pageWithBookmarks); err != nil {
continue
}
pageMap[pageWithBookmarks.Page.ID] = pageWithBookmarks.Page
}
if len(pageMap) == 0 {
return []Page{{ID: 1, Name: "main"}}
}
// Get the order from pages.json
order := fs.getPageOrder()
// If no order file exists, create default order
if len(order) == 0 {
for id := range pageMap {
order = append(order, id)
}
// Save the default order
fs.savePageOrder(order)
}
// Build pages array in the specified order
for _, id := range order {
if page, exists := pageMap[id]; exists {
pages = append(pages, page)
}
}
// Add any pages that exist in files but not in order
for id, page := range pageMap {
found := false
for _, orderId := range order {
if orderId == id {
found = true
break
}
}
if !found {
pages = append(pages, page)
}
}
return pages
}
func (fs *FileStore) GetPageOrder() []int {
fs.mutex.RLock()
defer fs.mutex.RUnlock()
return fs.getPageOrder()
}
func (fs *FileStore) getPageOrder() []int {
fs.ensureDataDir()
data, err := os.ReadFile(fs.pageOrderFile)
if err != nil {
return []int{}
}
var pageOrder PageOrder
if err := json.Unmarshal(data, &pageOrder); err != nil {
return []int{}
}
return pageOrder.Order
}
func (fs *FileStore) SavePageOrder(order []int) {
fs.mutex.Lock()
defer fs.mutex.Unlock()
fs.savePageOrder(order)
}
func (fs *FileStore) savePageOrder(order []int) {
fs.ensureDataDir()
pageOrder := PageOrder{
Order: order,
}
data, _ := json.MarshalIndent(pageOrder, "", " ")
os.WriteFile(fs.pageOrderFile, data, 0644)
}
func (fs *FileStore) SavePage(page Page, bookmarks []Bookmark) {
fs.mutex.Lock()
defer fs.mutex.Unlock()
fs.ensureDataDir()
// The page ID IS the file number
// bookmarks-1.json has page.id = 1
// When saving, try to preserve existing categories stored in the file
fileName := fmt.Sprintf("%s/bookmarks-%d.json", fs.dataDir, page.ID)
var existing PageWithBookmarks
if data, err := os.ReadFile(fileName); err == nil {
_ = json.Unmarshal(data, &existing)
}
pageWithBookmarks := PageWithBookmarks{
Page: page,
Categories: existing.Categories,
Bookmarks: bookmarks,
}
if pageWithBookmarks.Categories == nil {
pageWithBookmarks.Categories = getDefaultNewPageCategories()
}
data, _ := json.MarshalIndent(pageWithBookmarks, "", " ")
os.WriteFile(fileName, data, 0644)
}
func (fs *FileStore) DeletePage(pageID int) error {
fs.mutex.Lock()
defer fs.mutex.Unlock()
fs.ensureDataDir()
// Delete bookmarks-{pageID}.json
filePath := fmt.Sprintf("%s/bookmarks-%d.json", fs.dataDir, pageID)
return os.Remove(filePath)
}
func (fs *FileStore) GetSettings() Settings {
fs.mutex.RLock()
defer fs.mutex.RUnlock()
fs.ensureDataDir()
data, err := os.ReadFile(fs.settingsFile)
if err != nil {
// Return default settings if file doesn't exist
return Settings{
CurrentPage: 1,
Theme: "dark",
OpenInNewTab: true,
ColumnsPerRow: 3,
FontSize: "m",
ShowBackgroundDots: true,
ShowTitle: true,
ShowDate: true,
ShowConfigButton: true,
ShowSearchButton: true,
ShowFindersButton: false,
ShowCommandsButton: false,
ShowSearchButtonText: true,
ShowFindersButtonText: true,
ShowCommandsButtonText: true,
ShowStatus: false,
ShowPing: false,
ShowStatusLoading: false,
SkipFastPing: false,
GlobalShortcuts: true,
HyprMode: false,
AnimationsEnabled: true,
EnableCustomTitle: false,
CustomTitle: "",
ShowPageInTitle: false,
ShowPageNamesInTabs: false,
EnableCustomFavicon: false,
CustomFaviconPath: "",
EnableCustomFont: false,
CustomFontPath: "",
Language: "en",
InterleaveMode: false,
ShowPageTabs: true,
AlwaysCollapseCategories: false,
EnableFuzzySuggestions: false,
FuzzySuggestionsStartWith: false,
KeepSearchOpenWhenEmpty: false,
ShowIcons: false,
IncludeFindersInSearch: false,
}
}
var settings Settings
json.Unmarshal(data, &settings)
// Set default language if empty
if settings.Language == "" {
settings.Language = "en"
}
return settings
}
func (fs *FileStore) SaveSettings(settings Settings) {
fs.mutex.Lock()
defer fs.mutex.Unlock()
fs.ensureDataDir()
data, _ := json.MarshalIndent(settings, "", " ")
os.WriteFile(fs.settingsFile, data, 0644)
}
func getDefaultColors() ColorTheme {
return ColorTheme{
Light: ThemeColors{
TextPrimary: "#1F2937",
TextSecondary: "#6B7280",
TextTertiary: "#9CA3AF",
BackgroundPrimary: "#F9FAFB",
BackgroundSecondary: "#F3F4F6",
BackgroundDots: "#E5E7EB",
BackgroundModal: "rgba(255, 255, 255, 0.9)",
BorderPrimary: "#D1D5DB",
BorderSecondary: "#E5E7EB",
AccentSuccess: "#059669",
AccentWarning: "#D97706",
AccentError: "#DC2626",
},
Dark: ThemeColors{
TextPrimary: "#E5E7EB",
TextSecondary: "#9CA3AF",
TextTertiary: "#6B7280",
BackgroundPrimary: "#000",
BackgroundSecondary: "#1F2937",
BackgroundDots: "#1F2937",
BackgroundModal: "rgba(0, 0, 0, 0.8)",
BorderPrimary: "#4B5563",
BorderSecondary: "#374151",
AccentSuccess: "#10B981",
AccentWarning: "#F59E0B",
AccentError: "#EF4444",
},
Custom: make(map[string]ThemeColors), // Initialize empty custom themes map
}
}
func (fs *FileStore) GetColors() ColorTheme {
fs.mutex.RLock()
defer fs.mutex.RUnlock()
fs.ensureDataDir()
data, err := os.ReadFile(fs.colorsFile)
if err != nil {
// Return default colors if file doesn't exist
return getDefaultColors()
}
var colors ColorTheme
if err := json.Unmarshal(data, &colors); err != nil {
return getDefaultColors()
}
// Ensure custom themes map is initialized
if colors.Custom == nil {
colors.Custom = make(map[string]ThemeColors)
}
return colors
}
func (fs *FileStore) SaveColors(colors ColorTheme) {
fs.mutex.Lock()
defer fs.mutex.Unlock()
fs.ensureDataDir()
data, _ := json.MarshalIndent(colors, "", " ")
os.WriteFile(fs.colorsFile, data, 0644)
}