-
-
Notifications
You must be signed in to change notification settings - Fork 40
Add settings export/import #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SinnerK0N
wants to merge
6
commits into
Inrixia:master
Choose a base branch
from
SinnerK0N:settings-import-export
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0c8677f
Settings export/import
SinnerK0N b12eefe
Settings import/export - replace raw idb calls with reactivestores an…
SinnerK0N 1477a19
Settings export/import - more improvements
SinnerK0N 6a1c82b
Settings export/import - export all feature flags
SinnerK0N 7edc769
Settings export/import - use userOverrides for feature flags
SinnerK0N 4815134
Settings export/import - wrap multiline statements in {}
SinnerK0N File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| /** | ||
| * Download content as a file | ||
| */ | ||
| export const downloadObject = (content: string, filename: string, type: string) => | ||
| { | ||
| const blob = new Blob([content], { type }); | ||
| const url = URL.createObjectURL(blob); | ||
|
|
||
| const a = document.createElement("a"); | ||
| a.href = url; | ||
| a.download = filename; | ||
|
|
||
| a.click(); | ||
|
|
||
| URL.revokeObjectURL(url); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
plugins/ui/src/SettingsPage/SettingsTab/LunaSettingsTransfer.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| import React from "react"; | ||
| import { useConfirm } from "material-ui-confirm"; | ||
|
|
||
| import Stack from "@mui/material/Stack"; | ||
| import FileDownloadIcon from "@mui/icons-material/FileDownload"; | ||
| import FileUploadIcon from "@mui/icons-material/FileUpload"; | ||
|
|
||
| import { Messager, SettingsTransfer, type ExportData } from "@luna/core"; | ||
| import { downloadObject, redux, Tidal } from "@luna/lib"; | ||
| import { relaunch } from "plugins/lib.native/src/index.native"; | ||
|
|
||
| import { LunaButton, LunaSettings, LunaSwitchSetting } from "../../components"; | ||
|
|
||
| export const LunaSettingsTransfer = React.memo(() => | ||
| { | ||
| const confirm = useConfirm(); | ||
| const fileInputRef = React.useRef<HTMLInputElement>(null); | ||
| const [busy, setBusy] = React.useState(false); | ||
| const [stripCode, setStripCode] = React.useState(true); | ||
|
|
||
| const onExport = React.useCallback(async () => | ||
| { | ||
| setBusy(true); | ||
| try | ||
| { | ||
| //feature flags | ||
| const featureFlags = redux.store.getState().featureFlags.userOverrides as Record<string, boolean>;; | ||
|
|
||
| const data = await SettingsTransfer.dump(stripCode, Object.keys(featureFlags).length > 0 ? featureFlags : null); | ||
|
|
||
| const dateStr = new Date().toISOString().replace(/[:.]/g, "-").slice(0, 19); | ||
| downloadObject(JSON.stringify(data), `tidaluna-settings-${dateStr}.json`, "application/json"); | ||
| } | ||
| catch (err: any) | ||
| { | ||
| Messager.Error("Failed to export settings: ", err.message); | ||
| } | ||
| finally | ||
| { | ||
| setBusy(false); | ||
| } | ||
| }, [stripCode]); | ||
|
|
||
| const onImportClick = React.useCallback(() => | ||
| { | ||
| fileInputRef.current?.click(); | ||
| }, []); | ||
|
|
||
| const onFileSelected = React.useCallback(async (event: React.ChangeEvent<HTMLInputElement>) => | ||
| { | ||
| const file = event.target.files?.[0]; | ||
| if (!file) | ||
| return; | ||
|
|
||
| event.target.value = ""; | ||
|
|
||
| try | ||
| { | ||
| const text = await file.text(); | ||
| const data: ExportData = JSON.parse(text); | ||
|
|
||
| if (!SettingsTransfer.validate(data)) | ||
| { | ||
| Messager.Error("Invalid settings file format"); | ||
| return; | ||
| } | ||
|
|
||
| const result = await confirm({ | ||
| title: "Import Settings", | ||
| description: `Import settings exported on ${new Date(data.timestamp).toLocaleString()}? Existing settings will be cleared and replaced, then the app will restart.`, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment says "Existing settings will be cleared and replaced" but seems not to? |
||
| confirmationText: "Import & Restart", | ||
| }); | ||
| if (!result.confirmed) | ||
| return; | ||
|
|
||
| setBusy(true); | ||
|
|
||
| //stores | ||
| await SettingsTransfer.restore(data); | ||
|
|
||
| //feature flags | ||
| if (data.featureFlags != null) | ||
| { | ||
| const currentFlags = Tidal.featureFlags; | ||
| for (const [name, value] of Object.entries(data.featureFlags)) | ||
| { | ||
| if (name in currentFlags && currentFlags[name].value !== value) | ||
| redux.actions["featureFlags/TOGGLE_USER_OVERRIDE"]({ ...currentFlags[name], value }); | ||
| } | ||
| } | ||
|
|
||
| Messager.Info("Settings imported successfully, restarting..."); | ||
|
|
||
| await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
| await relaunch(); | ||
| } | ||
| catch (err: any) | ||
| { | ||
| Messager.Error("Failed to import settings: ", err.message); | ||
| } | ||
| finally | ||
| { | ||
| setBusy(false); | ||
| } | ||
| }, []); | ||
|
|
||
| return ( | ||
| <LunaSettings title="Settings Transfer" desc="Exports installed plugins, plugin settings, themes, store URLs and feature flag overrides. Import clears existing settings, restores them and restarts the app."> | ||
| <Stack direction="row" spacing={2}> | ||
| <LunaButton disabled={busy} onClick={onExport} startIcon={<FileDownloadIcon />} children="Export Settings" /> | ||
| <LunaButton disabled={busy} onClick={onImportClick} startIcon={<FileUploadIcon />} children="Import Settings" /> | ||
| <input ref={fileInputRef} type="file" accept=".json" style={{ display: "none" }} onChange={onFileSelected} /> | ||
| </Stack> | ||
| <LunaSwitchSetting | ||
| title="Include plugin source code" | ||
| desc="Including plugin source code will increase the size of the exported file. This is only useful for exporting dev or unreleased plugins." | ||
| checked={!stripCode} | ||
| onClick={() => setStripCode(!stripCode)} | ||
| /> | ||
| </LunaSettings> | ||
| ); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import { ReactiveStore } from "./ReactiveStore"; | ||
| import { LunaPlugin } from "./LunaPlugin"; | ||
|
|
||
| export interface ExportData | ||
| { | ||
| version: 1; //future proofing -> if anything changes we want the ability to load old exports correctly | ||
| timestamp: string; | ||
| stores: Record<string, Record<string, unknown>>; | ||
| featureFlags: Record<string, boolean> | null; | ||
| } | ||
|
|
||
| export class SettingsTransfer | ||
| { | ||
| //new stores to be added here | ||
| private static readonly exportableStores: ReactiveStore[] = | ||
| [ | ||
| ReactiveStore.getStore("@luna/pluginStorage"), | ||
| LunaPlugin.pluginStorage, //@luna/plugins | ||
| ReactiveStore.getStore("@luna/storage"), | ||
| ]; | ||
|
|
||
| public static async dump(stripCode: boolean = true, featureFlags: Record<string, boolean> | null = null): Promise<ExportData> | ||
| { | ||
| const stores: Record<string, Record<string, unknown>> = {}; | ||
| for (const store of this.exportableStores) | ||
| { | ||
| if (store === LunaPlugin.pluginStorage) | ||
| stores[store.idbName] = await LunaPlugin.dumpStorage(stripCode); | ||
| else | ||
| stores[store.idbName] = await store.dump(); | ||
| } | ||
|
|
||
| return { | ||
| version: 1, | ||
| timestamp: new Date().toISOString(), | ||
| stores, | ||
| featureFlags, | ||
| }; | ||
| } | ||
|
|
||
| public static async restore(data: ExportData) | ||
| { | ||
| for (const store of this.exportableStores) | ||
| { | ||
| const storeData = data.stores[store.idbName]; | ||
| if (!storeData) | ||
| continue; | ||
|
|
||
| await store.clear(); | ||
|
|
||
| for (const [key, value] of Object.entries(storeData)) | ||
| await store.set(key, value); | ||
| } | ||
| } | ||
|
|
||
| public static validate(data: unknown): data is ExportData | ||
| { | ||
| if (typeof data !== "object" || data === null) | ||
| return false; | ||
|
|
||
| const obj = data as Record<string, unknown>; | ||
| if (obj.version !== 1) | ||
| return false; | ||
|
|
||
| if (typeof obj.stores !== "object" || obj.stores === null) | ||
| return false; | ||
|
|
||
| return true; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo double semicolon