send along tags with api request

This commit is contained in:
Eric Bailey
2023-09-25 14:28:05 -05:00
parent 763edc8fb3
commit f9914d6ff6
4 changed files with 35 additions and 7 deletions
+2
View File
@@ -83,6 +83,7 @@ interface PostOpts {
knownHandles?: Set<string> knownHandles?: Set<string>
onStateChange?: (state: string) => void onStateChange?: (state: string) => void
langs?: string[] langs?: string[]
tags?: string[]
} }
export async function post(store: RootStoreModel, opts: PostOpts) { export async function post(store: RootStoreModel, opts: PostOpts) {
@@ -264,6 +265,7 @@ export async function post(store: RootStoreModel, opts: PostOpts) {
embed, embed,
langs, langs,
labels, labels,
tags: opts.tags?.filter(t => t.replace(/^#/, '')),
}) })
} catch (e: any) { } catch (e: any) {
console.error(`Failed to create post: ${e.toString()}`) console.error(`Failed to create post: ${e.toString()}`)
+10 -1
View File
@@ -91,6 +91,7 @@ export const ComposePost = observer(function ComposePost({
const [labels, setLabels] = useState<string[]>([]) const [labels, setLabels] = useState<string[]>([])
const [suggestedLinks, setSuggestedLinks] = useState<Set<string>>(new Set()) const [suggestedLinks, setSuggestedLinks] = useState<Set<string>>(new Set())
const gallery = useMemo(() => new GalleryModel(store), [store]) const gallery = useMemo(() => new GalleryModel(store), [store])
const [tags, setTags] = useState<string[]>([])
const autocompleteView = useMemo<UserAutocompleteModel>( const autocompleteView = useMemo<UserAutocompleteModel>(
() => new UserAutocompleteModel(store), () => new UserAutocompleteModel(store),
@@ -167,6 +168,13 @@ export const ComposePost = observer(function ComposePost({
[gallery, track], [gallery, track],
) )
const onChangeTags = useCallback(
(tags: string[]) => {
setTags(tags)
},
[setTags],
)
const onPressPublish = async () => { const onPressPublish = async () => {
if (isProcessing || graphemeLength > MAX_GRAPHEME_LENGTH) { if (isProcessing || graphemeLength > MAX_GRAPHEME_LENGTH) {
return return
@@ -195,6 +203,7 @@ export const ComposePost = observer(function ComposePost({
onStateChange: setProcessingState, onStateChange: setProcessingState,
knownHandles: autocompleteView.knownHandles, knownHandles: autocompleteView.knownHandles,
langs: store.preferences.postLanguages, langs: store.preferences.postLanguages,
tags,
}) })
} catch (e: any) { } catch (e: any) {
if (extLink) { if (extLink) {
@@ -387,7 +396,7 @@ export const ComposePost = observer(function ComposePost({
pal.border, pal.border,
{borderTopWidth: 1, paddingVertical: 10, marginTop: 10}, {borderTopWidth: 1, paddingVertical: 10, marginTop: 10},
]}> ]}>
<TagInput max={8} /> <TagInput max={8} onChangeTags={onChangeTags} />
</View> </View>
</ScrollView> </ScrollView>
{!extLink && suggestedLinks.size > 0 ? ( {!extLink && suggestedLinks.size > 0 ? (
+22 -6
View File
@@ -38,19 +38,35 @@ function Tag({
) )
} }
export function TagInput({max}: {max: number}) { export function TagInput({
max,
onChangeTags,
}: {
max: number
onChangeTags: (tags: string[]) => void
}) {
const pal = usePalette('default') const pal = usePalette('default')
const input = React.useRef<TextInput>(null) const input = React.useRef<TextInput>(null)
const [value, setValue] = React.useState('') const [value, setValue] = React.useState('')
const [tags, setTags] = React.useState<string[]>([]) const [tags, setTags] = React.useState<string[]>([])
const handleChangeTags = React.useCallback(
(_tags: string[]) => {
setTags(_tags)
onChangeTags(_tags)
},
[onChangeTags, setTags],
)
const onKeyPress = React.useCallback( const onKeyPress = React.useCallback(
(e: NativeSyntheticEvent<TextInputKeyPressEventData>) => { (e: NativeSyntheticEvent<TextInputKeyPressEventData>) => {
if (e.nativeEvent.key === 'Enter') { if (e.nativeEvent.key === 'Enter') {
const _tags = value.trim().split(' ').filter(Boolean) const _tags = value.trim().split(' ').filter(Boolean)
if (_tags.length > 0) { if (_tags.length > 0) {
setTags(Array.from(new Set([...tags, ..._tags])).slice(0, max)) handleChangeTags(
Array.from(new Set([...tags, ..._tags])).slice(0, max),
)
setValue('') setValue('')
} }
@@ -58,10 +74,10 @@ export function TagInput({max}: {max: number}) {
input.current?.focus() input.current?.focus()
}, 100) }, 100)
} else if (e.nativeEvent.key === 'Backspace' && value === '') { } else if (e.nativeEvent.key === 'Backspace' && value === '') {
setTags(tags.slice(0, -1)) handleChangeTags(tags.slice(0, -1))
} }
}, },
[max, value, tags, setValue, setTags], [max, value, tags, setValue, handleChangeTags],
) )
const onChangeText = React.useCallback((value: string) => { const onChangeText = React.useCallback((value: string) => {
@@ -71,9 +87,9 @@ export function TagInput({max}: {max: number}) {
const removeTag = React.useCallback( const removeTag = React.useCallback(
(tag: string) => { (tag: string) => {
setTags(tags.filter(t => t !== tag)) handleChangeTags(tags.filter(t => t !== tag))
}, },
[tags, setTags], [tags, handleChangeTags],
) )
return ( return (
@@ -33,6 +33,7 @@ function getDecorations(doc: ProsemirrorNode) {
while ((match = TAG_REGEX.exec(textContent))) { while ((match = TAG_REGEX.exec(textContent))) {
const [, m] = match const [, m] = match
const tag = m.trim().replace(/\p{P}+$/gu, '') const tag = m.trim().replace(/\p{P}+$/gu, '')
if (tag.length > 66) continue
const from = match.index + 1 const from = match.index + 1
const to = from + tag.length + 1 const to = from + tag.length + 1
decorations.push( decorations.push(