Replaces all direct usage of browser globals and infrastructure service imports in UI components with dedicated application/state backend hooks. Introduces lint rules to prevent direct access to backend bridges and localStorage from components, promoting a cleaner separation of concerns and improved maintainability. Moves user preferences (e.g., port forwarding form mode) to persistent state hooks, updates port forwarding and SFTP logic to rely on backend hooks, and centralizes logging through a logger utility. Cleans up debug code and removes obsolete scripts from HTML. Improves testability, prepares for alternative backend implementations, and enforces architectural boundaries.
46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
const safeParse = <T>(value: string | null): T | null => {
|
|
if (!value) return null;
|
|
try {
|
|
return JSON.parse(value) as T;
|
|
} catch {
|
|
return null;
|
|
}
|
|
};
|
|
|
|
export const localStorageAdapter = {
|
|
read<T>(key: string): T | null {
|
|
return safeParse<T>(localStorage.getItem(key));
|
|
},
|
|
write<T>(key: string, value: T) {
|
|
localStorage.setItem(key, JSON.stringify(value));
|
|
},
|
|
readString(key: string): string | null {
|
|
return localStorage.getItem(key);
|
|
},
|
|
writeString(key: string, value: string) {
|
|
localStorage.setItem(key, value);
|
|
},
|
|
readBoolean(key: string): boolean | null {
|
|
const value = localStorage.getItem(key);
|
|
if (value === null) return null;
|
|
if (value === "true") return true;
|
|
if (value === "false") return false;
|
|
return null;
|
|
},
|
|
writeBoolean(key: string, value: boolean) {
|
|
localStorage.setItem(key, value ? "true" : "false");
|
|
},
|
|
readNumber(key: string): number | null {
|
|
const value = localStorage.getItem(key);
|
|
if (!value) return null;
|
|
const num = parseInt(value, 10);
|
|
return isNaN(num) ? null : num;
|
|
},
|
|
writeNumber(key: string, value: number) {
|
|
localStorage.setItem(key, String(value));
|
|
},
|
|
remove(key: string) {
|
|
localStorage.removeItem(key);
|
|
},
|
|
};
|