From 261919f17a804233f3ec6e300f2c4e5f4bf5b1cd Mon Sep 17 00:00:00 2001 From: Mo Tarbin Date: Thu, 24 Apr 2025 21:34:07 -0400 Subject: [PATCH] refactor: update priority parsing to use '!' prefix and enhance highlight handling in CustomParsers --- src/views/TestView/SmartTaskTitleInput.jsx | 2 +- src/views/components/AddTaskModal.jsx | 34 +++++++++++++++++----- src/views/components/CustomParsers.js | 25 ++++++++++------ 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/src/views/TestView/SmartTaskTitleInput.jsx b/src/views/TestView/SmartTaskTitleInput.jsx index 320cd6d..f6440f7 100644 --- a/src/views/TestView/SmartTaskTitleInput.jsx +++ b/src/views/TestView/SmartTaskTitleInput.jsx @@ -85,7 +85,7 @@ const SmartTaskTitleInput = ({ if ( lastWord.startsWith('@') || lastWord.startsWith('#') || - lastWord.startsWith('P') + lastWord.startsWith('!') ) { setSuggestionTrigger(lastWord[0]) // last word without the first character: diff --git a/src/views/components/AddTaskModal.jsx b/src/views/components/AddTaskModal.jsx index 1948329..c922f63 100644 --- a/src/views/components/AddTaskModal.jsx +++ b/src/views/components/AddTaskModal.jsx @@ -161,7 +161,7 @@ const TaskInput = ({ autoFocus, onChoreUpdate, isModalOpen, onClose }) => { setTaskText(sentence) setTaskTitle(cleanedSentence.trim()) - const rendered = renderText( + const { rendered, plainText } = renderHighlightedSentence( sentence, repeat.highlight, priority.highlight, @@ -176,8 +176,9 @@ const TaskInput = ({ autoFocus, onChoreUpdate, isModalOpen, onClose }) => { ) setRenderedParts(rendered) + setTaskTitle(plainText) } - const renderText = ( + const renderHighlightedSentence = ( sentence, repeatHighlight, priorityHighlight, @@ -186,6 +187,7 @@ const TaskInput = ({ autoFocus, onChoreUpdate, isModalOpen, onClose }) => { ) => { const parts = [] let lastIndex = 0 + let plainText = '' // Combine all highlight ranges and sort them by their start index const allHighlights = [] @@ -224,10 +226,13 @@ const TaskInput = ({ autoFocus, onChoreUpdate, isModalOpen, onClose }) => { resolvedHighlights.push(current) } } + for (const highlight of resolvedHighlights) { // Add the text before the highlight if (highlight.start > lastIndex) { - parts.push(sentence.substring(lastIndex, highlight.start)) + const textBefore = sentence.substring(lastIndex, highlight.start) + parts.push(textBefore) + plainText += textBefore } // Determine the class name based on the highlight type @@ -250,6 +255,7 @@ const TaskInput = ({ autoFocus, onChoreUpdate, isModalOpen, onClose }) => { } // Add the highlighted span + const highlightedText = sentence.substring(highlight.start, highlight.end) parts.push( { textDecorationStyle: 'dashed', }} > - {sentence.substring(highlight.start, highlight.end)} + {highlightedText} , ) @@ -272,10 +278,15 @@ const TaskInput = ({ autoFocus, onChoreUpdate, isModalOpen, onClose }) => { // Add any remaining text after the last highlight if (lastIndex < sentence.length) { - parts.push(sentence.substring(lastIndex)) + const remainingText = sentence.substring(lastIndex) + parts.push(remainingText) + plainText += remainingText } - return parts + return { + parts, + plainText, + } } const createChore = () => { const chore = { @@ -415,7 +426,16 @@ const TaskInput = ({ autoFocus, onChoreUpdate, isModalOpen, onClose }) => { display: 'name', options: userLabels ? userLabels : [], }, - '!': ['P1', 'P2', 'P3', 'P4'], + '!': { + value: 'id', + display: 'name', + options: [ + { id: '1', name: 'P1' }, + { id: '2', name: 'P2' }, + { id: '3', name: 'P3' }, + { id: '4', name: 'P4' }, + ], + }, '@': { // value: 'userId', // display: 'displayName', diff --git a/src/views/components/CustomParsers.js b/src/views/components/CustomParsers.js index 7161849..632ea59 100644 --- a/src/views/components/CustomParsers.js +++ b/src/views/components/CustomParsers.js @@ -48,10 +48,10 @@ const ALL_MONTHS = Object.values(VALID_MONTHS).filter( export const parsePriority = inputSentence => { let sentence = inputSentence.toLowerCase() const priorityMap = { - 1: ['p1', 'priority 1', 'high priority', 'urgent', 'asap', 'important'], - 2: ['p2', 'priority 2', 'medium priority'], - 3: ['p3', 'priority 3', 'low priority'], - 4: ['p4', 'priority 4'], + 1: ['!p1', 'priority 1', 'high priority', 'urgent', 'asap', 'important'], + 2: ['!p2', 'priority 2', 'medium priority'], + 3: ['!p3', 'priority 3', 'low priority'], + 4: ['!p4', 'priority 4'], } for (const [priority, terms] of Object.entries(priorityMap)) { @@ -69,14 +69,18 @@ export const parsePriority = inputSentence => { }) .filter(term => term.start !== -1), - cleanedSentence: terms.reduce( - (s, t) => s.replace(t, ''), - inputSentence, + cleanedSentence: sentence.replace( + new RegExp(`(${terms.join('|')})`, 'g'), + '', ), } } } - return { result: 0, cleanedSentence: inputSentence } + return { + result: 0, + highlight: [], + cleanedSentence: inputSentence, + } } export const parseLabels = (inputSentence, userLabels) => { let sentence = inputSentence.toLowerCase() @@ -103,7 +107,10 @@ export const parseLabels = (inputSentence, userLabels) => { } }), - cleanedSentence: sentence, + cleanedSentence: sentence.replace( + new RegExp(`#(${userLabels.map(l => l.name).join('|')})`, 'g'), + '', + ), } } return { result: null, cleanedSentence: sentence }