about summary refs log tree commit diff
path: root/src/components/NewskieDialog.tsx
blob: 30f70f549c7677a4690cfe1826a54c4743fef1cf (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import {useMemo, useState} from 'react'
import {View} from 'react-native'
import {type AppBskyActorDefs, moderateProfile} from '@atproto/api'
import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react'
import {differenceInSeconds} from 'date-fns'

import {HITSLOP_10} from '#/lib/constants'
import {useGetTimeAgo} from '#/lib/hooks/useTimeAgo'
import {sanitizeDisplayName} from '#/lib/strings/display-names'
import {isNative} from '#/platform/detection'
import {useModerationOpts} from '#/state/preferences/moderation-opts'
import {useSession} from '#/state/session'
import {atoms as a, useTheme, web} from '#/alf'
import {Button, ButtonText} from '#/components/Button'
import * as Dialog from '#/components/Dialog'
import {useDialogControl} from '#/components/Dialog'
import {Newskie} from '#/components/icons/Newskie'
import * as StarterPackCard from '#/components/StarterPack/StarterPackCard'
import {Text} from '#/components/Typography'

export function NewskieDialog({
  profile,
  disabled,
}: {
  profile: AppBskyActorDefs.ProfileViewDetailed
  disabled?: boolean
}) {
  const {_} = useLingui()
  const control = useDialogControl()

  const createdAt = profile.createdAt as string | undefined

  const [now] = useState(() => Date.now())
  const daysOld = useMemo(() => {
    if (!createdAt) return Infinity
    return differenceInSeconds(now, new Date(createdAt)) / 86400
  }, [createdAt, now])

  if (!createdAt || daysOld > 7) return null

  return (
    <View style={[a.pr_2xs]}>
      <Button
        disabled={disabled}
        label={_(
          msg`This user is new here. Press for more info about when they joined.`,
        )}
        hitSlop={HITSLOP_10}
        onPress={control.open}>
        {({hovered, pressed}) => (
          <Newskie
            size="lg"
            fill="#FFC404"
            style={{
              opacity: hovered || pressed ? 0.5 : 1,
            }}
          />
        )}
      </Button>

      <Dialog.Outer control={control} nativeOptions={{preventExpansion: true}}>
        <Dialog.Handle />
        <DialogInner profile={profile} createdAt={createdAt} now={now} />
      </Dialog.Outer>
    </View>
  )
}

function DialogInner({
  profile,
  createdAt,
  now,
}: {
  profile: AppBskyActorDefs.ProfileViewDetailed
  createdAt: string
  now: number
}) {
  const control = Dialog.useDialogContext()
  const {_} = useLingui()
  const t = useTheme()
  const moderationOpts = useModerationOpts()
  const {currentAccount} = useSession()
  const timeAgo = useGetTimeAgo()
  const isMe = profile.did === currentAccount?.did

  const profileName = useMemo(() => {
    const name = profile.displayName || profile.handle

    if (isMe) {
      return _(msg`You`)
    }

    if (!moderationOpts) return name
    const moderation = moderateProfile(profile, moderationOpts)

    return sanitizeDisplayName(name, moderation.ui('displayName'))
  }, [_, isMe, moderationOpts, profile])

  return (
    <Dialog.ScrollableInner
      label={_(msg`New user info dialog`)}
      style={web({maxWidth: 400})}>
      <View style={[a.gap_md]}>
        <View style={[a.align_center]}>
          <View
            style={[
              {
                height: 60,
                width: 64,
              },
            ]}>
            <Newskie
              width={64}
              height={64}
              fill="#FFC404"
              style={[a.absolute, a.inset_0]}
            />
          </View>
          <Text style={[a.font_bold, a.text_xl]}>
            {isMe ? <Trans>Welcome, friend!</Trans> : <Trans>Say hello!</Trans>}
          </Text>
        </View>
        <Text style={[a.text_md, a.text_center, a.leading_snug]}>
          {profile.joinedViaStarterPack ? (
            <Trans>
              {profileName} joined Bluesky using a starter pack{' '}
              {timeAgo(createdAt, now, {format: 'long'})} ago
            </Trans>
          ) : (
            <Trans>
              {profileName} joined Bluesky{' '}
              {timeAgo(createdAt, now, {format: 'long'})} ago
            </Trans>
          )}
        </Text>
        {profile.joinedViaStarterPack ? (
          <StarterPackCard.Link
            starterPack={profile.joinedViaStarterPack}
            onPress={() => control.close()}>
            <View
              style={[
                a.w_full,
                a.mt_sm,
                a.p_lg,
                a.border,
                a.rounded_sm,
                t.atoms.border_contrast_low,
              ]}>
              <StarterPackCard.Card
                starterPack={profile.joinedViaStarterPack}
              />
            </View>
          </StarterPackCard.Link>
        ) : null}

        {isNative && (
          <Button
            label={_(msg`Close`)}
            color="secondary"
            size="small"
            style={[a.mt_sm]}
            onPress={() => control.close()}>
            <ButtonText>
              <Trans>Close</Trans>
            </ButtonText>
          </Button>
        )}
      </View>

      <Dialog.Close />
    </Dialog.ScrollableInner>
  )
}