Skip to content
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

implemented global persisted settings #2501

Merged
merged 11 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ out/
/web-ext-artifacts
/firebase-sample-dist
*.nex
src/app/utils/settings.schema.json
src/app/utils/validate_settings_schema.js
extension-builds/
coverage

Expand Down
2 changes: 0 additions & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ out/
/web-ext-artifacts
/firebase-sample-dist
*.nex
src/app/utils/settings.schema.json
src/app/utils/validate_settings_schema.js
extension-builds/
coverage

Expand Down
10 changes: 3 additions & 7 deletions packages/altair-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@
"@types/zen-observable": "^0.8.1",
"@typescript-eslint/eslint-plugin": "^7.4.0",
"@typescript-eslint/parser": "^7.4.0",
"ajv-cli": "3.1.0",
"babel-plugin-transform-async-to-generator": "6.24.1",
"babel-preset-env": "1.7.0",
"babel-preset-react": "6.24.1",
Expand Down Expand Up @@ -162,8 +161,7 @@
"ts-jest": "29.0.5",
"ts-mocks": "2.6.1",
"ts-node": "9.1.1",
"typescript": "5.2.2",
"typescript-json-schema": "0.50.1"
"typescript": "5.2.2"
},
"homepage": "https://altair-graphql.github.io/altair/",
"license": "MIT",
Expand All @@ -178,13 +176,11 @@
"scripts": {
"analyze": "ng build --stats-json && npx webpack-bundle-analyzer dist/stats.json",
"analyze:prod": "ng build --aot --stats-json && npx webpack-bundle-analyzer dist/stats.json",
"build": "yarn generate-settings-schema-validator && node --max_old_space_size=8000 ../../node_modules/@angular/cli/bin/ng build --aot --stats-json && yarn sentry:sourcemaps:inject",
"generate-settings-schema-validator": "./scripts/generate-settings-schema.sh",
"build": "node --max_old_space_size=8000 ../../node_modules/@angular/cli/bin/ng build --aot --stats-json && yarn sentry:sourcemaps:inject",
"lint": "ng lint",
"new:component": "ng g component modules/altair/components/",
"ng": "ng",
"postinstall": "yarn generate-settings-schema-validator",
"prepare": "yarn generate-settings-schema-validator && node --max_old_space_size=8000 ../../node_modules/@angular/cli/bin/ng build --output-hashing=none --aot --stats-json && yarn sentry:sourcemaps:inject",
"prepare": "node --max_old_space_size=8000 ../../node_modules/@angular/cli/bin/ng build --output-hashing=none --aot --stats-json && yarn sentry:sourcemaps:inject",
"start": "ng serve",
"sentry:sourcemaps:inject": "sentry-cli sourcemaps inject --org altair-graphql --project electron ./dist",
"test": "jest && ng lint",
Expand Down
4 changes: 0 additions & 4 deletions packages/altair-app/scripts/generate-settings-schema.sh

This file was deleted.

2 changes: 2 additions & 0 deletions packages/altair-app/src/app/modules/altair/altair.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import { ReducerBootstrapper } from './store/reducer-bootstrapper';
import { RootState } from 'altair-graphql-core/build/types/state/state.interfaces';
import { AccountEffects } from './effects/account.effect';
import { WorkspaceEffects } from './effects/workspace.effect';
import { ElectronEffects } from './effects/electron.effect';

registerLocaleData(en);

Expand Down Expand Up @@ -147,6 +148,7 @@ const providers = [
LocalEffects,
AccountEffects,
WorkspaceEffects,
ElectronEffects,
]),
// StoreDevtoolsModule.instrument({
// logOnly: environment.production,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import settingsSchema from '../../utils/settings.schema.json';
import settingsSchema from 'altair-graphql-core/build/settings.schema.json';
import { JSONSchema7 } from 'json-schema';
import { jsonSchema } from 'codemirror-json-schema';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { KeyboardShortcutCategory } from '../../services/keybinder/keybinder.ser
import { SettingsState } from 'altair-graphql-core/build/types/state/settings.interfaces';
import { AltairConfig } from 'altair-graphql-core/build/config';
import { Extension } from '@codemirror/state';
import settingsSchema from '../../utils/settings.schema.json';
import settingsSchema from 'altair-graphql-core/build/settings.schema.json';
import { getEditorExtensions } from './extensions';
import { IDictionary } from 'altair-graphql-core/build/types/shared';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Injectable } from '@angular/core';
import { createEffect, Actions, ofType } from '@ngrx/effects';
import { Store } from '@ngrx/store';
import { RootState } from 'altair-graphql-core/build/types/state/state.interfaces';
import { of } from 'rxjs';
import { tap, withLatestFrom } from 'rxjs/operators';
import { ElectronAppService } from '../services';

import * as variablesActions from '../store/variables/variables.action';
import * as settingsActions from '../store/settings/settings.action';

@Injectable()
export class ElectronEffects {
updateSettingsOnFile$ = createEffect(
() => {
return this.actions$.pipe(
ofType(settingsActions.UPDATE_SETTINGS, settingsActions.SET_SETTINGS_JSON),
withLatestFrom(
this.store,
(action: variablesActions.UpdateFileVariableDataAction, state) => {
return {
state,
};
}
),
tap(({ state }) => {
return of(this.electronAppService.updateSettingsOnFile(state.settings));
})
);
},
{ dispatch: false }
);

constructor(
private actions$: Actions,
private store: Store<RootState>,
private electronAppService: ElectronAppService
) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { IDictionary } from 'altair-graphql-core/build/types/shared';
import { IQueryCollection } from 'altair-graphql-core/build/types/state/collection.interfaces';
import { electronAPI } from '@altairgraphql/electron-interop/build/renderer';
import { environment } from 'environments/environment';
import { SettingsState } from 'altair-graphql-core/build/types/state/settings.interfaces';

interface ConnectOptions {
importFileContent: (content: string) => void;
Expand Down Expand Up @@ -156,9 +157,7 @@ export class ElectronAppService {
this.api.events.onReloadDocs(() => {
this.zone.run(() =>
this.store.dispatch(
new queryActions.SendIntrospectionQueryRequestAction(
this.activeWindowId
)
new queryActions.SendIntrospectionQueryRequestAction(this.activeWindowId)
)
);
});
Expand Down Expand Up @@ -343,4 +342,12 @@ export class ElectronAppService {
fileType: 'agbkp',
});
}

getSettingsFromFile() {
return this.api?.actions.getAltairAppSettingsFromFile();
}

updateSettingsOnFile(settings: SettingsState) {
return this.api?.actions.updateAltairAppSettingsOnFile(settings);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ export class ReducerBootstrapper {
updateFromLocalStorage: true,
});

// Merge electron setting state with initial setting state if available
const settingsFromFile = await this.electronAppService.getSettingsFromFile();
if (settingsFromFile) {
this.initialState = {
...this.initialState,
settings: {
...this.initialState?.settings,
...settingsFromFile,
},
};
}

// try to import backup if no initial state
if (!this.initialState) {
if (await this.electronAppService.importAutobackupData()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { AllActions } from '../action';

export const getInitialState = (): SettingsState => {
const altairConfig = getAltairConfig();
const initialSettings = altairConfig.initialData.settings || {};
const initialSettings = altairConfig.initialData.settings ?? {};
return {
theme: altairConfig.defaultTheme,
language: <SettingsLanguage>altairConfig.default_language,
Expand All @@ -23,17 +23,29 @@ export function settingsReducer(
state = getInitialState(),
action: AllActions
): SettingsState {
const persistedSettings = getAltairConfig().initialData.persistedSettings ?? {};
switch (action.type) {
case settings.SET_SETTINGS_JSON: {
const newState = { ...getInitialState(), ...jsonc(action.payload.value) };
const newState = {
...getInitialState(),
...jsonc(action.payload.value),
...persistedSettings, // apply persisted settings last
};

return newState;
}
case settings.UPDATE_SETTINGS: {
const newState = { ...state, ...action.payload };
const newState = {
...state,
...action.payload,
...persistedSettings, // apply persisted settings last
};
return newState;
}
default:
return state;
return {
...state,
...persistedSettings, // apply persisted settings last
};
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { settingsReducer } from './settings.reducer';
import { SET_SETTINGS_JSON, SetSettingsJsonAction } from './settings.action';
import { AltairConfig } from 'altair-graphql-core/build/config';

let mockAltairConfig = {
initialData: {},
Expand All @@ -10,6 +11,8 @@ let mockAltairConfig = {
};
jest.mock('altair-graphql-core/build/config', () => {
return {
AltairConfig: jest.requireActual('altair-graphql-core/build/config')
.AltairConfig,
getAltairConfig() {
return mockAltairConfig;
},
Expand Down Expand Up @@ -71,6 +74,32 @@ describe('settings', () => {
});
});

it('should set persistent settings after user provided settings', () => {
mockAltairConfig = new AltairConfig({
initialSettings: {
theme: 'dark',
disablePushNotification: true,
},
persistedSettings: {
theme: 'light',
},
});
const initialState = settingsReducer(undefined, {
type: 'UNKNOWN_ACTION',
} as any);
const newState = settingsReducer(
initialState,
new SetSettingsJsonAction({ value: JSON.stringify({ theme: 'changed' }) })
);
expect(newState).toEqual({
theme: 'light',
disablePushNotification: true,
language: 'en-US',
addQueryDepthLimit: 3,
tabSize: 2,
});
});

it(`should set settings data from provided JSON string for [${SET_SETTINGS_JSON}] action`, () => {
const newState = settingsReducer(
undefined,
Expand Down
Loading
Loading