diff --git a/artifacts/teaboy-os/src/pages/home.tsx b/artifacts/teaboy-os/src/pages/home.tsx index 7c45f1d8..40ade6fe 100644 --- a/artifacts/teaboy-os/src/pages/home.tsx +++ b/artifacts/teaboy-os/src/pages/home.tsx @@ -98,8 +98,8 @@ export default function HomePage() { const [orderedApps, setOrderedApps] = useState(null); useEffect(() => { - if (apps && !updateOrder.isPending) setOrderedApps(apps); - }, [apps, updateOrder.isPending]); + if (apps) setOrderedApps((prev) => prev ?? apps); + }, [apps]); const sensors = useSensors( useSensor(PointerSensor, { activationConstraint: { distance: 8 } }), @@ -112,12 +112,18 @@ export default function HomePage() { const oldIndex = orderedApps.findIndex((a) => a.id === active.id); const newIndex = orderedApps.findIndex((a) => a.id === over.id); if (oldIndex < 0 || newIndex < 0) return; + const previous = orderedApps; const next = arrayMove(orderedApps, oldIndex, newIndex); setOrderedApps(next); updateOrder.mutate( { data: { order: next.map((a) => a.id) } }, { + onSuccess: (data) => { + setOrderedApps(data); + queryClient.setQueryData(getListAppsQueryKey(), data); + }, onError: () => { + setOrderedApps(previous); queryClient.invalidateQueries({ queryKey: getListAppsQueryKey() }); }, },