feat: Update HTML title on web #626 #599 (#655)

For any `Screen` that shows on desktop, `title` is "(1) ... - Bluesky"
where "(1)" is the unread notification count.

The titles are unlocalized and the string "Bluesky" is hardcoded,
following the pattern of the rest of the app.

Display names and post content are loaded into the title as effects.

Tested:
* all screens
* screen changes / component mounts/unmounts
* long posts with links and images
* display name set/unset
* spamming myself with notifications, clearing notifications
* /profile/did:... links
* lint (only my changed files), jest, e2e.

New utilities: `useUnreadCountLabel`, `bskyTitle`,
`combinedDisplayName`, `useSetTitle`.

resolves: #626 #599
This commit is contained in:
LW 2023-05-16 11:13:05 -07:00 committed by GitHub
parent a5838694bd
commit 50c1841a06
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 180 additions and 21 deletions

View file

@ -0,0 +1,16 @@
import {useEffect} from 'react'
import {useNavigation} from '@react-navigation/native'
import {NavigationProp} from 'lib/routes/types'
import {bskyTitle} from 'lib/strings/headings'
import {useUnreadCountLabel} from './useUnreadCountLabel'
export function useSetTitle(title?: string) {
const navigation = useNavigation<NavigationProp>()
const unreadCountLabel = useUnreadCountLabel()
useEffect(() => {
if (title) {
navigation.setOptions({title: bskyTitle(title, unreadCountLabel)})
}
}, [title, navigation, unreadCountLabel])
}

View file

@ -0,0 +1,19 @@
import {useEffect, useReducer} from 'react'
import {DeviceEventEmitter} from 'react-native'
import {useStores} from 'state/index'
export function useUnreadCountLabel() {
// HACK: We don't have anything like Redux selectors,
// and we don't want to use <RootStoreContext.Consumer />
// to react to the whole store
const [, forceUpdate] = useReducer(x => x + 1, 0)
useEffect(() => {
const subscription = DeviceEventEmitter.addListener(
'unread-notifications',
forceUpdate,
)
return () => subscription?.remove()
}, [forceUpdate])
return useStores().me.notifications.unreadCountLabel
}

View file

@ -10,3 +10,18 @@ export function sanitizeDisplayName(str: string): string {
}
return ''
}
export function combinedDisplayName({
handle,
displayName,
}: {
handle?: string
displayName?: string
}): string {
if (!handle) {
return ''
}
return displayName
? `${sanitizeDisplayName(displayName)} (@${handle})`
: `@${handle}`
}

View file

@ -0,0 +1,4 @@
export function bskyTitle(page: string, unreadCountLabel?: string) {
const unreadPrefix = unreadCountLabel ? `(${unreadCountLabel}) ` : ''
return `${unreadPrefix}${page} - Bluesky`
}