<commit_before>import storage from 'electron-json-storage'
import promisify from 'es6-promisify'
import defaultsDeep from 'lodash/defaultsDeep'
import omit from 'lodash/omit'

import { setError } from 'reducers/error'

const storageGet = promisify(storage.get)

export function loadSettings() {
  return async dispatch => {
    let shouldResetDefaults = false
    let res
    try {
      res = await storageGet('settings')
    } catch (e) {
      shouldResetDefaults = true
      dispatch(setError(e))
    }
    const settings = defaultsDeep(res, {
      lastOpenedFolder: null,
      editor: {
        wrapLines: true,
        autoFold: false,
        foldLevel: 1,
        highlightTag: false,
        lightTheme: false,
      },
      mjml: {
        minify: false,
        beautify: false,
      },
      projects: [],
      api: {
        Subject: 'MJML App test email',
        APIKey: '',
        APISecret: '',
        SenderName: '<NAME>',
        SenderEmail: '',
        TargetEmails: [],
        LastEmails: [],
      },
      previewSize: {
        current: 500,
        mobile: 320,
        desktop: 650,
      },
    })

    // clean old format for TargetEmails<commit_msg>fix #188 handle old settings format<commit_after>import storage from 'electron-json-storage'
import promisify from 'es6-promisify'
import defaultsDeep from 'lodash/defaultsDeep'
import omit from 'lodash/omit'

import { setError } from 'reducers/error'

const storageGet = promisify(storage.get)
const storageSet = promisify(storage.set)

export function loadSettings() {
  return async dispatch => {
    let shouldResetDefaults = false
    let res
    try {
      res = await storageGet('settings')

      // check for old format and reformat
      if ((typeof res.projects === 'object') && !(res.projects instanceof Array)) {
        res = res.projects
        await storageSet('settings', res)
      }
    } catch (e) {
      shouldResetDefaults = true
      dispatch(setError(e))
    }

    const settings = defaultsDeep(res, {
      lastOpenedFolder: null,
      editor: {
        wrapLines: true,
        autoFold: false,
        foldLevel: 1,
        highlightTag: false,
        lightTheme: false,
      },
      mjml: {
        minify: false,
        beautify: false,
      },
      projects: [],
      api: {
        Subject: 'MJML App test email',
        APIKey: '',
        APISecret: '',
        SenderName: 'MJML App',
        SenderEmail: '',
        TargetEmails: [],
        LastEmails: [],
      },
      previewSize: {
        current: 500,
        mobile: 320,
        desktop: 650,
      },
    })

    // clean old format for TargetEmails
{'fraction_non_alphanumeric': 0.10800604229607251, 'fraction_numerical': 0.00944108761329305, 'mean_word_length': 2.1573301549463646, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6682521', 'n_tokens_mistral': 810, 'n_tokens_neox': 766, 'n_words': 206}