improve hash handling

This commit is contained in:
Eric Bailey
2023-09-25 16:32:02 -05:00
parent ad9672ef7f
commit 5c3416d4f8
+18 -8
View File
@@ -16,6 +16,19 @@ import {
import {Text} from 'view/com/util/text/Text' import {Text} from 'view/com/util/text/Text'
import {usePalette} from 'lib/hooks/usePalette' import {usePalette} from 'lib/hooks/usePalette'
function uniq(tags: string[]) {
return Array.from(new Set(tags))
}
function sanitize(tagString: string) {
return tagString
.trim()
.split(' ')
.filter(Boolean)
.map(t => t.trim())
.map(t => t.replace(/^#/, ''))
}
function Tag({ function Tag({
tag, tag,
removeTag, removeTag,
@@ -62,12 +75,10 @@ export function TagInput({
const onKeyPress = React.useCallback( const onKeyPress = React.useCallback(
(e: NativeSyntheticEvent<TextInputKeyPressEventData>) => { (e: NativeSyntheticEvent<TextInputKeyPressEventData>) => {
if (e.nativeEvent.key === 'Enter' || e.nativeEvent.key === ' ') { if (e.nativeEvent.key === 'Enter' || e.nativeEvent.key === ' ') {
const _tags = value.trim().split(' ').filter(Boolean) const _tags = sanitize(value)
if (_tags.length > 0) { if (_tags.length > 0) {
handleChangeTags( handleChangeTags(uniq([...tags, ..._tags]).slice(0, max))
Array.from(new Set([...tags, ..._tags])).slice(0, max),
)
} }
InteractionManager.runAfterInteractions(() => { InteractionManager.runAfterInteractions(() => {
@@ -82,15 +93,14 @@ export function TagInput({
) )
const onChangeText = React.useCallback((value: string) => { const onChangeText = React.useCallback((value: string) => {
const sanitized = value.replace(/^#/, '') setValue(value)
setValue(sanitized)
}, []) }, [])
const onBlur = React.useCallback(() => { const onBlur = React.useCallback(() => {
const _tags = value.trim().split(' ').filter(Boolean) const _tags = sanitize(value)
if (_tags.length > 0) { if (_tags.length > 0) {
handleChangeTags(Array.from(new Set([...tags, ..._tags])).slice(0, max)) handleChangeTags(uniq([...tags, ..._tags]).slice(0, max))
setValue('') setValue('')
} }
}, [value, tags, max, handleChangeTags]) }, [value, tags, max, handleChangeTags])