about summary refs log tree commit diff
path: root/src/view/screens/stacks/Composer.tsx
blob: e1b36567acdbed88dee4f642a7f133eddb9b1241 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import React, {useLayoutEffect, useRef} from 'react'
import {Text, TouchableOpacity} from 'react-native'
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
import {Shell} from '../../shell'
import type {RootTabsScreenProps} from '../../routes/types'
import {Composer as ComposerComponent} from '../../com/composer/Composer'

export const Composer = ({
  navigation,
  route,
}: RootTabsScreenProps<'Composer'>) => {
  const {replyTo} = route.params
  const ref = useRef<{publish: () => Promise<boolean>}>()

  useLayoutEffect(() => {
    navigation.setOptions({
      headerShown: true,
      headerTitle: replyTo ? 'Reply' : 'New Post',
      headerLeft: () => (
        <TouchableOpacity onPress={() => navigation.goBack()}>
          <FontAwesomeIcon icon="x" />
        </TouchableOpacity>
      ),
      headerRight: () => (
        <TouchableOpacity
          onPress={() => {
            if (!ref.current) {
              return
            }
            ref.current.publish().then(
              posted => {
                if (posted) {
                  navigation.goBack()
                }
              },
              err => console.error('Failed to create post', err),
            )
          }}>
          <Text>Post</Text>
        </TouchableOpacity>
      ),
    })
  }, [navigation, replyTo, ref])

  return (
    <Shell>
      <ComposerComponent ref={ref} replyTo={replyTo} />
    </Shell>
  )
}