Untitled

mail@pastecode.io avatar
unknown
javascript
a year ago
1.9 kB
2
Indexable
Never
try {
    if (mainWindow) {
      let title;
      let defaultPath;
      let filters;
      // const savePath = dialog.showSaveDialogSync(mainWindow, {
      //   title: 'Save Exported CSV',
      //   defaultPath: 'pCloudPassExports.csv',
      //   filters: [{ name: 'CSV Files', extensions: ['csv'] }],
      // });

      switch (args[1]) {
        case 'csv':
          title = 'Save Exported CSV';
          defaultPath = 'pCloudPassExports.csv';
          filters = [{ name: 'CSV Files', extensions: ['csv'] }];
          break;

        case 'txt':
          title = 'Save Exported TXT';
          defaultPath = 'pCloudPassExports.txt';
          filters = [{ name: 'Text Files', extensions: ['txt'] }];
          break;

        default:
          console.error(`Unsupported file type: ${args[1]}`);
          event.reply('file-saved', {
            status: 'error',
            message: `Unsupported file type: ${args[1]}`,
          });
          return; // exit out of this function since the file type is unsupported
      }

      const savePath = dialog.showSaveDialogSync(mainWindow, {
        title,
        defaultPath,
        filters,
      });

      if (savePath) {
        fs.writeFileSync(savePath, args[0]);
        event.reply('file-saved', {
          status: 'success',
          message: 'File saved successfully.',
        });
      } else {
        event.reply('file-saved', {
          status: 'error',
          message: 'Save dialog was closed without selecting a path.',
        });
      }
    }
  } catch (error) {
    if (error instanceof Error) {
      console.error('Error during the file save operation: ', error);
      event.reply('file-saved', {
        status: 'error',
        message: `Error writing to the specified file path: ${error.message}`,
      });
    }
  }