Skip to content

Commit

Permalink
Merge pull request #16 from sereneinserenade/14-bug-export-pinned-not…
Browse files Browse the repository at this point in the history
…es-while-importexport-fromto-a-file

fix: take `pinnedNoteIds` in consideration while import/export
  • Loading branch information
sereneinserenade authored Jun 9, 2022
2 parents b76a5d9 + 153bd20 commit b049b4a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
23 changes: 14 additions & 9 deletions src/pages/Newtab/components/MainTop.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import React, { useCallback, useState } from 'react'
import { Button, Tooltip, FormElement, Input, useTheme, changeTheme, Switch } from '@nextui-org/react'
import { RiAddLine, RiMenuFoldFill, RiMenuUnfoldFill, RiMoonFill, RiPrinterLine, RiSunFill } from 'react-icons/ri'

import { Button, changeTheme, Tooltip, useTheme } from '@nextui-org/react'
import { saveAs } from 'file-saver'
import { BiExport, BiImport } from 'react-icons/bi'
import { FiHome } from 'react-icons/fi'
import { BiImport, BiExport } from 'react-icons/bi'
import { RiAddLine, RiMenuFoldFill, RiMenuUnfoldFill, RiMoonFill, RiPrinterLine, RiSunFill } from 'react-icons/ri'
import { useRecoilState, useRecoilValue } from 'recoil'
import { v4 as uuidv4 } from 'uuid'
import { saveAs } from 'file-saver';

import './css/MainTop.scss'
import { ImportDataModal } from './data'
import { activeNoteState, binNotesState, notesState, sidebarActiveState } from '../Store'
import { Note, QuickLink } from '../types'
import { useRecoilState, useRecoilValue } from 'recoil'
import { ImportDataModal } from './data'

import './css/MainTop.scss'

const { storage } = chrome

Expand Down Expand Up @@ -89,18 +91,21 @@ const MainTop = () => {
binNotes: Note[]
quicklinks: QuickLink[]
quicklinksorder: string[]
pinnedNoteIds: string[]
}

const data: Data = {
dbnotes: notes,
binNotes,
quicklinks: [],
quicklinksorder: []
quicklinksorder: [],
pinnedNoteIds: [],
}

storage.local.get(['quicklinks', 'quicklinksorder'], ({ quicklinks, quicklinksorder }) => {
storage.local.get(['quicklinks', 'quicklinksorder', 'pinnedNoteIds'], ({ quicklinks, quicklinksorder, pinnedNoteIds }) => {
data.quicklinks = quicklinks || []
data.quicklinksorder = quicklinksorder || []
data.pinnedNoteIds = pinnedNoteIds

const fileToSave = new Blob([JSON.stringify(data)], { type: 'application/json' })

Expand Down
40 changes: 30 additions & 10 deletions src/pages/Newtab/components/data/ImportDataModal.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React, { useEffect, useRef, useState } from 'react';

import { Modal, Text, Button, Loading } from '@nextui-org/react';
import { useRecoilValue } from 'recoil';
import { Button, Loading, Modal, Text } from '@nextui-org/react';
import { isPlainObject } from 'lodash';
import { useRecoilValue } from 'recoil';

import { Note, QuickLink } from '../../types';
import { binNotesState, notesState } from '../../Store';
import './css/ImportDataModel.scss'
import { Note, QuickLink } from '../../types';

import './css/ImportDataModel.scss';

interface ImportDataModalProps {
isImportDataModelOpen: boolean
Expand All @@ -18,6 +19,7 @@ interface Data {
binNotes: Note[]
quicklinks: QuickLink[]
quicklinksorder: string[]
pinnedNoteIds: string[]
}

const { storage } = chrome
Expand All @@ -43,12 +45,30 @@ export const ImportDataModal: React.FC<ImportDataModalProps> = ({ isImportDataMo

try {
const json: Data = JSON.parse(jsonDataString)
const { dbnotes, binNotes, quicklinks, quicklinksorder } = json

const [isNotesValid, isBinNotesValid, isQuicklinksValid, isQuicklinksOrderValid] = [Array.isArray(dbnotes), Array.isArray(binNotes), isPlainObject(quicklinks), Array.isArray(quicklinksorder)]

if (!isNotesValid || !isBinNotesValid || !isQuicklinksOrderValid || !isQuicklinksValid) {
const err = `${!isNotesValid ? 'notes, ' : ""}${!isBinNotesValid ? 'binNotes, ' : ""}${!isQuicklinksValid ? 'quicklinks, ' : ""}${!isQuicklinksOrderValid ? 'quicklinksorder' : ""} not valid.`
const { dbnotes, binNotes, quicklinks, quicklinksorder, pinnedNoteIds } = json

const [
isNotesValid,
isBinNotesValid,
isQuicklinksValid,
isQuicklinksOrderValid,
isPinnedNoteIdsValid
] = [
Array.isArray(dbnotes),
Array.isArray(binNotes),
isPlainObject(quicklinks),
Array.isArray(quicklinksorder),
Array.isArray(pinnedNoteIds || [])
]

if (
!isNotesValid
|| !isBinNotesValid
|| !isQuicklinksOrderValid
|| !isQuicklinksValid
|| !isPinnedNoteIdsValid
) {
const err = `${!isNotesValid ? 'notes, ' : ""}${!isBinNotesValid ? 'binNotes, ' : ""}${!isQuicklinksValid ? 'quicklinks, ' : ""}${!isQuicklinksOrderValid ? 'quicklinksorder, ' : ""}${!isPinnedNoteIdsValid ? 'pinnedNoteIds, ' : ''} not valid.`
console.error(err)
setIsJsonValid(false)
return
Expand Down

0 comments on commit b049b4a

Please sign in to comment.