2021-01-16 00:00:31 +01:00
|
|
|
import ComponentAccount from '@components/Account'
|
2021-01-07 19:13:09 +01:00
|
|
|
import haptics from '@components/haptics'
|
2021-01-19 01:13:45 +01:00
|
|
|
import ComponentHashtag from '@components/Hashtag'
|
2022-09-20 22:23:01 +02:00
|
|
|
import React, { useContext, useEffect } from 'react'
|
|
|
|
import ComposeContext from '../utils/createContext'
|
2022-12-07 23:56:00 +01:00
|
|
|
import { formatText } from '../utils/processText'
|
2022-09-20 22:23:01 +02:00
|
|
|
|
|
|
|
type Props = { item: Mastodon.Account & Mastodon.Tag }
|
|
|
|
|
|
|
|
const ComposeRootSuggestion: React.FC<Props> = ({ item }) => {
|
|
|
|
const { composeState, composeDispatch } = useContext(ComposeContext)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (composeState.text.raw.length === 0) {
|
|
|
|
composeDispatch({ type: 'tag', payload: undefined })
|
|
|
|
}
|
|
|
|
}, [composeState.text.raw.length])
|
|
|
|
|
|
|
|
const onPress = () => {
|
|
|
|
const focusedInput = composeState.textInputFocus.current
|
|
|
|
const updatedText = (): string => {
|
|
|
|
const main = item.acct ? `@${item.acct}` : `#${item.name}`
|
|
|
|
const textInput = composeState.textInputFocus.current
|
|
|
|
if (composeState.tag) {
|
|
|
|
const contentFront = composeState[textInput].raw.slice(0, composeState.tag.index)
|
|
|
|
const contentRear = composeState[textInput].raw.slice(composeState.tag.lastIndex)
|
|
|
|
|
|
|
|
const spaceFront =
|
|
|
|
composeState[textInput].raw.length === 0 || /\s/g.test(contentFront.slice(-1)) ? '' : ' '
|
|
|
|
const spaceRear = /\s/g.test(contentRear[0]) ? '' : ' '
|
|
|
|
|
|
|
|
return [contentFront, spaceFront, main, spaceRear, contentRear].join('')
|
|
|
|
} else {
|
|
|
|
return composeState[textInput].raw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
formatText({
|
|
|
|
textInput: focusedInput,
|
|
|
|
composeDispatch,
|
|
|
|
content: updatedText(),
|
|
|
|
disableDebounce: true
|
|
|
|
})
|
|
|
|
haptics('Light')
|
|
|
|
}
|
|
|
|
|
|
|
|
return item.acct ? (
|
2022-12-02 00:13:59 +01:00
|
|
|
<ComponentAccount account={item} props={{ onPress }} />
|
2022-09-20 22:23:01 +02:00
|
|
|
) : (
|
2022-12-03 23:10:20 +01:00
|
|
|
<ComponentHashtag hashtag={item} onPress={onPress} />
|
2022-09-20 22:23:01 +02:00
|
|
|
)
|
|
|
|
}
|
2021-01-07 19:13:09 +01:00
|
|
|
|
|
|
|
export default ComposeRootSuggestion
|