From a5a1f49bfe2aef46bda88bc3e570105de396274a Mon Sep 17 00:00:00 2001 From: Kashargul <144968721+Kashargul@users.noreply.github.com> Date: Sat, 15 Feb 2025 14:50:16 +0100 Subject: [PATCH] storage to typescript (#17144) * storage to typescript * fix * keys * keys --- .../common/{storage.js => storage.ts} | 51 +++++++++++++------ tgui/packages/tgui-say/TguiSay.tsx | 8 +-- tgui/packages/tgui-say/helpers.ts | 2 +- 3 files changed, 40 insertions(+), 21 deletions(-) rename tgui/packages/common/{storage.js => storage.ts} (57%) diff --git a/tgui/packages/common/storage.js b/tgui/packages/common/storage.ts similarity index 57% rename from tgui/packages/common/storage.js rename to tgui/packages/common/storage.ts index 97f8b1535ae..a794867ab0e 100644 --- a/tgui/packages/common/storage.js +++ b/tgui/packages/common/storage.ts @@ -10,6 +10,11 @@ export const IMPL_MEMORY = 0; export const IMPL_HUB_STORAGE = 1; export const IMPL_INDEXED_DB = 2; +type StorageImplementation = + | typeof IMPL_MEMORY + | typeof IMPL_HUB_STORAGE + | typeof IMPL_INDEXED_DB; + const INDEXED_DB_VERSION = 1; const INDEXED_DB_NAME = 'virgo'; const INDEXED_DB_STORE_NAME = 'storage-v1'; @@ -17,7 +22,15 @@ const INDEXED_DB_STORE_NAME = 'storage-v1'; const READ_ONLY = 'readonly'; const READ_WRITE = 'readwrite'; -const testGeneric = (testFn) => () => { +type StorageBackend = { + impl: StorageImplementation; + get(key: string): Promise; + set(key: string, value: any): Promise; + remove(key: string): Promise; + clear(): Promise; +}; + +const testGeneric = (testFn: () => boolean) => (): boolean => { try { return Boolean(testFn()); } catch { @@ -26,30 +39,33 @@ const testGeneric = (testFn) => () => { }; const testHubStorage = testGeneric( - () => window.hubStorage && window.hubStorage.getItem, + () => window.hubStorage && !!window.hubStorage.getItem, ); -class HubStorageBackend { +class HubStorageBackend implements StorageBackend { + public impl: StorageImplementation; + constructor() { this.impl = IMPL_HUB_STORAGE; } - async get(key) { - const value = await window.hubStorage.getItem('virgo-' + key); + async get(key: string): Promise { + const value = await window.hubStorage.getItem(key); if (typeof value === 'string') { return JSON.parse(value); } + return undefined; } - set(key, value) { - window.hubStorage.setItem('virgo-' + key, JSON.stringify(value)); + async set(key: string, value: any): Promise { + window.hubStorage.setItem(key, JSON.stringify(value)); } - remove(key) { - window.hubStorage.removeItem('virgo-' + key); + async remove(key: string): Promise { + window.hubStorage.removeItem(key); } - clear() { + async clear(): Promise { window.hubStorage.clear(); } } @@ -58,7 +74,10 @@ class HubStorageBackend { * Web Storage Proxy object, which selects the best backend available * depending on the environment. */ -class StorageProxy { +class StorageProxy implements StorageBackend { + private backendPromise: Promise; + public impl: StorageImplementation = IMPL_MEMORY; + constructor() { this.backendPromise = (async () => { if (!Byond.TRIDENT) { @@ -74,25 +93,25 @@ class StorageProxy { } return new HubStorageBackend(); } - })(); + })() as Promise; } - async get(key) { + async get(key: string): Promise { const backend = await this.backendPromise; return backend.get(key); } - async set(key, value) { + async set(key: string, value: any): Promise { const backend = await this.backendPromise; return backend.set(key, value); } - async remove(key) { + async remove(key: string): Promise { const backend = await this.backendPromise; return backend.remove(key); } - async clear() { + async clear(): Promise { const backend = await this.backendPromise; return backend.clear(); } diff --git a/tgui/packages/tgui-say/TguiSay.tsx b/tgui/packages/tgui-say/TguiSay.tsx index 963f90270b8..6fc93c8771c 100644 --- a/tgui/packages/tgui-say/TguiSay.tsx +++ b/tgui/packages/tgui-say/TguiSay.tsx @@ -51,11 +51,11 @@ export function TguiSay() { const [lightMode, setLightMode] = useState(false); const [value, setValue] = useState(''); - function handleArrowKeys(direction: KEY.Up | KEY.Down): void { + function handleArrowKeys(direction: KEY.PageUp | KEY.PageDown): void { const chat = chatHistory.current; const iterator = channelIterator.current; - if (direction === KEY.Up) { + if (direction === KEY.PageUp) { if (chat.isAtLatest() && value) { // Save current message to temp history if at the most recent message chat.saveTemp(value); @@ -234,8 +234,8 @@ export function TguiSay() { event.currentTarget.selectionEnd = selectionEnd + 2; } break; - case KEY.Up: - case KEY.Down: + case KEY.PageUp: + case KEY.PageDown: event.preventDefault(); handleArrowKeys(event.key); break; diff --git a/tgui/packages/tgui-say/helpers.ts b/tgui/packages/tgui-say/helpers.ts index 89327ed05ea..c39f6887476 100644 --- a/tgui/packages/tgui-say/helpers.ts +++ b/tgui/packages/tgui-say/helpers.ts @@ -48,7 +48,7 @@ function setWindowVisibility(visible: boolean): void { }); } -const CHANNEL_REGEX = /^[:.]\w|,b\s/; +const CHANNEL_REGEX = /^[:.]\w\s|^,b\s/; /** Tests for a channel prefix, returning it or none */ export function getPrefix(