From b1df3e1d5467ac74c656c8b78621b39fbc9514fc Mon Sep 17 00:00:00 2001 From: Harsh Shah Date: Tue, 4 Apr 2023 15:20:06 +0530 Subject: [PATCH] Feat : Save and import side settings There were requests from multiple users that they use squoosh for compression but for each iteration side settings resets to default causing issues and there is no way to save and import side settings. There will be two buttons adjacent to copy-over save side settings : This will save side encoder and latest settings to localstorage of browser import side settings : This will import side encoder and latest settings from localstorage of browser and replace the existing settings Also if there are saved settings in locaStorage then whenever user loads the app it will take that settings and populate the side so user do not have to repeatedly enter same settings for similar compression operation subject to user has saved side settings Update:1 Import settings button remains disabled if there is nothing to import Whenever the side setting is saved there will be event fired and eventually listened to enable import button All 2 operations show notifications now Import notification has undo option --- .../lazy-app/Compress/Options/index.tsx | 63 +++++++- .../lazy-app/Compress/Options/style.css | 17 +++ src/client/lazy-app/Compress/index.tsx | 138 ++++++++++++++++-- src/client/lazy-app/icons/index.tsx | 24 +++ 4 files changed, 225 insertions(+), 17 deletions(-) diff --git a/src/client/lazy-app/Compress/Options/index.tsx b/src/client/lazy-app/Compress/Options/index.tsx index 6e437506..0130865f 100644 --- a/src/client/lazy-app/Compress/Options/index.tsx +++ b/src/client/lazy-app/Compress/Options/index.tsx @@ -17,7 +17,7 @@ import Toggle from './Toggle'; import Select from './Select'; import { Options as QuantOptionsComponent } from 'features/processors/quantize/client'; import { Options as ResizeOptionsComponent } from 'features/processors/resize/client'; -import { SwapIcon } from 'client/lazy-app/icons'; +import { ImportIcon, SaveIcon, SwapIcon } from 'client/lazy-app/icons'; interface Props { index: 0 | 1; @@ -29,10 +29,14 @@ interface Props { onEncoderOptionsChange(index: 0 | 1, newOptions: EncoderOptions): void; onProcessorOptionsChange(index: 0 | 1, newOptions: ProcessorState): void; onCopyToOtherSideClick(index: 0 | 1): void; + onSaveSideSettingsClick(index: 0 | 1): void; + onImportSideSettingsClick(index: 0 | 1): void; } interface State { supportedEncoderMap?: PartialButNotUndefined; + leftSideSettings?: string | null; + rightSideSettings?: string | null; } type PartialButNotUndefined = { @@ -60,6 +64,8 @@ const supportedEncoderMapP: Promise> = export default class Options extends Component { state: State = { supportedEncoderMap: undefined, + leftSideSettings: localStorage.getItem('leftSideSettings'), + rightSideSettings: localStorage.getItem('rightSideSettings'), }; constructor() { @@ -69,6 +75,29 @@ export default class Options extends Component { ); } + private setLeftSideSettings = () => { + this.setState({ + leftSideSettings: localStorage.getItem('leftSideSettings'), + }); + }; + + private setRightSideSettings = () => { + this.setState({ + rightSideSettings: localStorage.getItem('rightSideSettings'), + }); + }; + + componentDidMount(): void { + // Changing the state when side setting is stored in localstorage + window.addEventListener('leftSideSettings', this.setLeftSideSettings); + window.addEventListener('rightSideSettings', this.setRightSideSettings); + } + + componentWillUnmount(): void { + window.removeEventListener('leftSideSettings', this.setLeftSideSettings); + window.removeEventListener('removeSideSettings', this.setRightSideSettings); + } + private onEncoderTypeChange = (event: Event) => { const el = event.currentTarget as HTMLSelectElement; @@ -110,6 +139,14 @@ export default class Options extends Component { this.props.onCopyToOtherSideClick(this.props.index); }; + private onSaveSideSettingClick = () => { + this.props.onSaveSideSettingsClick(this.props.index); + }; + + private onImportSideSettingsClick = () => { + this.props.onImportSideSettingsClick(this.props.index); + }; + render( { source, encoderState, processorState }: Props, { supportedEncoderMap }: State, @@ -139,6 +176,30 @@ export default class Options extends Component { > + +