about summary refs log tree commit diff
path: root/src/screens/Onboarding/StepProfile/PlaceholderCanvas.tsx
blob: acc0893350a9f4156528b6277ea91336d156bb15 (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
import React from 'react'
import {View} from 'react-native'
import type ViewShot from 'react-native-view-shot'

import {useAvatar} from '#/screens/Onboarding/StepProfile/index'
import {atoms as a} from '#/alf'

const LazyViewShot = React.lazy(
  // @ts-expect-error dynamic import
  () => import('react-native-view-shot/src/index'),
)

const SIZE_MULTIPLIER = 5

export interface PlaceholderCanvasRef {
  capture: () => Promise<string | undefined>
}

// This component is supposed to be invisible to the user. We only need this for ViewShot to have something to
// "screenshot".
export const PlaceholderCanvas = React.forwardRef<PlaceholderCanvasRef, {}>(
  function PlaceholderCanvas({}, ref) {
    const {avatar} = useAvatar()
    const viewshotRef = React.useRef<ViewShot>(null)
    const Icon = avatar.placeholder.component

    const styles = React.useMemo(
      () => ({
        container: [a.absolute, {top: -2000}],
        imageContainer: [
          a.align_center,
          a.justify_center,
          {height: 150 * SIZE_MULTIPLIER, width: 150 * SIZE_MULTIPLIER},
        ],
      }),
      [],
    )

    React.useImperativeHandle(ref, () => ({
      capture: async () => {
        if (viewshotRef.current?.capture) {
          return await viewshotRef.current.capture()
        }
      },
    }))

    return (
      <View style={styles.container}>
        <React.Suspense fallback={null}>
          <LazyViewShot
            // @ts-ignore this library doesn't have types
            ref={viewshotRef}
            options={{
              fileName: 'placeholderAvatar',
              format: 'jpg',
              quality: 0.8,
              height: 150 * SIZE_MULTIPLIER,
              width: 150 * SIZE_MULTIPLIER,
            }}>
            <View
              style={[
                styles.imageContainer,
                {backgroundColor: avatar.backgroundColor},
              ]}
              collapsable={false}>
              <Icon
                height={85 * SIZE_MULTIPLIER}
                width={85 * SIZE_MULTIPLIER}
                style={{color: 'white'}}
              />
            </View>
          </LazyViewShot>
        </React.Suspense>
      </View>
    )
  },
)