about summary refs log tree commit diff
path: root/src/view/com/sheets/SharePost.tsx
blob: b0f22c54e28bedd347e7d076174f071d88b0ccfd (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import React, {
  forwardRef,
  useState,
  useMemo,
  useImperativeHandle,
  useRef,
} from 'react'
import {
  Button,
  StyleSheet,
  Text,
  TouchableOpacity,
  TouchableWithoutFeedback,
  View,
} from 'react-native'
import BottomSheet, {BottomSheetBackdropProps} from '@gorhom/bottom-sheet'
import Animated, {
  Extrapolate,
  interpolate,
  useAnimatedStyle,
} from 'react-native-reanimated'
import Toast from 'react-native-root-toast'
import Clipboard from '@react-native-clipboard/clipboard'
import {s} from '../../lib/styles'

export const ShareBottomSheet = forwardRef(function ShareBottomSheet(
  {}: {},
  ref,
) {
  const [isOpen, setIsOpen] = useState<boolean>(false)
  const [uri, setUri] = useState<string>('')
  const bottomSheetRef = useRef<BottomSheet>(null)

  useImperativeHandle(ref, () => ({
    open(uri: string) {
      console.log('sharing', uri)
      setUri(uri)
      setIsOpen(true)
    },
  }))

  const onPressCopy = () => {
    Clipboard.setString(uri)
    Toast.show('Link copied', {
      position: Toast.positions.TOP,
    })
  }
  const onShareBottomSheetChange = (snapPoint: number) => {
    if (snapPoint === -1) {
      console.log('unsharing')
      setIsOpen(false)
    }
  }
  const onClose = () => {
    bottomSheetRef.current?.close()
  }

  const CustomBackdrop = ({animatedIndex, style}: BottomSheetBackdropProps) => {
    console.log('hit!', animatedIndex.value)
    // animated variables
    const opacity = useAnimatedStyle(() => ({
      opacity: interpolate(
        animatedIndex.value, // current snap index
        [-1, 0], // input range
        [0, 0.5], // output range
        Extrapolate.CLAMP,
      ),
    }))

    const containerStyle = useMemo(
      () => [style, {backgroundColor: '#000'}, opacity],
      [style, opacity],
    )

    return (
      <TouchableWithoutFeedback onPress={onClose}>
        <Animated.View style={containerStyle} />
      </TouchableWithoutFeedback>
    )
  }
  return (
    <>
      {isOpen && (
        <BottomSheet
          ref={bottomSheetRef}
          snapPoints={['50%']}
          enablePanDownToClose
          backdropComponent={CustomBackdrop}
          onChange={onShareBottomSheetChange}>
          <View>
            <Text style={[s.textCenter, s.bold, s.mb10]}>Share this post</Text>
            <Text style={[s.textCenter, s.mb10]}>{uri}</Text>
            <Button title="Copy to clipboard" onPress={onPressCopy} />
            <View style={s.p10}>
              <TouchableOpacity onPress={onClose} style={styles.closeBtn}>
                <Text style={s.textCenter}>Close</Text>
              </TouchableOpacity>
            </View>
          </View>
        </BottomSheet>
      )}
    </>
  )
})

const styles = StyleSheet.create({
  closeBtn: {
    width: '100%',
    borderColor: '#000',
    borderWidth: 1,
    borderRadius: 4,
    padding: 10,
  },
})