forked from stellar-kracken/swipely_frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortcutContext.tsx
More file actions
115 lines (103 loc) · 2.96 KB
/
Copy pathShortcutContext.tsx
File metadata and controls
115 lines (103 loc) · 2.96 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
import React, { createContext, useContext, useState, useEffect } from "react";
export type ShortcutCategory = "Navigation" | "Actions" | "General";
export interface Shortcut {
id: string;
keys: string; // e.g., "g h" or "/"
label: string;
category: ShortcutCategory;
description?: string;
}
interface ShortcutContextType {
shortcuts: Shortcut[];
updateShortcut: (id: string, newKeys: string) => void;
resetToDefaults: () => void;
isHelpOpen: boolean;
setHelpOpen: (open: boolean) => void;
}
const DEFAULT_SHORTCUTS: Shortcut[] = [
{ id: "nav-home", keys: "g h", label: "Go to Home", category: "Navigation" },
{
id: "nav-bridges",
keys: "g b",
label: "Go to Bridges",
category: "Navigation",
},
{
id: "nav-assets",
keys: "g a",
label: "Go to Assets",
category: "Navigation",
},
{
id: "nav-liquidity",
keys: "g l",
label: "Go to Liquidity",
category: "Navigation",
},
{ id: "action-search", keys: "/", label: "Search", category: "Actions" },
{
id: "action-refresh",
keys: "r",
label: "Refresh Data",
category: "Actions",
},
{ id: "action-theme", keys: "t", label: "Toggle Theme", category: "Actions" },
{ id: "general-help", keys: "?", label: "Show Help", category: "General" },
];
const ShortcutContext = createContext<ShortcutContextType | undefined>(
undefined
);
const STORAGE_KEY = "sbw_user_shortcuts";
export const ShortcutProvider: React.FC<{ children: React.ReactNode }> = ({
children,
}) => {
const [shortcuts, setShortcuts] = useState<Shortcut[]>(DEFAULT_SHORTCUTS);
const [isHelpOpen, setHelpOpen] = useState(false);
// Load from localStorage on mount
useEffect(() => {
const saved = localStorage.getItem(STORAGE_KEY);
if (saved) {
try {
const parsed = JSON.parse(saved);
// Merge defaults with saved to handle new shortcuts in updates
const merged = DEFAULT_SHORTCUTS.map((def) => {
const user = parsed.find((p: Shortcut) => p.id === def.id);
return user ? { ...def, keys: user.keys } : def;
});
setShortcuts(merged);
} catch (e) {
console.error("Failed to parse shortcuts", e);
}
}
}, []);
const updateShortcut = (id: string, newKeys: string) => {
const updated = shortcuts.map((s) =>
s.id === id ? { ...s, keys: newKeys } : s
);
setShortcuts(updated);
localStorage.setItem(STORAGE_KEY, JSON.stringify(updated));
};
const resetToDefaults = () => {
setShortcuts(DEFAULT_SHORTCUTS);
localStorage.removeItem(STORAGE_KEY);
};
return (
<ShortcutContext.Provider
value={{
shortcuts,
updateShortcut,
resetToDefaults,
isHelpOpen,
setHelpOpen,
}}
>
{children}
</ShortcutContext.Provider>
);
};
export const useShortcuts = () => {
const context = useContext(ShortcutContext);
if (!context)
throw new Error("useShortcuts must be used within ShortcutProvider");
return context;
};