Simplify undo logic

This commit is contained in:
Eric Bailey
2026-01-14 09:28:10 -06:00
parent 26399b27d0
commit 76af7566b6
2 changed files with 40 additions and 21 deletions
@@ -1,4 +1,3 @@
import {useRef} from 'react'
import {View} from 'react-native' import {View} from 'react-native'
import {msg, Trans} from '@lingui/macro' import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react' import {useLingui} from '@lingui/react'
@@ -12,10 +11,7 @@ import * as Dialog from '#/components/Dialog'
import {Loader} from '#/components/Loader' import {Loader} from '#/components/Loader'
import * as toast from '#/components/Toast' import * as toast from '#/components/Toast'
import {Span, Text} from '#/components/Typography' import {Span, Text} from '#/components/Typography'
import { import {useUpdateLiveEventPreferences} from '#/features/liveEvents/preferences'
type LiveEventPreferencesAction,
useUpdateLiveEventPreferences,
} from '#/features/liveEvents/preferences'
import { import {
type LiveEventFeed, type LiveEventFeed,
type LiveEventFeedMetricContext, type LiveEventFeedMetricContext,
@@ -56,8 +52,6 @@ function Inner({
metricContext: LiveEventFeedMetricContext metricContext: LiveEventFeedMetricContext
}) { }) {
const {_} = useLingui() const {_} = useLingui()
const canUndo = useRef(true)
const undoAction = useRef<LiveEventPreferencesAction | null>(null)
const { const {
isPending, isPending,
mutate: update, mutate: update,
@@ -66,19 +60,19 @@ function Inner({
} = useUpdateLiveEventPreferences({ } = useUpdateLiveEventPreferences({
feed, feed,
metricContext, metricContext,
onSuccess() { onUpdateSuccess({undoAction}) {
toast.show( toast.show(
<toast.Outer> <toast.Outer>
<toast.Icon /> <toast.Icon />
<toast.Text> <toast.Text>
<Trans>Your live event preferences have been updated.</Trans> <Trans>Your live event preferences have been updated.</Trans>
</toast.Text> </toast.Text>
{canUndo.current && undoAction.current && ( {undoAction && (
<toast.Action <toast.Action
label={_(msg`Undo`)} label={_(msg`Undo`)}
onPress={() => { onPress={() => {
if (undoAction.current) { if (undoAction) {
update(undoAction.current) update(undoAction)
} }
}}> }}>
<Trans>Undo</Trans> <Trans>Undo</Trans>
@@ -91,8 +85,7 @@ function Inner({
) )
// must protect to avoid closing an already closed dialog // must protect to avoid closing an already closed dialog
if (canUndo.current) { if (undoAction) {
canUndo.current = false
control.close() control.close()
} }
}, },
@@ -133,7 +126,6 @@ function Inner({
color="primary_subtle" color="primary_subtle"
onPress={() => { onPress={() => {
update({type: 'hideFeed', id: feed.id}) update({type: 'hideFeed', id: feed.id})
undoAction.current = {type: 'unhideFeed', id: feed.id}
}}> }}>
<ButtonText> <ButtonText>
<Trans>Hide this event</Trans> <Trans>Hide this event</Trans>
@@ -146,7 +138,6 @@ function Inner({
color="secondary" color="secondary"
onPress={() => { onPress={() => {
update({type: 'toggleHideAllFeeds'}) update({type: 'toggleHideAllFeeds'})
undoAction.current = {type: 'toggleHideAllFeeds'}
}}> }}>
<ButtonText> <ButtonText>
<Trans>Hide all events</Trans> <Trans>Hide all events</Trans>
+34 -6
View File
@@ -17,7 +17,12 @@ import {
export type LiveEventPreferencesAction = Parameters< export type LiveEventPreferencesAction = Parameters<
Agent['updateLiveEventPreferences'] Agent['updateLiveEventPreferences']
>[0] >[0] & {
/**
* Flag that is internal to this hook, do not set when updating prefs
*/
__canUndo?: boolean
}
export function useLiveEventPreferences() { export function useLiveEventPreferences() {
const query = usePreferencesQuery() const query = usePreferencesQuery()
@@ -54,8 +59,9 @@ function useWebOnlyDebugLiveEventPreferences() {
export function useUpdateLiveEventPreferences(props: { export function useUpdateLiveEventPreferences(props: {
feed?: LiveEventFeed feed?: LiveEventFeed
metricContext: LiveEventFeedMetricContext metricContext: LiveEventFeedMetricContext
onSuccess?: () => void onUpdateSuccess?: (props: {
onError?: (error: Error) => void undoAction: LiveEventPreferencesAction | null
}) => void
}) { }) {
const queryClient = useQueryClient() const queryClient = useQueryClient()
const agent = useAgent() const agent = useAgent()
@@ -63,10 +69,32 @@ export function useUpdateLiveEventPreferences(props: {
return useMutation< return useMutation<
AppBskyActorDefs.LiveEventPreferences, AppBskyActorDefs.LiveEventPreferences,
Error, Error,
LiveEventPreferencesAction LiveEventPreferencesAction,
{undoAction: LiveEventPreferencesAction | null}
>({ >({
onError: props?.onError, onSettled(data, error, variables) {
onSuccess: props?.onSuccess, // If __canUndo is not explicitly set to false, we allow undo
const canUndo = variables.__canUndo === undefined ? true : false
let undoAction: LiveEventPreferencesAction | null = null
switch (variables.type) {
case 'hideFeed':
undoAction = {type: 'unhideFeed', id: variables.id, __canUndo: false}
break
case 'unhideFeed':
undoAction = {type: 'hideFeed', id: variables.id, __canUndo: false}
break
case 'toggleHideAllFeeds':
undoAction = {type: 'toggleHideAllFeeds', __canUndo: false}
break
}
if (data && !error) {
props?.onUpdateSuccess?.({
undoAction: canUndo ? undoAction : null,
})
}
},
mutationFn: async action => { mutationFn: async action => {
const updated = await agent.updateLiveEventPreferences(action) const updated = await agent.updateLiveEventPreferences(action)