Skip to content

Commit

Permalink
feat: Add Mentions to draft messages #WPB-12062 (#3687)
Browse files Browse the repository at this point in the history
Co-authored-by: Yamil Medina <[email protected]>
  • Loading branch information
borichellow and yamilmedina authored Dec 13, 2024
1 parent e6d8091 commit 9e75446
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,6 @@ fun rememberMessageComposerStateHolder(

val messageTextFieldValue = remember { mutableStateOf(TextFieldValue()) }

LaunchedEffect(draftMessageComposition.draftText) {
if (draftMessageComposition.draftText.isNotBlank()) {
messageTextFieldValue.value = messageTextFieldValue.value.copy(
text = draftMessageComposition.draftText,
selection = TextRange(draftMessageComposition.draftText.length) // Place cursor at the end of the new text
)
}
}

val messageCompositionHolder = remember {
mutableStateOf(
MessageCompositionHolder(
Expand All @@ -77,6 +68,23 @@ fun rememberMessageComposerStateHolder(
)
)
}

LaunchedEffect(draftMessageComposition.draftText) {
if (draftMessageComposition.draftText.isNotBlank()) {
messageTextFieldValue.value = messageTextFieldValue.value.copy(
text = draftMessageComposition.draftText,
selection = TextRange(draftMessageComposition.draftText.length) // Place cursor at the end of the new text
)
}

if (draftMessageComposition.selectedMentions.isNotEmpty()) {
messageCompositionHolder.value.setMentions(
draftMessageComposition.draftText,
draftMessageComposition.selectedMentions.map { it.intoMessageMention() }
)
}
}

LaunchedEffect(Unit) {
messageCompositionHolder.value.handleMessageTextUpdates()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,20 @@ class MessageCompositionHolder(
)
messageComposition.update {
it.copy(
selectedMentions = mentions.mapNotNull { it.toUiMention(editMessageText) },
selectedMentions = mentions.mapNotNull { mention -> mention.toUiMention(editMessageText) },
editMessageId = messageId
)
}
onSaveDraft(messageComposition.value.toDraft(editMessageText))
}

fun setMentions(editMessageText: String, mentions: List<MessageMention>) {
messageComposition.update {
it.copy(selectedMentions = mentions.mapNotNull { mention -> mention.toUiMention(editMessageText) })
}
onSaveDraft(messageComposition.value.toDraft(editMessageText))
}

fun addOrRemoveMessageMarkdown(
markdown: RichTextMarkdown,
) {
Expand Down

0 comments on commit 9e75446

Please sign in to comment.