refactor: update priority parsing to use '!' prefix and enhance highlight handling in CustomParsers

This commit is contained in:
Mo Tarbin
2025-04-24 21:34:07 -04:00
parent 4206cbd557
commit 261919f17a
3 changed files with 44 additions and 17 deletions

View File

@@ -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:

View File

@@ -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(
<span
key={highlight.start}
@@ -262,7 +268,7 @@ const TaskInput = ({ autoFocus, onChoreUpdate, isModalOpen, onClose }) => {
textDecorationStyle: 'dashed',
}}
>
{sentence.substring(highlight.start, highlight.end)}
{highlightedText}
</span>,
)
@@ -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',

View File

@@ -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 }