From ac2e973cb0f289e1367f1a14388d35da79c9a378 Mon Sep 17 00:00:00 2001 From: Lorenzo Alberto Maria Ambrosi Date: Thu, 20 Feb 2020 15:38:10 +0100 Subject: [PATCH] Add generic error's message Change-type: patch Changelog-entry: Add generic error's message Signed-off-by: Lorenzo Alberto Maria Ambrosi --- .../source-selector/source-selector.tsx | 130 +++++++++-------- lib/gui/app/modules/image-writer.ts | 2 +- lib/gui/app/pages/main/Flash.tsx | 8 +- lib/gui/modules/child-writer.ts | 1 - lib/shared/messages.ts | 4 +- npm-shrinkwrap.json | 132 +----------------- package.json | 2 - 7 files changed, 76 insertions(+), 203 deletions(-) diff --git a/lib/gui/app/components/source-selector/source-selector.tsx b/lib/gui/app/components/source-selector/source-selector.tsx index e0d0e7b9..571ec283 100644 --- a/lib/gui/app/components/source-selector/source-selector.tsx +++ b/lib/gui/app/components/source-selector/source-selector.tsx @@ -16,7 +16,6 @@ import { faFile, faLink } from '@fortawesome/free-solid-svg-icons'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; -import * as jsonStorageCb from 'electron-json-storage'; import { sourceDestination } from 'etcher-sdk'; import * as _ from 'lodash'; import { GPTPartition, MBRPartition } from 'partitioninfo'; @@ -46,35 +45,36 @@ import { colors } from '../../theme'; import { middleEllipsis } from '../../utils/middle-ellipsis'; import { SVGIcon } from '../svg-icon/svg-icon'; -const jsonStorage = { - get: (key: string) => { - return new Promise((resolve, reject) => { - jsonStorageCb.get(key, (err, value) => { - if (err) { - reject(err); - throw err; - } - resolve(value); - return value; - }); - }); - }, - set: (key: string, value: object) => { - return new Promise((resolve, reject) => { - jsonStorageCb.set(key, value, err => { - if (err) { - reject(err); - throw err; - } - resolve(value); - return value; - }); - }); - }, -}; +const recentUrlImagesKey = 'recentUrlImages'; -const getRecentUrlImages = () => - jsonStorage.get('recentUrlImages') as Promise; +function normalizeRecentUrlImages(urls: any[]): string[] { + if (!Array.isArray(urls)) { + urls = []; + } + return _.chain(urls) + .filter(_.isString) + .reject(_.isEmpty) + .uniq() + .takeRight(5) + .value(); +} + +function getRecentUrlImages(): string[] { + let urls = []; + try { + urls = JSON.parse(localStorage.getItem(recentUrlImagesKey) || '[]'); + } catch { + // noop + } + return normalizeRecentUrlImages(urls); +} + +function setRecentUrlImages(urls: string[]) { + localStorage.setItem( + recentUrlImagesKey, + JSON.stringify(normalizeRecentUrlImages(urls)), + ); +} const Card = styled(BaseCard)` hr { @@ -107,32 +107,27 @@ const URLSelector = ({ done }: { done: (imageURL: string) => void }) => { string[], (value: React.SetStateAction) => void, ] = React.useState([]); + const [loading, setLoading] = React.useState(false); React.useEffect(() => { const fetchRecentUrlImages = async () => { - try { - const recentUrlImages: string[] = await getRecentUrlImages(); - if (!Array.isArray(recentUrlImages)) { - setRecentImages([]); - } else { - setRecentImages(recentUrlImages); - } - } catch (err) { - console.error(err); - } + const recentUrlImages: string[] = await getRecentUrlImages(); + setRecentImages(recentUrlImages); }; fetchRecentUrlImages(); }, []); return ( { - const sanitizedRecentUrls = _.uniq( - _.reject([...recentImages, imageURL], _.isEmpty), - ); - await jsonStorage.set( - 'recentUrlImages', - _.takeRight(sanitizedRecentUrls, 5), - ); - done(imageURL); + setLoading(true); + const sanitizedRecentUrls = normalizeRecentUrlImages([ + ...recentImages, + imageURL, + ]); + setRecentUrlImages(sanitizedRecentUrls); + await done(imageURL); }} >