|
| 1 | +import { showConfirmationModal } from 'components/ConfirmationModal'; |
| 2 | +import { extensionBarStorageKey, useExtensionsBarStore } from 'components/stores/extensionsBarStore'; |
| 3 | +import { usePopupsStore } from 'components/stores/popupsStore'; |
| 4 | +import React from 'react'; |
| 5 | +import { MdArrowBack, MdDelete, MdRefresh } from 'react-icons/md'; |
| 6 | + |
| 7 | +export function StorageManager(): React.ReactNode { |
| 8 | + const [storages, setStorages] = React.useState<[string, unknown][]>([]); |
| 9 | + const { setManagerPopup } = usePopupsStore(); |
| 10 | + |
| 11 | + React.useEffect(() => { |
| 12 | + refreshStorages(); |
| 13 | + }, []); |
| 14 | + |
| 15 | + function refreshStorages(): void { |
| 16 | + const storage: [string, unknown][] = Object.entries(localStorage).filter(([key]) => |
| 17 | + key.endsWith('::local') |
| 18 | + || key.endsWith('::session') |
| 19 | + || key.endsWith('::sync') |
| 20 | + || key.endsWith('::managed')).sort(([keyA], [keyB]) => keyA.localeCompare(keyB)); |
| 21 | + storage.push(...Object.entries(localStorage).filter(([key]) => key.includes('extendium'))); |
| 22 | + setStorages(storage); |
| 23 | + } |
| 24 | + |
| 25 | + function renderObject(obj: object, keyStr: string, depth = 0): React.ReactNode { |
| 26 | + return ( |
| 27 | + <div className="storage-manager-item" style={{ paddingLeft: `${depth * 10}px` }}> |
| 28 | + <details key={keyStr}> |
| 29 | + <summary>{keyStr}</summary> |
| 30 | + {Object.entries(obj).map(([key, value]) => ( |
| 31 | + typeof value === 'object' && value !== null |
| 32 | + ? renderObject(value as object, key, depth + 1) |
| 33 | + : <div className="storage-manager-item" style={{ paddingLeft: `${(depth + 1) * 10}px` }} key={key}>{key}: {String(value)}</div> |
| 34 | + ))} |
| 35 | + </details> |
| 36 | + </div> |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + function removeStorage(key: string): void { |
| 41 | + showConfirmationModal({ |
| 42 | + title: 'Clear storage', |
| 43 | + description: ( |
| 44 | + <p>Are you sure you want to clear out the <br /> "{formatStorageKey(key)}"?</p> |
| 45 | + ), |
| 46 | + okButtonText: 'Clear', |
| 47 | + onOK: () => { |
| 48 | + localStorage.removeItem(key); |
| 49 | + if (key === extensionBarStorageKey) { |
| 50 | + useExtensionsBarStore.setState({ extensionsOrder: [] }); |
| 51 | + } |
| 52 | + refreshStorages(); |
| 53 | + }, |
| 54 | + bNeverPopOut: true, |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + function formatStorageKey(key: string): string { |
| 59 | + const [extensionId, area] = key.split('::'); |
| 60 | + |
| 61 | + if (area === undefined) { |
| 62 | + return extensionId ?? 'Unknown'; |
| 63 | + } |
| 64 | + |
| 65 | + return `${extensionId} (${area} storage)`; |
| 66 | + } |
| 67 | + |
| 68 | + return ( |
| 69 | + <div className="storage-manager"> |
| 70 | + <div className="header"> |
| 71 | + <button onClick={() => { setManagerPopup({ route: null }); }} type="button" className="md-button"> |
| 72 | + <MdArrowBack /> |
| 73 | + </button> |
| 74 | + <h1>Storage Manager</h1> |
| 75 | + <button type="button" onClick={() => { refreshStorages(); }} className="md-button"><MdRefresh /></button> |
| 76 | + </div> |
| 77 | + <p> |
| 78 | + Here you can manage all data that Extendium has stored for each extension. |
| 79 | + <br /> |
| 80 | + This is useful if you want to clear out all data for a specific extension if for example it is not working properly or if you want to reset settings. |
| 81 | + <br /> |
| 82 | + All this data is stored in the Steam browser's local storage and can be accessed through the browser's developer tools on any steamloopback address like the extension backgrounds. |
| 83 | + </p> |
| 84 | + |
| 85 | + {storages.map(([key, value]) => ( |
| 86 | + <div key={key} className="storage-manager-row"> |
| 87 | + {renderObject(JSON.parse(value as string) as object, formatStorageKey(key))} |
| 88 | + <button type="button" onClick={() => { removeStorage(key); }}><MdDelete /></button> |
| 89 | + </div> |
| 90 | + ))} |
| 91 | + </div> |
| 92 | + ); |
| 93 | +} |
0 commit comments