about summary refs log tree commit diff
path: root/src/components/StarterPack/Wizard/WizardEditListDialog.tsx
blob: 731323f7ff2c2b26abf692f9e5c19aec10e45dbb (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import {useRef} from 'react'
import {type ListRenderItemInfo} from 'react-native'
import {View} from 'react-native'
import {
  type AppBskyActorDefs,
  type AppBskyFeedDefs,
  type ModerationOpts,
} from '@atproto/api'
import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react'

import {useInitialNumToRender} from '#/lib/hooks/useInitialNumToRender'
import {isWeb} from '#/platform/detection'
import {useSession} from '#/state/session'
import {type ListMethods} from '#/view/com/util/List'
import {
  type WizardAction,
  type WizardState,
} from '#/screens/StarterPack/Wizard/State'
import {atoms as a, native, useTheme, web} from '#/alf'
import {Button, ButtonText} from '#/components/Button'
import * as Dialog from '#/components/Dialog'
import {
  WizardFeedCard,
  WizardProfileCard,
} from '#/components/StarterPack/Wizard/WizardListCard'
import {Text} from '#/components/Typography'

function keyExtractor(
  item: AppBskyActorDefs.ProfileViewBasic | AppBskyFeedDefs.GeneratorView,
  index: number,
) {
  return `${item.did}-${index}`
}

export function WizardEditListDialog({
  control,
  state,
  dispatch,
  moderationOpts,
  profile,
}: {
  control: Dialog.DialogControlProps
  state: WizardState
  dispatch: (action: WizardAction) => void
  moderationOpts: ModerationOpts
  profile: AppBskyActorDefs.ProfileViewDetailed
}) {
  const {_} = useLingui()
  const t = useTheme()
  const {currentAccount} = useSession()
  const initialNumToRender = useInitialNumToRender()

  const listRef = useRef<ListMethods>(null)

  const getData = () => {
    if (state.currentStep === 'Feeds') return state.feeds

    return [
      profile,
      ...state.profiles.filter(p => p.did !== currentAccount?.did),
    ]
  }

  const renderItem = ({item}: ListRenderItemInfo<any>) =>
    state.currentStep === 'Profiles' ? (
      <WizardProfileCard
        profile={item}
        btnType="remove"
        state={state}
        dispatch={dispatch}
        moderationOpts={moderationOpts}
      />
    ) : (
      <WizardFeedCard
        generator={item}
        btnType="remove"
        state={state}
        dispatch={dispatch}
        moderationOpts={moderationOpts}
      />
    )

  return (
    <Dialog.Outer control={control} testID="newChatDialog">
      <Dialog.Handle />
      <Dialog.InnerFlatList
        ref={listRef}
        data={getData()}
        renderItem={renderItem}
        keyExtractor={keyExtractor}
        ListHeaderComponent={
          <View
            style={[
              native(a.pt_4xl),
              a.flex_row,
              a.justify_between,
              a.border_b,
              a.px_sm,
              a.mb_sm,
              t.atoms.bg,
              t.atoms.border_contrast_medium,
              isWeb
                ? [
                    a.align_center,
                    {
                      height: 48,
                    },
                  ]
                : [a.pb_sm, a.align_end],
            ]}>
            <View style={{width: 60}} />
            <Text style={[a.font_bold, a.text_xl]}>
              {state.currentStep === 'Profiles' ? (
                <Trans>Edit People</Trans>
              ) : (
                <Trans>Edit Feeds</Trans>
              )}
            </Text>
            <View style={{width: 60}}>
              {isWeb && (
                <Button
                  label={_(msg`Close`)}
                  variant="ghost"
                  color="primary"
                  size="small"
                  onPress={() => control.close()}>
                  <ButtonText>
                    <Trans>Close</Trans>
                  </ButtonText>
                </Button>
              )}
            </View>
          </View>
        }
        stickyHeaderIndices={[0]}
        style={[
          web([a.py_0, {height: '100vh', maxHeight: 600}, a.px_0]),
          native({
            height: '100%',
            paddingHorizontal: 0,
            marginTop: 0,
            paddingTop: 0,
          }),
        ]}
        webInnerStyle={[a.py_0, {maxWidth: 500, minWidth: 200}]}
        keyboardDismissMode="on-drag"
        removeClippedSubviews={true}
        initialNumToRender={initialNumToRender}
      />
    </Dialog.Outer>
  )
}