Repository: marktext/marktext
File: src/main/dataCenter/index.js (JavaScript)

Code before:
   */
  setItems (settings) {
    if (!settings) {
      log.error('Cannot change settings without entires: object is undefined or null.')
      return
    }

    Object.keys(settings).forEach(key => {
      this.setItem(key, settings[key])
    })
  }

  _listenForIpcMain () {
    // local main events
    ipcMain.on('set-image-folder-path', newPath => {
      this.setItem('imageFolderPath', newPath)
    })

    // events from renderer process
    ipcMain.on('mt::ask-for-user-data', async e => {
      const win = BrowserWindow.fromWebContents(e.sender)
      const userData = await this.getAll()
      win.webContents.send('mt::user-preference', userData)
    })

    ipcMain.on('mt::ask-for-modify-image-folder-path', async (e, filePaths) => {
      if (!filePaths[0]) {
        const win = BrowserWindow.fromWebContents(e.sender)
        filePaths = (await dialog.showOpenDialog(win, {
          properties: ['openDirectory', 'createDirectory']
        })).filePaths
      }
      if (filePaths && filePaths[0]) {
        this.setItem('imageFolderPath', filePaths[0])
      }
    })

    ipcMain.on('mt::set-user-data', (e, userData) => {
      this.setItems(userData)
    })

    ipcMain.on('mt::ask-for-image-path', async e => {
      const win = BrowserWindow.fromWebContents(e.sender)
      const { filePaths } = await dialog.showOpenDialog(win, {
        properties: ['openFile'],
        filters: [{
          name: 'Images',
          extensions: IMAGE_EXTENSIONS
        }]
      })


Review comment (suggestion):
This event handler should receive a string `filePath` and not an array. The caller shouldn't be affected by the internal function logic. If not present call `showOpenDialog`, check the result and set `null` or the given string. You can also rename the variable `filePaths` to `imagePath` to improve readability.

Code after:
   */
  setItems (settings) {
    if (!settings) {
      log.error('Cannot change settings without entires: object is undefined or null.')
      return
    }

    Object.keys(settings).forEach(key => {
      this.setItem(key, settings[key])
    })
  }

  _listenForIpcMain () {
    // local main events
    ipcMain.on('set-image-folder-path', newPath => {
      this.setItem('imageFolderPath', newPath)
    })

    // events from renderer process
    ipcMain.on('mt::ask-for-user-data', async e => {
      const win = BrowserWindow.fromWebContents(e.sender)
      const userData = await this.getAll()
      win.webContents.send('mt::user-preference', userData)
    })

    ipcMain.on('mt::ask-for-modify-image-folder-path', async (e, imagePath) => {
      if (!imagePath) {
        const win = BrowserWindow.fromWebContents(e.sender)
        imagePath = (await dialog.showOpenDialog(win, {
          properties: ['openDirectory', 'createDirectory']
        })).filePaths[0]
      }
      if (imagePath) {
        this.setItem('imageFolderPath', imagePath)
      }
    })

    ipcMain.on('mt::set-user-data', (e, userData) => {
      this.setItems(userData)
    })

    ipcMain.on('mt::ask-for-image-path', async e => {
      const win = BrowserWindow.fromWebContents(e.sender)
      const { filePaths } = await dialog.showOpenDialog(win, {
        properties: ['openFile'],
        filters: [{
          name: 'Images',
          extensions: IMAGE_EXTENSIONS
        }]
      })