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:
@@ -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])
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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}`
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
export function bskyTitle(page: string, unreadCountLabel?: string) {
|
||||
const unreadPrefix = unreadCountLabel ? `(${unreadCountLabel}) ` : ''
|
||||
return `${unreadPrefix}${page} - Bluesky`
|
||||
}
|
||||
Reference in New Issue
Block a user