diff --git a/src/screens/Signup/__tests__/handleDomains.test.ts b/src/screens/Signup/__tests__/handleDomains.test.ts new file mode 100644 index 000000000..d75f670e1 --- /dev/null +++ b/src/screens/Signup/__tests__/handleDomains.test.ts @@ -0,0 +1,48 @@ +import {filterUserDomains} from '#/screens/Signup/handleDomains' + +const BLACKSKY_DOMAINS = [ + '.myatproto.social', + '.blacksky.app', + '.cryptoanarchy.network', + '.latinsky.app', + '.afrolatinsky.app', +] + +describe('filterUserDomains', () => { + it("restricts domains to the selected community's handles", () => { + expect( + filterUserDomains(BLACKSKY_DOMAINS, [ + '.latinsky.app', + '.afrolatinsky.app', + ]), + ).toEqual(['.latinsky.app', '.afrolatinsky.app']) + }) + + it('puts a community domain first so it becomes the default suffix', () => { + const [first] = filterUserDomains(BLACKSKY_DOMAINS, ['.latinsky.app']) + expect(first).toBe('.latinsky.app') + }) + + it('shows every advertised domain when the community config is missing', () => { + expect(filterUserDomains(BLACKSKY_DOMAINS, undefined)).toEqual( + BLACKSKY_DOMAINS, + ) + }) + + it('shows every advertised domain when the community lists none', () => { + expect(filterUserDomains(BLACKSKY_DOMAINS, [])).toEqual(BLACKSKY_DOMAINS) + }) + + it('ignores community handles the PDS does not advertise', () => { + expect( + filterUserDomains( + ['.medsky.network', '.nursesky.network'], + ['.medsky.app', '.medsky.network'], + ), + ).toEqual(['.medsky.network']) + }) + + it('returns nothing when no advertised domain matches the community', () => { + expect(filterUserDomains(['.blacksky.app'], ['.latinsky.app'])).toEqual([]) + }) +}) diff --git a/src/screens/Signup/handleDomains.ts b/src/screens/Signup/handleDomains.ts new file mode 100644 index 000000000..cb6e1e88f --- /dev/null +++ b/src/screens/Signup/handleDomains.ts @@ -0,0 +1,17 @@ +/** + * Narrow the domains a PDS advertises down to the ones the selected community + * publishes. + * + * An empty or missing `allowed` means "no community restriction known" — the + * community's config hasn't loaded or couldn't be fetched — so every advertised + * domain stays selectable. Signup continuing with an extra domain on offer + * beats blocking it on a brand-service failure. + */ +export function filterUserDomains( + domains: string[], + allowed?: string[], +): string[] { + if (!allowed || allowed.length === 0) return domains + + return domains.filter(domain => allowed.includes(domain)) +} diff --git a/src/screens/Signup/index.tsx b/src/screens/Signup/index.tsx index 011c26442..0812e6595 100644 --- a/src/screens/Signup/index.tsx +++ b/src/screens/Signup/index.tsx @@ -6,8 +6,10 @@ import {AppBskyGraphStarterpack} from '@atproto/api' import {msg} from '@lingui/core/macro' import {useLingui} from '@lingui/react' import {Trans} from '@lingui/react/macro' +import {useQuery} from '@tanstack/react-query' -import {useBrand} from '#/lib/community/BrandContext' +import {DEFAULT_BRAND_CONFIG, useBrand} from '#/lib/community/BrandContext' +import {fetchBrandBySlug} from '#/lib/community/resolveBrand' import {FEEDBACK_FORM_URL} from '#/lib/constants' import {logger} from '#/logger' import {useServiceQuery} from '#/state/queries/service' @@ -98,6 +100,30 @@ export function Signup({ refetch, } = useServiceQuery(state.serviceUrl) + /* + * The picked community's handle domains live in its published brand config, + * not in the ambient brand — which native signup pins to the bundled Blacksky + * copy while logged out. Fetch the live config for whichever community is + * selected (Blacksky included, since the bundle can be stale). + */ + const communitySlug = + state.selectedBrandSlug ?? DEFAULT_BRAND_CONFIG.metadata.slug + const {data: communityConfig} = useQuery({ + queryKey: ['signup-brand-config', communitySlug], + queryFn: () => fetchBrandBySlug(communitySlug), + staleTime: 5 * 60 * 1000, + }) + + /* + * On a failed or pending fetch this falls back to the ambient brand: correct + * on web (injected by hostname) and empty on native, which leaves every + * domain the PDS advertises selectable. Showing an extra domain beats + * blocking signup on a brand-service blip. + */ + const availableHandles = + communityConfig?.services.pds.availableHandles ?? + brand.services.pds.availableHandles + useEffect(() => { if (isFetching) { dispatch({type: 'setIsLoading', value: true}) @@ -111,7 +137,7 @@ export function Signup({ dispatch({ type: 'setServiceDescription', value: undefined, - availableHandles: brand.services.pds.availableHandles, + availableHandles, }) dispatch({ type: 'setError', @@ -123,11 +149,11 @@ export function Signup({ dispatch({ type: 'setServiceDescription', value: serviceInfo, - availableHandles: brand.services.pds.availableHandles, + availableHandles, }) dispatch({type: 'setError', value: ''}) } - }, [_, serviceInfo, isError, brand.services.pds.availableHandles]) + }, [_, serviceInfo, isError, availableHandles]) useEffect(() => { if (state.pendingSubmit) { diff --git a/src/screens/Signup/state.ts b/src/screens/Signup/state.ts index ffc2ac1d1..a1ce75952 100644 --- a/src/screens/Signup/state.ts +++ b/src/screens/Signup/state.ts @@ -13,6 +13,7 @@ import {createFullHandle} from '#/lib/strings/handles' import {getAge} from '#/lib/strings/time' import {useSessionApi} from '#/state/session' import {useOnboardingDispatch} from '#/state/shell' +import {filterUserDomains} from '#/screens/Signup/handleDomains' import {type AnalyticsContextType, useAnalytics} from '#/analytics' export type ServiceDescription = ComAtprotoServerDescribeServer.OutputSchema @@ -192,11 +193,7 @@ export function reducer(s: SignupState, a: SignupAction): SignupState { LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut) const domains = a.value?.availableUserDomains ?? [] - const allowed = a.availableHandles - const filtered = - allowed && allowed.length > 0 - ? domains.filter(d => allowed.includes(d)) - : domains + const filtered = filterUserDomains(domains, a.availableHandles) next.serviceDescription = a.value ? {...a.value, availableUserDomains: filtered}