about summary refs log tree commit diff
path: root/src/screens/ProfileList/AboutSection.tsx
blob: 47f29b8386b0196f877b6d7a16f9717be12f1255 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import {useCallback, useImperativeHandle, useState} from 'react'
import {View} from 'react-native'
import {type AppBskyGraphDefs} from '@atproto/api'
import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react'

import {isNative} from '#/platform/detection'
import {useSession} from '#/state/session'
import {ListMembers} from '#/view/com/lists/ListMembers'
import {EmptyState} from '#/view/com/util/EmptyState'
import {type ListRef} from '#/view/com/util/List'
import {LoadLatestBtn} from '#/view/com/util/load-latest/LoadLatestBtn'
import {atoms as a, useBreakpoints} from '#/alf'
import {Button, ButtonIcon, ButtonText} from '#/components/Button'
import {PersonPlus_Stroke2_Corner0_Rounded as PersonPlusIcon} from '#/components/icons/Person'

interface SectionRef {
  scrollToTop: () => void
}

interface AboutSectionProps {
  ref?: React.Ref<SectionRef>
  list: AppBskyGraphDefs.ListView
  onPressAddUser: () => void
  headerHeight: number
  scrollElRef: ListRef
}

export function AboutSection({
  ref,
  list,
  onPressAddUser,
  headerHeight,
  scrollElRef,
}: AboutSectionProps) {
  const {_} = useLingui()
  const {currentAccount} = useSession()
  const {gtMobile} = useBreakpoints()
  const [isScrolledDown, setIsScrolledDown] = useState(false)
  const isOwner = list.creator.did === currentAccount?.did

  const onScrollToTop = useCallback(() => {
    scrollElRef.current?.scrollToOffset({
      animated: isNative,
      offset: -headerHeight,
    })
  }, [scrollElRef, headerHeight])

  useImperativeHandle(ref, () => ({
    scrollToTop: onScrollToTop,
  }))

  const renderHeader = useCallback(() => {
    if (!isOwner) {
      return <View />
    }
    if (!gtMobile) {
      return (
        <View style={[a.px_sm, a.py_sm]}>
          <Button
            testID="addUserBtn"
            label={_(msg`Add a user to this list`)}
            onPress={onPressAddUser}
            color="primary"
            size="small"
            variant="outline"
            style={[a.py_md]}>
            <ButtonIcon icon={PersonPlusIcon} />
            <ButtonText>
              <Trans>Add people</Trans>
            </ButtonText>
          </Button>
        </View>
      )
    }
    return (
      <View style={[a.px_lg, a.py_md, a.flex_row_reverse]}>
        <Button
          testID="addUserBtn"
          label={_(msg`Add a user to this list`)}
          onPress={onPressAddUser}
          color="primary"
          size="small"
          variant="ghost"
          style={[a.py_sm]}>
          <ButtonIcon icon={PersonPlusIcon} />
          <ButtonText>
            <Trans>Add people</Trans>
          </ButtonText>
        </Button>
      </View>
    )
  }, [isOwner, _, onPressAddUser, gtMobile])

  const renderEmptyState = useCallback(() => {
    return (
      <View style={[a.gap_xl, a.align_center]}>
        <EmptyState icon="users-slash" message={_(msg`This list is empty.`)} />
        {isOwner && (
          <Button
            testID="emptyStateAddUserBtn"
            label={_(msg`Start adding people`)}
            onPress={onPressAddUser}
            color="primary"
            size="small">
            <ButtonIcon icon={PersonPlusIcon} />
            <ButtonText>
              <Trans>Start adding people!</Trans>
            </ButtonText>
          </Button>
        )}
      </View>
    )
  }, [_, onPressAddUser, isOwner])

  return (
    <View>
      <ListMembers
        testID="listItems"
        list={list.uri}
        scrollElRef={scrollElRef}
        renderHeader={renderHeader}
        renderEmptyState={renderEmptyState}
        headerOffset={headerHeight}
        onScrolledDownChange={setIsScrolledDown}
      />
      {isScrolledDown && (
        <LoadLatestBtn
          onPress={onScrollToTop}
          label={_(msg`Scroll to top`)}
          showIndicator={false}
        />
      )}
    </View>
  )
}