Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440import {useEffect, useRef, useState} from 'react'import {View} from 'react-native'import {Trans, useLingui} from '@lingui/react/macro'
import {getLinkMeta} from '#/lib/link-meta/link-meta'import {atoms as a, tokens, useTheme, web} from '#/alf'import {Button, ButtonIcon, ButtonText} from '#/components/Button'import * as Dialog from '#/components/Dialog'import * as TextField from '#/components/forms/TextField'import {ChainLink_Stroke2_Corner0_Rounded as ChainLinkIcon} from '#/components/icons/ChainLink'import {TimesLarge_Stroke2_Corner0_Rounded as XIcon} from '#/components/icons/TimesLarge'import {createStaticClick, InlineLinkText} from '#/components/Link'import {Loader} from '#/components/Loader'import {Text} from '#/components/Typography'import {useAnalytics} from '#/analytics'import {IS_LIQUID_GLASS} from '#/env'import {useSupportPalette} from '../palette'import { formatLinkUrl, getLinkHost, normalizeProfileLinkUrl, validateLinkInput,} from '../providers'import {checkSafelink} from '../safelink'import {useUploadLinkIcon} from '../state'import {MAX_TITLE_LENGTH, type ProfileLink} from '../types'import {ProviderLogo} from './ProviderLogo'import {SupportedSitesDialog} from './SupportedSitesDialog'
/** The counter stays out of the way until the title is getting long. */const SHOW_COUNTER_FROM = 20
/** * Turns a page title into something pill-sized: the part before a separator * like " | " or " - " (sites append a tagline or their name), cut back to the * last whole word that fits. */function suggestTitle(pageTitle: string | undefined): string { const first = (pageTitle ?? '').split(/\s+[|\-\u2013\u2014\u00b7]\s+|:\s+/)[0] ?? '' const trimmed = first.trim() if (trimmed.length <= MAX_TITLE_LENGTH) return trimmed const cut = trimmed.slice(0, MAX_TITLE_LENGTH) const lastSpace = cut.lastIndexOf(' ') return (lastSpace > 0 ? cut.slice(0, lastSpace) : cut).trim()}
/** * Adds, edits or removes a profile link. Pass the link being edited, or null * to add a new one. The caller decides what saving means. */export function LinkFormDialog({ control, link, existingUrls, onSave, onRemove,}: { control: Dialog.DialogControlProps link: ProfileLink | null /** Other links on the profile, to catch duplicates. */ existingUrls: string[] /** Runs after the sheet closes. */ onSave: (link: ProfileLink) => void /** Runs after the sheet closes. */ onRemove?: () => void}) { return ( <Dialog.Outer control={control} webOptions={{alignCenter: true}} /* * The link field autofocuses, and a sheet that can grow to full height * jumps to it when the keyboard opens, scrolling the title out of view. * Keeping the sheet at content height lifts it above the keyboard. */ nativeOptions={{preventExpansion: true}}> <Dialog.Handle /> <LinkFormInner link={link} existingUrls={existingUrls} onSave={onSave} onRemove={onRemove} /> </Dialog.Outer> )}
function LinkFormInner({ link, existingUrls, onSave, onRemove,}: { link: ProfileLink | null existingUrls: string[] onSave: (link: ProfileLink) => void onRemove?: () => void}) { const control = Dialog.useDialogContext() const {t: l} = useLingui() const t = useTheme() const ax = useAnalytics() const palette = useSupportPalette() const uploadLinkIcon = useUploadLinkIcon() const supportedSitesControl = Dialog.useDialogControl()
const isEditing = !!link const startingInput = link ? formatLinkUrl(link.url) : '' const [input, setInput] = useState(startingInput) const [touched, setTouched] = useState(false) const [title, setTitle] = useState(link?.title ?? '') /* * Before saving a new destination we ask the link preview service whether * the page exists, and fetch the site's icon. Some sites block scrapers, so * a failed check is a warning: pressing Save again saves anyway. */ const [reachability, setReachability] = useState< 'unchecked' | 'checking' | 'reachable' | 'unreachable' >('unchecked') /* * "ko-fi" is invalid until the ".com" lands, so errors only show once Save * is pressed, not on every keystroke. An http link is the exception: it * shows right away, since typing more can't fix it. */ const [error, setError] = useState< 'invalid' | 'blocked' | 'shortener' | 'duplicate' | undefined >()
const validation = validateLinkInput(input) const provider = validation.provider // untouched input keeps the saved link exactly as it was const targetUrl = link && input.trim() === startingInput ? link.url : validation.url
/* * Suggest a title from the page once the link parses. Only a blank title, * or one we filled in earlier, is replaced; anything typed stays. Support * links skip this, so they read "Support on <provider>". */ const autoTitle = useRef('') useEffect(() => { if ( !targetUrl || provider || validation.isShortener || validation.isInsecure ) { return } let cancelled = false void getLinkMeta(targetUrl, 8e3).then(meta => { if (cancelled || meta.error) return setReachability('reachable') // a standard.site record's title, like a blog's name, beats the HTML one const suggestion = suggestTitle(meta.view?.external?.title || meta.title) if (!suggestion) return const previousAuto = autoTitle.current autoTitle.current = suggestion setTitle(prev => prev.trim() && prev !== previousAuto ? prev : suggestion, ) }) return () => { cancelled = true } }, [targetUrl, provider, validation.isShortener, validation.isInsecure])
const nextTitle = title.trim() || undefined const isUnchanged = isEditing && targetUrl === link.url && nextTitle === link.title const canSave = !validation.isEmpty && !validation.isInsecure && !isUnchanged && reachability !== 'checking'
const save = async () => { if (!canSave) return if (!targetUrl) { setError('invalid') return } if (validation.isShortener) { setError('shortener') ax.metric('profile:links:rejected', { reason: 'shortener', domain: getLinkHost(targetUrl), }) return } const normalized = normalizeProfileLinkUrl(targetUrl) const isDuplicate = existingUrls.some( url => normalizeProfileLinkUrl(url) === normalized, ) if (isDuplicate) { setError('duplicate') return } const isNewDestination = targetUrl !== link?.url // a saved link has proven itself; only check new destinations const needsCheck = isNewDestination && reachability === 'unchecked' /* * Fetch the icon on every save, not just for new links, so editing a link * picks up a favicon the site has changed. If that fails, an edit keeps * the icon it had. Support links use bundled logos instead. */ const needsIcon = !provider let icon = isNewDestination ? undefined : link?.icon let iconUrl = isNewDestination ? undefined : link?.iconUrl if (needsCheck || needsIcon || isNewDestination) { setReachability('checking') const [meta, uploadedIcon, safelinkAction] = await Promise.all([ needsCheck ? getLinkMeta(targetUrl, 8e3) : undefined, needsIcon ? uploadLinkIcon(targetUrl) : undefined, // unlike reachability, "save anyway" can't skip this isNewDestination ? checkSafelink(targetUrl) : undefined, ]) if (safelinkAction === 'block') { setReachability('unchecked') setError('blocked') ax.metric('profile:links:rejected', {reason: 'safelink'}) return } if (meta?.error) { setReachability('unreachable') return } if (uploadedIcon) { icon = uploadedIcon.icon iconUrl = uploadedIcon.previewUrl } } const saved: ProfileLink = {url: targetUrl} if (nextTitle) saved.title = nextTitle if (icon) saved.icon = icon if (iconUrl) saved.iconUrl = iconUrl // an edit rewrites the same record, so a report on it still points here if (link?.uri) saved.uri = link.uri control.close(() => onSave(saved)) }
const shownError = validation.isInsecure ? 'insecure' : error const errorText = shownError === 'invalid' ? l`That doesn’t look like a link. Try something like yoursite.com.` : shownError === 'blocked' ? l`This link can’t be added. It goes to a site that’s blocked on Blacksky.` : shownError === 'shortener' ? l`Short links can’t be added. Use the full link instead.` : shownError === 'insecure' ? l`Only secure links can be added. Use a link that starts with https://.` : shownError === 'duplicate' ? l`You’ve already added this link.` : reachability === 'unreachable' ? l`We couldn’t reach this link. Check it, or save anyway.` : undefined
// the close button sits as far from the top as the content is from the sides const closeInset = IS_LIQUID_GLASS ? tokens.space._2xl : tokens.space.xl
return ( <Dialog.ScrollableInner style={web({maxWidth: 480})} label={isEditing ? l`Edit link` : l`Add a link`}> <Button label={l`Cancel`} size="small" shape="round" color="secondary" onPress={() => control.close()} style={[a.absolute, a.z_10, {top: closeInset, right: closeInset}]}> <ButtonIcon icon={XIcon} /> </Button> <View style={[a.gap_2xl, a.mt_sm]}> <View style={[a.gap_sm]}> <Text style={[ a.text_2xl, a.font_bold, a.leading_tight, // clear of the close button {paddingRight: 48}, ]}> {isEditing ? <Trans>Edit link</Trans> : <Trans>Add a link</Trans>} </Text> <Text style={[a.text_md, a.leading_snug, t.atoms.text_contrast_high]}> <Trans> Links show as buttons under your bio. Links to support sites like Ko-fi or Patreon show as a green button.{' '} <InlineLinkText label={l`View all support sites`} style={[a.text_md, a.leading_snug]} {...createStaticClick(() => supportedSitesControl.open())}> View all support sites </InlineLinkText> </Trans> </Text> </View>
<View style={[a.gap_lg]}> <View> <TextField.LabelText> <Trans>Link</Trans> </TextField.LabelText> <TextField.Root isInvalid={!!errorText}> <TextField.Icon icon={ChainLinkIcon} /> <Dialog.Input testID="profileLinkUrlInput" label={l`Link`} placeholder="yoursite.com" value={input} onChangeText={value => { setInput(value) setTouched(true) setReachability('unchecked') setError(undefined) }} autoFocus autoCapitalize="none" autoCorrect={false} autoComplete="url" keyboardType="url" returnKeyType="done" onSubmitEditing={() => void save()} /> </TextField.Root> {errorText ? ( <Text style={[a.mt_xs, a.text_sm, {color: t.palette.negative_500}]}> {errorText} </Text> ) : touched && provider ? ( <View style={[a.flex_row, a.align_start, a.gap_xs, a.mt_xs]}> <ProviderLogo provider={provider} size="xs" // sits on the first line of text when the note wraps style={{color: palette.tintText, marginTop: 2}} /> <Text style={[ a.flex_1, a.text_sm, a.leading_snug, {color: palette.tintText}, ]}> <Trans> {provider.name} is a recognized support link. It gets the green support button. </Trans> </Text> </View> ) : null} </View>
<View> <TextField.LabelText> <Trans>Title (optional)</Trans> </TextField.LabelText> <TextField.Root> <Dialog.Input testID="profileLinkTitleInput" label={l`Title`} placeholder={ provider ? l`Support on ${provider.name}` : l`e.g. Portfolio` } value={title} onChangeText={setTitle} maxLength={MAX_TITLE_LENGTH} autoCapitalize="words" returnKeyType="done" onSubmitEditing={() => void save()} /> {title.length >= SHOW_COUNTER_FROM ? ( <TextField.SuffixText label={l`${title.length} out of ${MAX_TITLE_LENGTH} characters`} style={[ a.text_sm, t.atoms.text_contrast_low, {paddingRight: 0}, ]}> {title.length}/{MAX_TITLE_LENGTH} </TextField.SuffixText> ) : null} </TextField.Root> </View> </View>
<View style={[a.gap_sm]}> <Button testID="profileLinkSaveBtn" label={ reachability === 'unreachable' ? l`Save anyway` : isEditing ? l`Save changes` : l`Add link` } onPress={() => void save()} disabled={!canSave} size="large" color="primary" style={[ provider && {backgroundColor: palette.bg}, !canSave && {opacity: 0.5}, ]} hoverStyle={ provider ? {backgroundColor: palette.bgHover} : undefined }> <ButtonText> {reachability === 'unreachable' ? ( <Trans>Save anyway</Trans> ) : isEditing ? ( <Trans>Save changes</Trans> ) : ( <Trans>Add link</Trans> )} </ButtonText> {reachability === 'checking' && <ButtonIcon icon={Loader} />} </Button> {onRemove ? ( <Button testID="profileLinkRemoveBtn" label={l`Remove link`} onPress={() => control.close(onRemove)} size="large" color="negative_subtle"> <ButtonText> <Trans>Remove link</Trans> </ButtonText> </Button> ) : null} </View> </View> <SupportedSitesDialog control={supportedSitesControl} /> </Dialog.ScrollableInner> )}