migrate the notification settings queries to the appview and chat clients
App preferences live on the appview and chat preferences on the chat service, so the combined update mutation holds both clients. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -10,10 +10,10 @@ import {
|
|||||||
useQueryClient,
|
useQueryClient,
|
||||||
} from '@tanstack/react-query'
|
} from '@tanstack/react-query'
|
||||||
|
|
||||||
import {DM_SERVICE_HEADERS} from '#/lib/constants'
|
|
||||||
import {logger} from '#/logger'
|
import {logger} from '#/logger'
|
||||||
import {useAgent} from '#/state/session'
|
import {useAppviewClient, useChatClient} from '#/state/session'
|
||||||
import * as Toast from '#/components/Toast'
|
import * as Toast from '#/components/Toast'
|
||||||
|
import {app, chat} from '#/lexicons'
|
||||||
|
|
||||||
const RQKEY_ROOT = 'notification-settings'
|
const RQKEY_ROOT = 'notification-settings'
|
||||||
const RQKEY_APP = [RQKEY_ROOT, 'app']
|
const RQKEY_APP = [RQKEY_ROOT, 'app']
|
||||||
@@ -67,13 +67,13 @@ type ChatNotificationSettingsUpdate =
|
|||||||
export function useNotificationSettingsQuery({
|
export function useNotificationSettingsQuery({
|
||||||
enabled,
|
enabled,
|
||||||
}: {enabled?: boolean} = {}) {
|
}: {enabled?: boolean} = {}) {
|
||||||
const agent = useAgent()
|
const client = useAppviewClient()
|
||||||
|
|
||||||
return useQuery({
|
return useQuery({
|
||||||
queryKey: RQKEY_APP,
|
queryKey: RQKEY_APP,
|
||||||
queryFn: async (): Promise<AppNotificationSettingsPreferences> => {
|
queryFn: async (): Promise<AppNotificationSettingsPreferences> => {
|
||||||
const res = await agent.app.bsky.notification.getPreferences()
|
const data = await client.call(app.bsky.notification.getPreferences)
|
||||||
return appPreferencesWithoutChat(res.data.preferences)
|
return appPreferencesWithoutChat(data.preferences)
|
||||||
},
|
},
|
||||||
enabled,
|
enabled,
|
||||||
})
|
})
|
||||||
@@ -82,21 +82,24 @@ export function useNotificationSettingsQuery({
|
|||||||
export function useChatNotificationSettingsQuery({
|
export function useChatNotificationSettingsQuery({
|
||||||
enabled,
|
enabled,
|
||||||
}: {enabled?: boolean} = {}) {
|
}: {enabled?: boolean} = {}) {
|
||||||
const agent = useAgent()
|
const client = useChatClient()
|
||||||
|
|
||||||
return useQuery({
|
return useQuery({
|
||||||
queryKey: RQKEY_CHAT,
|
queryKey: RQKEY_CHAT,
|
||||||
queryFn: async (): Promise<ChatNotificationSettingsPreferences> => {
|
queryFn: async (): Promise<ChatNotificationSettingsPreferences> => {
|
||||||
const res = await agent.chat.bsky.notification.getPreferences(undefined, {
|
const data = await client.call(chat.bsky.notification.getPreferences)
|
||||||
headers: DM_SERVICE_HEADERS,
|
return chatPreferencesForSettings(data.preferences)
|
||||||
})
|
|
||||||
return chatPreferencesForSettings(res.data.preferences)
|
|
||||||
},
|
},
|
||||||
enabled,
|
enabled,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
export function useNotificationSettingsUpdateMutation() {
|
export function useNotificationSettingsUpdateMutation() {
|
||||||
const agent = useAgent()
|
/*
|
||||||
|
* App preferences live on the appview and chat preferences on the chat
|
||||||
|
* service, so a combined update fans out over both clients.
|
||||||
|
*/
|
||||||
|
const appviewClient = useAppviewClient()
|
||||||
|
const chatClient = useChatClient()
|
||||||
const queryClient = useQueryClient()
|
const queryClient = useQueryClient()
|
||||||
|
|
||||||
return useMutation({
|
return useMutation({
|
||||||
@@ -104,13 +107,13 @@ export function useNotificationSettingsUpdateMutation() {
|
|||||||
const {appUpdate, chatUpdate} = splitNotificationSettingsUpdate(update)
|
const {appUpdate, chatUpdate} = splitNotificationSettingsUpdate(update)
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
hasUpdates(appUpdate)
|
hasUpdates(appUpdate)
|
||||||
? agent.app.bsky.notification.putPreferencesV2(appUpdate)
|
? appviewClient.call(
|
||||||
|
app.bsky.notification.putPreferencesV2,
|
||||||
|
appUpdate,
|
||||||
|
)
|
||||||
: undefined,
|
: undefined,
|
||||||
hasUpdates(chatUpdate)
|
hasUpdates(chatUpdate)
|
||||||
? agent.chat.bsky.notification.putPreferences(chatUpdate, {
|
? chatClient.call(chat.bsky.notification.putPreferences, chatUpdate)
|
||||||
headers: DM_SERVICE_HEADERS,
|
|
||||||
encoding: 'application/json',
|
|
||||||
})
|
|
||||||
: undefined,
|
: undefined,
|
||||||
])
|
])
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user