about summary refs log tree commit diff
path: root/src/components/dialogs/LinkWarning.tsx
blob: 9ae871812705d60aedc0c8dbd0f3882d973df2ec (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
import {useCallback, useMemo} from 'react'
import {View} from 'react-native'
import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react'

import {useOpenLink} from '#/lib/hooks/useOpenLink'
import {shareUrl} from '#/lib/sharing'
import {isPossiblyAUrl, splitApexDomain} from '#/lib/strings/url-helpers'
import {atoms as a, useBreakpoints, useTheme, web} from '#/alf'
import {Button, ButtonText} from '#/components/Button'
import * as Dialog from '#/components/Dialog'
import {Text} from '#/components/Typography'
import {useGlobalDialogsControlContext} from './Context'

export function LinkWarningDialog() {
  const {linkWarningDialogControl} = useGlobalDialogsControlContext()

  return (
    <Dialog.Outer
      control={linkWarningDialogControl.control}
      nativeOptions={{preventExpansion: true}}
      webOptions={{alignCenter: true}}
      onClose={linkWarningDialogControl.clear}>
      <Dialog.Handle />
      <InAppBrowserConsentInner link={linkWarningDialogControl.value} />
    </Dialog.Outer>
  )
}

function InAppBrowserConsentInner({
  link,
}: {
  link?: {href: string; displayText: string; share?: boolean}
}) {
  const control = Dialog.useDialogContext()
  const {_} = useLingui()
  const t = useTheme()
  const openLink = useOpenLink()
  const {gtMobile} = useBreakpoints()

  const potentiallyMisleading = useMemo(
    () => link && isPossiblyAUrl(link.displayText),
    [link],
  )

  const onPressVisit = useCallback(() => {
    control.close(() => {
      if (!link) return
      if (link.share) {
        shareUrl(link.href)
      } else {
        openLink(link.href, undefined, true)
      }
    })
  }, [control, link, openLink])

  const onCancel = useCallback(() => {
    control.close()
  }, [control])

  return (
    <Dialog.ScrollableInner
      style={web({maxWidth: 450})}
      label={
        potentiallyMisleading
          ? _(msg`Potentially misleading link warning`)
          : _(msg`Leaving Bluesky`)
      }>
      <View style={[a.gap_2xl]}>
        <View style={[a.gap_sm]}>
          <Text style={[a.font_heavy, a.text_2xl]}>
            {potentiallyMisleading ? (
              <Trans>Potentially misleading link</Trans>
            ) : (
              <Trans>Leaving Bluesky</Trans>
            )}
          </Text>
          <Text style={[t.atoms.text_contrast_high, a.text_md, a.leading_snug]}>
            <Trans>This link is taking you to the following website:</Trans>
          </Text>
          {link && <LinkBox href={link.href} />}
          {potentiallyMisleading && (
            <Text
              style={[t.atoms.text_contrast_high, a.text_md, a.leading_snug]}>
              <Trans>Make sure this is where you intend to go!</Trans>
            </Text>
          )}
        </View>
        <View
          style={[
            a.flex_1,
            a.gap_sm,
            gtMobile && [a.flex_row_reverse, a.justify_start],
          ]}>
          <Button
            label={link?.share ? _(msg`Share link`) : _(msg`Visit site`)}
            accessibilityHint={_(msg`Opens link ${link?.href ?? ''}`)}
            onPress={onPressVisit}
            size="large"
            variant="solid"
            color={potentiallyMisleading ? 'secondary_inverted' : 'primary'}>
            <ButtonText>
              {link?.share ? (
                <Trans>Share link</Trans>
              ) : (
                <Trans>Visit site</Trans>
              )}
            </ButtonText>
          </Button>
          <Button
            label={_(msg`Go back`)}
            onPress={onCancel}
            size="large"
            variant="ghost"
            color="secondary">
            <ButtonText>
              <Trans>Go back</Trans>
            </ButtonText>
          </Button>
        </View>
      </View>
      <Dialog.Close />
    </Dialog.ScrollableInner>
  )
}

function LinkBox({href}: {href: string}) {
  const t = useTheme()
  const [scheme, hostname, rest] = useMemo(() => {
    try {
      const urlp = new URL(href)
      const [subdomain, apexdomain] = splitApexDomain(urlp.hostname)
      return [
        urlp.protocol + '//' + subdomain,
        apexdomain,
        urlp.pathname.replace(/\/$/, '') + urlp.search + urlp.hash,
      ]
    } catch {
      return ['', href, '']
    }
  }, [href])
  return (
    <View
      style={[
        t.atoms.bg,
        t.atoms.border_contrast_medium,
        a.px_md,
        {paddingVertical: 10},
        a.rounded_sm,
        a.border,
      ]}>
      <Text style={[a.text_md, a.leading_snug, t.atoms.text_contrast_medium]}>
        {scheme}
        <Text style={[a.text_md, a.leading_snug, t.atoms.text, a.font_bold]}>
          {hostname}
        </Text>
        {rest}
      </Text>
    </View>
  )
}