Add avatar to mobile autocomplete and create grapheme hook (#602)
* Add avatar to mobile autocomplete and create grapheme hook * Remove comment, update filename, cut out redundant logiczio/stable
parent
9a91b0c538
commit
8f6b5d3df9
|
@ -0,0 +1,36 @@
|
||||||
|
import Graphemer from 'graphemer'
|
||||||
|
import {useCallback, useMemo} from 'react'
|
||||||
|
|
||||||
|
export const useGrapheme = () => {
|
||||||
|
const splitter = useMemo(() => new Graphemer(), [])
|
||||||
|
|
||||||
|
const getGraphemeString = useCallback(
|
||||||
|
(name: string, length: number) => {
|
||||||
|
let remainingCharacters = 0
|
||||||
|
|
||||||
|
if (name.length > length) {
|
||||||
|
const graphemes = splitter.splitGraphemes(name)
|
||||||
|
|
||||||
|
if (graphemes.length > length) {
|
||||||
|
remainingCharacters = 0
|
||||||
|
name = `${graphemes.slice(0, length).join('')}...`
|
||||||
|
} else {
|
||||||
|
remainingCharacters = length - graphemes.length
|
||||||
|
name = graphemes.join('')
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
remainingCharacters = length - name.length
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
name,
|
||||||
|
remainingCharacters,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[splitter],
|
||||||
|
)
|
||||||
|
|
||||||
|
return {
|
||||||
|
getGraphemeString,
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,6 +5,8 @@ import {UserAutocompleteModel} from 'state/models/discovery/user-autocomplete'
|
||||||
import {useAnimatedValue} from 'lib/hooks/useAnimatedValue'
|
import {useAnimatedValue} from 'lib/hooks/useAnimatedValue'
|
||||||
import {usePalette} from 'lib/hooks/usePalette'
|
import {usePalette} from 'lib/hooks/usePalette'
|
||||||
import {Text} from 'view/com/util/text/Text'
|
import {Text} from 'view/com/util/text/Text'
|
||||||
|
import {UserAvatar} from 'view/com/util/UserAvatar'
|
||||||
|
import {useGrapheme} from '../hooks/useGrapheme'
|
||||||
|
|
||||||
export const Autocomplete = observer(
|
export const Autocomplete = observer(
|
||||||
({
|
({
|
||||||
|
@ -16,6 +18,7 @@ export const Autocomplete = observer(
|
||||||
}) => {
|
}) => {
|
||||||
const pal = usePalette('default')
|
const pal = usePalette('default')
|
||||||
const positionInterp = useAnimatedValue(0)
|
const positionInterp = useAnimatedValue(0)
|
||||||
|
const {getGraphemeString} = useGrapheme()
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
Animated.timing(positionInterp, {
|
Animated.timing(positionInterp, {
|
||||||
|
@ -35,58 +38,83 @@ export const Autocomplete = observer(
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Animated.View style={topAnimStyle}>
|
||||||
|
{view.isActive ? (
|
||||||
|
<View style={[pal.view, styles.container, pal.border]}>
|
||||||
|
{view.suggestions.length > 0 ? (
|
||||||
|
view.suggestions.slice(0, 5).map(item => {
|
||||||
|
// Eventually use an average length
|
||||||
|
const MAX_CHARS = 40
|
||||||
|
const MAX_HANDLE_CHARS = 20
|
||||||
|
|
||||||
|
// Using this approach because styling is not respecting
|
||||||
|
// bounding box wrapping (before converting to ellipsis)
|
||||||
|
const {name: displayHandle, remainingCharacters} =
|
||||||
|
getGraphemeString(item.handle, MAX_HANDLE_CHARS)
|
||||||
|
|
||||||
|
const {name: displayName} = getGraphemeString(
|
||||||
|
item.displayName ?? item.handle,
|
||||||
|
MAX_CHARS -
|
||||||
|
MAX_HANDLE_CHARS +
|
||||||
|
(remainingCharacters > 0 ? remainingCharacters : 0),
|
||||||
|
)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<View style={[styles.container, view.isActive && styles.visible]}>
|
|
||||||
<Animated.View
|
|
||||||
style={[
|
|
||||||
styles.animatedContainer,
|
|
||||||
pal.view,
|
|
||||||
pal.border,
|
|
||||||
topAnimStyle,
|
|
||||||
view.isActive && styles.visible,
|
|
||||||
]}>
|
|
||||||
{view.suggestions.slice(0, 5).map(item => (
|
|
||||||
<TouchableOpacity
|
<TouchableOpacity
|
||||||
testID="autocompleteButton"
|
testID="autocompleteButton"
|
||||||
key={item.handle}
|
key={item.handle}
|
||||||
style={[pal.border, styles.item]}
|
style={[pal.border, styles.item]}
|
||||||
onPress={() => onSelect(item.handle)}
|
onPress={() => onSelect(item.handle)}
|
||||||
accessibilityLabel={`Select ${item.handle}`}
|
accessibilityLabel={`Select ${item.handle}`}
|
||||||
accessibilityHint={`Autocompletes to ${item.handle}`}>
|
accessibilityHint="">
|
||||||
|
<View style={styles.avatarAndHandle}>
|
||||||
|
<UserAvatar avatar={item.avatar ?? null} size={24} />
|
||||||
<Text type="md-medium" style={pal.text}>
|
<Text type="md-medium" style={pal.text}>
|
||||||
{item.displayName || item.handle}
|
{displayName}
|
||||||
<Text type="sm" style={pal.textLight}>
|
|
||||||
@{item.handle}
|
|
||||||
</Text>
|
</Text>
|
||||||
|
</View>
|
||||||
|
<Text type="sm" style={pal.textLight} numberOfLines={1}>
|
||||||
|
@{displayHandle}
|
||||||
</Text>
|
</Text>
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
))}
|
)
|
||||||
</Animated.View>
|
})
|
||||||
|
) : (
|
||||||
|
<Text type="sm" style={[pal.text, pal.border, styles.noResults]}>
|
||||||
|
No result
|
||||||
|
</Text>
|
||||||
|
)}
|
||||||
</View>
|
</View>
|
||||||
|
) : null}
|
||||||
|
</Animated.View>
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
const styles = StyleSheet.create({
|
const styles = StyleSheet.create({
|
||||||
container: {
|
container: {
|
||||||
display: 'none',
|
marginLeft: -54,
|
||||||
height: 250,
|
top: 10,
|
||||||
},
|
|
||||||
animatedContainer: {
|
|
||||||
display: 'none',
|
|
||||||
position: 'absolute',
|
|
||||||
left: -64,
|
|
||||||
right: 0,
|
|
||||||
top: 0,
|
|
||||||
borderTopWidth: 1,
|
borderTopWidth: 1,
|
||||||
},
|
},
|
||||||
visible: {
|
|
||||||
display: 'flex',
|
|
||||||
},
|
|
||||||
item: {
|
item: {
|
||||||
borderBottomWidth: 1,
|
borderBottomWidth: 1,
|
||||||
paddingVertical: 16,
|
paddingVertical: 12,
|
||||||
paddingHorizontal: 16,
|
display: 'flex',
|
||||||
height: 50,
|
flexDirection: 'row',
|
||||||
|
alignItems: 'center',
|
||||||
|
justifyContent: 'space-between',
|
||||||
|
gap: 6,
|
||||||
|
},
|
||||||
|
avatarAndHandle: {
|
||||||
|
display: 'flex',
|
||||||
|
flexDirection: 'row',
|
||||||
|
gap: 6,
|
||||||
|
alignItems: 'center',
|
||||||
|
},
|
||||||
|
noResults: {
|
||||||
|
paddingVertical: 12,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
import React, {
|
import React, {
|
||||||
forwardRef,
|
forwardRef,
|
||||||
useCallback,
|
|
||||||
useEffect,
|
useEffect,
|
||||||
useImperativeHandle,
|
useImperativeHandle,
|
||||||
useMemo,
|
|
||||||
useState,
|
useState,
|
||||||
} from 'react'
|
} from 'react'
|
||||||
import {StyleSheet, View} from 'react-native'
|
import {StyleSheet, View} from 'react-native'
|
||||||
|
@ -16,9 +14,9 @@ import {
|
||||||
} from '@tiptap/suggestion'
|
} from '@tiptap/suggestion'
|
||||||
import {UserAutocompleteModel} from 'state/models/discovery/user-autocomplete'
|
import {UserAutocompleteModel} from 'state/models/discovery/user-autocomplete'
|
||||||
import {usePalette} from 'lib/hooks/usePalette'
|
import {usePalette} from 'lib/hooks/usePalette'
|
||||||
import Graphemer from 'graphemer'
|
|
||||||
import {Text} from 'view/com/util/text/Text'
|
import {Text} from 'view/com/util/text/Text'
|
||||||
import {UserAvatar} from 'view/com/util/UserAvatar'
|
import {UserAvatar} from 'view/com/util/UserAvatar'
|
||||||
|
import {useGrapheme} from '../hooks/useGrapheme'
|
||||||
|
|
||||||
interface MentionListRef {
|
interface MentionListRef {
|
||||||
onKeyDown: (props: SuggestionKeyDownProps) => boolean
|
onKeyDown: (props: SuggestionKeyDownProps) => boolean
|
||||||
|
@ -99,7 +97,7 @@ const MentionList = forwardRef<MentionListRef, SuggestionProps>(
|
||||||
(props: SuggestionProps, ref) => {
|
(props: SuggestionProps, ref) => {
|
||||||
const [selectedIndex, setSelectedIndex] = useState(0)
|
const [selectedIndex, setSelectedIndex] = useState(0)
|
||||||
const pal = usePalette('default')
|
const pal = usePalette('default')
|
||||||
const splitter = useMemo(() => new Graphemer(), [])
|
const {getGraphemeString} = useGrapheme()
|
||||||
|
|
||||||
const selectItem = (index: number) => {
|
const selectItem = (index: number) => {
|
||||||
const item = props.items[index]
|
const item = props.items[index]
|
||||||
|
@ -148,32 +146,14 @@ const MentionList = forwardRef<MentionListRef, SuggestionProps>(
|
||||||
|
|
||||||
const {items} = props
|
const {items} = props
|
||||||
|
|
||||||
const getDisplayedName = useCallback(
|
|
||||||
(name: string) => {
|
|
||||||
// Heuristic value based on max display name and handle lengths
|
|
||||||
const DISPLAY_LIMIT = 30
|
|
||||||
if (name.length > DISPLAY_LIMIT) {
|
|
||||||
const graphemes = splitter.splitGraphemes(name)
|
|
||||||
|
|
||||||
if (graphemes.length > DISPLAY_LIMIT) {
|
|
||||||
return graphemes.length > DISPLAY_LIMIT
|
|
||||||
? `${graphemes.slice(0, DISPLAY_LIMIT).join('')}...`
|
|
||||||
: name.substring(0, DISPLAY_LIMIT)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return name
|
|
||||||
},
|
|
||||||
[splitter],
|
|
||||||
)
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="items">
|
<div className="items">
|
||||||
<View style={[pal.borderDark, pal.view, styles.container]}>
|
<View style={[pal.borderDark, pal.view, styles.container]}>
|
||||||
{items.length > 0 ? (
|
{items.length > 0 ? (
|
||||||
items.map((item, index) => {
|
items.map((item, index) => {
|
||||||
const displayName = getDisplayedName(
|
const {name: displayName} = getGraphemeString(
|
||||||
item.displayName ?? item.handle,
|
item.displayName ?? item.handle,
|
||||||
|
30, // Heuristic value; can be modified
|
||||||
)
|
)
|
||||||
const isSelected = selectedIndex === index
|
const isSelected = selectedIndex === index
|
||||||
|
|
||||||
|
@ -197,7 +177,7 @@ const MentionList = forwardRef<MentionListRef, SuggestionProps>(
|
||||||
</Text>
|
</Text>
|
||||||
</View>
|
</View>
|
||||||
<Text type="xs" style={pal.textLight} numberOfLines={1}>
|
<Text type="xs" style={pal.textLight} numberOfLines={1}>
|
||||||
{item.handle}
|
@{item.handle}
|
||||||
</Text>
|
</Text>
|
||||||
</View>
|
</View>
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue