1type HistoryEntry = {
2 id: number;
3 url: string;
4 title: string;
5 visit_count: number;
6 last_visited: string | null;
7 created_at: string | null;
8}
9
10export class AppService {
11 private history: HistoryEntry[] = [];
12 private shortcuts: Record<string, any> = {};
13
14 constructor() {}
15
16 async initialize(): Promise<void> {
17 try {
18 await Promise.all([
19 this.loadHistory(),
20 this.loadShortcuts()
21 ]);
22 } catch (error) {
23 console.error('Failed to initialize app:', error);
24 }
25 }
26
27 async loadHistory(): Promise<void> {
28 try {
29 const recentHistory: HistoryEntry[] = await fetch('/api/history/recent?limit=50').then(r => r.json());
30 this.history = recentHistory;
31 } catch (error) {
32 console.error('Failed to load history:', error);
33 // Mock history for development/fallback
34 this.history = [
35 {
36 id: 1,
37 url: 'https://github.com/wailsapp/wails',
38 title: 'Wails Framework',
39 visit_count: 3,
40 last_visited: new Date().toISOString(),
41 created_at: new Date().toISOString()
42 },
43 {
44 id: 2,
45 url: 'https://go.dev',
46 title: 'The Go Programming Language',
47 visit_count: 2,
48 last_visited: new Date().toISOString(),
49 created_at: new Date().toISOString()
50 },
51 {
52 id: 3,
53 url: 'https://developer.mozilla.org',
54 title: 'MDN Web Docs',
55 visit_count: 1,
56 last_visited: new Date().toISOString(),
57 created_at: new Date().toISOString()
58 }
59 ];
60 }
61 }
62
63 async loadShortcuts(): Promise<void> {
64 try {
65 const cfg = await fetch('/api/config').then(r => r.json());
66 this.shortcuts = cfg?.search_shortcuts || {};
67 } catch (error) {
68 console.error('Failed to load shortcuts:', error);
69 // Mock shortcuts for development/fallback
70 this.shortcuts = {
71 'g': { description: 'Google Search', url: 'https://google.com/search?q={query}' },
72 'gh': { description: 'GitHub Search', url: 'https://github.com/search?q={query}' },
73 'so': { description: 'Stack Overflow', url: 'https://stackoverflow.com/search?q={query}' },
74 'w': { description: 'Wikipedia', url: 'https://en.wikipedia.org/wiki/Special:Search?search={query}' }
75 };
76 }
77 }
78
79 getHistory(): HistoryEntry[] {
80 return this.history;
81 }
82
83 getShortcuts(): Record<string, any> {
84 return this.shortcuts;
85 }
86
87 async copyToClipboard(text: string): Promise<void> {
88 try {
89 await navigator.clipboard.writeText(text);
90 console.log('Copied to clipboard:', text);
91 } catch (err) {
92 console.error('Failed to copy:', err);
93 }
94 }
95}