-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* new settings screen * bring back the spinner * add experimental language * fix typo, change leading * integrate priority notifications API * update package * use refetch instead of invalidateQueries * fix read-after-write issue by polling for update * add spinner for initial load * rm onmutate, it's overcomplicated * set error state eagerly * Change language in description Co-authored-by: Hailey <[email protected]> * prettier * add `Toggle.Platform` * extract out mutation hook + error state * rm useless cache mutation * disambiguate isError and isPending * rm unused isError --------- Co-authored-by: Samuel Newman <[email protected]> Co-authored-by: Hailey <[email protected]>
- Loading branch information
1 parent
9bd8393
commit cfb8a31
Showing
20 changed files
with
305 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import {msg} from '@lingui/macro' | ||
import {useLingui} from '@lingui/react' | ||
import {useMutation, useQueryClient} from '@tanstack/react-query' | ||
|
||
import {until} from '#/lib/async/until' | ||
import {logger} from '#/logger' | ||
import {RQKEY as RQKEY_NOTIFS} from '#/state/queries/notifications/feed' | ||
import {useAgent} from '#/state/session' | ||
import * as Toast from '#/view/com/util/Toast' | ||
|
||
export function useNotificationsSettingsMutation() { | ||
const {_} = useLingui() | ||
const agent = useAgent() | ||
const queryClient = useQueryClient() | ||
|
||
return useMutation({ | ||
mutationFn: async (keys: string[]) => { | ||
const enabled = keys[0] === 'enabled' | ||
|
||
await agent.api.app.bsky.notification.putPreferences({ | ||
priority: enabled, | ||
}) | ||
|
||
await until( | ||
5, // 5 tries | ||
1e3, // 1s delay between tries | ||
res => res.data.priority === enabled, | ||
() => agent.api.app.bsky.notification.listNotifications({limit: 1}), | ||
) | ||
|
||
eagerlySetCachedPriority(queryClient, enabled) | ||
}, | ||
onError: err => { | ||
logger.error('Failed to save notification preferences', { | ||
safeMessage: err, | ||
}) | ||
Toast.show( | ||
_(msg`Failed to save notification preferences, please try again`), | ||
'xmark', | ||
) | ||
}, | ||
onSuccess: () => { | ||
Toast.show(_(msg`Preference saved`)) | ||
}, | ||
onSettled: () => { | ||
queryClient.invalidateQueries({queryKey: RQKEY_NOTIFS()}) | ||
}, | ||
}) | ||
} | ||
|
||
function eagerlySetCachedPriority( | ||
queryClient: ReturnType<typeof useQueryClient>, | ||
enabled: boolean, | ||
) { | ||
queryClient.setQueryData(RQKEY_NOTIFS(), (old: any) => { | ||
if (!old) return old | ||
return { | ||
...old, | ||
pages: old.pages.map((page: any) => { | ||
return { | ||
...page, | ||
priority: enabled, | ||
} | ||
}), | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.