UNPKG

l7note

Version:

Access your notion notes quick

133 lines (120 loc) 2.94 kB
import prompts, { Choice, PromptObject } from 'prompts'; import { Note } from './Note.js'; import open from 'open'; import chalk from 'chalk'; import { markAsDone } from './noteActions/markAsDone.js'; import { markAsDeleteByNote } from './noteActions/markAsDelete.js'; import { exit } from 'process'; enum options { OPENCLI = 1, OPENBROWSER, EDIT, MARKDELETE, DELETE, } const onListCancel = () => { console.log('BYE'); exit(1); }; const listOptions: prompts.Options = { onCancel: onListCancel }; const displayInCli = (note: Note) => { console.log(note.url); console.log(chalk.green(note.title)); console.log(note.tags); console.log(); console.log(note.text); }; const GoBackPrompt = async () => { const goBacktoListQuestion: PromptObject[] = [ { type: 'select', name: 'goBack', message: '', choices: [{ title: 'Back' }, { title: 'Exit' }], }, ]; const res = await prompts(goBacktoListQuestion, listOptions); if (res.goBack) { console.log('BYE'); exit(1); } }; const displayNotes = async (noteList: Note[]) => { console.clear(); const choises = noteList.map(note => { const newChoise: Choice = { title: note.title, description: chalk.cyan(note.text.substring(0, 32)), value: note, }; return newChoise; }); const noteDisplayList: PromptObject[] = [ { type: 'select', name: 'selected', message: 'Selet the note you want to view:', choices: choises, }, ]; const res = await prompts(noteDisplayList, listOptions); const whatToToSelect: PromptObject[] = [ { type: 'select', name: 'whatToDo', message: 'What do you want to do with the selected note:', choices: [ { title: 'Open in cli', value: options.OPENCLI, }, { title: 'Open in browser', value: options.OPENBROWSER, }, { title: 'Edit note', value: options.EDIT, }, { title: 'Mark as done', value: options.MARKDELETE, }, { title: `${chalk.red('Delete note')}`, value: options.DELETE, }, ], }, ]; const whatToDoRes = await prompts(whatToToSelect, listOptions); switch (whatToDoRes.whatToDo) { case options.OPENBROWSER: open(res.selected.url); await GoBackPrompt(); displayNotes(noteList); break; case options.OPENCLI: displayInCli(res.selected); await GoBackPrompt(); displayNotes(noteList); break; case options.MARKDELETE: markAsDone(res.selected); await GoBackPrompt(); console.log(res.selected); console.log(noteList); displayNotes(noteList); await GoBackPrompt(); break; case options.DELETE: markAsDeleteByNote(res.selected); await GoBackPrompt(); noteList = noteList.filter(note => note.id !== res.selected.id); displayNotes(noteList); break; default: break; } }; export { displayNotes };