From a2ca7db052c0ce20b404f6f81b691850b3318b97 Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Wed, 13 Mar 2024 14:33:54 +0000 Subject: mobile: Optimistic UI updates on actions --- .../mobile/components/bookmarks/BookmarkCard.tsx | 51 ++++-- .../mobile/components/bookmarks/BookmarkList.tsx | 23 +-- packages/mobile/components/ui/ActionButton.tsx | 21 +++ packages/mobile/components/ui/Toast.tsx | 183 +++++++++++++++++++++ packages/mobile/lib/providers.tsx | 6 +- 5 files changed, 258 insertions(+), 26 deletions(-) create mode 100644 packages/mobile/components/ui/ActionButton.tsx create mode 100644 packages/mobile/components/ui/Toast.tsx (limited to 'packages') diff --git a/packages/mobile/components/bookmarks/BookmarkCard.tsx b/packages/mobile/components/bookmarks/BookmarkCard.tsx index ae0d8a33..b3ddf302 100644 --- a/packages/mobile/components/bookmarks/BookmarkCard.tsx +++ b/packages/mobile/components/bookmarks/BookmarkCard.tsx @@ -1,10 +1,12 @@ import { ZBookmark } from "@hoarder/trpc/types/bookmarks"; import * as WebBrowser from "expo-web-browser"; -import { Star, Archive, Trash } from "lucide-react-native"; +import { Star, Archive, Trash, ArchiveRestore } from "lucide-react-native"; import { View, Text, Image, ScrollView, Pressable } from "react-native"; import Markdown from "react-native-markdown-display"; +import { ActionButton } from "../ui/ActionButton"; import { Skeleton } from "../ui/Skeleton"; +import { useToast } from "../ui/Toast"; import { api } from "@/lib/trpc"; @@ -30,20 +32,39 @@ export function isBookmarkStillLoading(bookmark: ZBookmark) { } function ActionBar({ bookmark }: { bookmark: ZBookmark }) { + const { toast } = useToast(); const apiUtils = api.useUtils(); - const { mutate: deleteBookmark } = api.bookmarks.deleteBookmark.useMutation({ - onSuccess: () => { - apiUtils.bookmarks.getBookmarks.invalidate(); - }, - }); - const { mutate: updateBookmark, variables } = - api.bookmarks.updateBookmark.useMutation({ + const { mutate: deleteBookmark, isPending: isDeletionPending } = + api.bookmarks.deleteBookmark.useMutation({ onSuccess: () => { apiUtils.bookmarks.getBookmarks.invalidate(); - apiUtils.bookmarks.getBookmark.invalidate({ bookmarkId: bookmark.id }); + }, + onError: () => { + toast({ + message: "Something went wrong", + variant: "destructive", + showProgress: false, + }); }, }); + const { + mutate: updateBookmark, + variables, + isPending: isUpdatePending, + } = api.bookmarks.updateBookmark.useMutation({ + onSuccess: () => { + apiUtils.bookmarks.getBookmarks.invalidate(); + apiUtils.bookmarks.getBookmark.invalidate({ bookmarkId: bookmark.id }); + }, + onError: () => { + toast({ + message: "Something went wrong", + variant: "destructive", + showProgress: false, + }); + }, + }); return ( @@ -61,7 +82,8 @@ function ActionBar({ bookmark }: { bookmark: ZBookmark }) { )} - updateBookmark({ bookmarkId: bookmark.id, @@ -69,9 +91,10 @@ function ActionBar({ bookmark }: { bookmark: ZBookmark }) { }) } > - - - : } + + deleteBookmark({ bookmarkId: bookmark.id, @@ -79,7 +102,7 @@ function ActionBar({ bookmark }: { bookmark: ZBookmark }) { } > - + ); } diff --git a/packages/mobile/components/bookmarks/BookmarkList.tsx b/packages/mobile/components/bookmarks/BookmarkList.tsx index 519ec47c..af5c9de7 100644 --- a/packages/mobile/components/bookmarks/BookmarkList.tsx +++ b/packages/mobile/components/bookmarks/BookmarkList.tsx @@ -1,5 +1,6 @@ import { useEffect, useState } from "react"; -import { FlatList, Text, View } from "react-native"; +import { Text, View } from "react-native"; +import Animated, { LinearTransition } from "react-native-reanimated"; import BookmarkCard from "./BookmarkCard"; @@ -37,25 +38,25 @@ export default function BookmarkList({ apiUtils.bookmarks.getBookmark.invalidate(); }; - if (!data.bookmarks.length) { - return ( - - No Bookmarks - - ); - } - return ( - } + renderItem={(b) => } + ListEmptyComponent={ + + No Bookmarks + + } data={data.bookmarks} refreshing={refreshing} onRefresh={onRefresh} + keyExtractor={(b) => b.id} /> ); } diff --git a/packages/mobile/components/ui/ActionButton.tsx b/packages/mobile/components/ui/ActionButton.tsx new file mode 100644 index 00000000..c51eb332 --- /dev/null +++ b/packages/mobile/components/ui/ActionButton.tsx @@ -0,0 +1,21 @@ +import { ActivityIndicator, Pressable, PressableProps } from "react-native"; + +export function ActionButton({ + children, + loading, + disabled, + ...props +}: PressableProps & { + loading: boolean; +}) { + if (disabled !== undefined) { + disabled ||= loading; + } else if (loading) { + disabled = true; + } + return ( + + {loading ? : children} + + ); +} diff --git a/packages/mobile/components/ui/Toast.tsx b/packages/mobile/components/ui/Toast.tsx new file mode 100644 index 00000000..fb319f84 --- /dev/null +++ b/packages/mobile/components/ui/Toast.tsx @@ -0,0 +1,183 @@ +import { createContext, useContext, useEffect, useRef, useState } from "react"; +import { Animated, Text, View } from "react-native"; + +import { cn } from "@/lib/utils"; + +const toastVariants = { + default: "bg-foreground", + destructive: "bg-destructive", + success: "bg-green-500", + info: "bg-blue-500", +}; + +interface ToastProps { + id: number; + message: string; + onHide: (id: number) => void; + variant?: keyof typeof toastVariants; + duration?: number; + showProgress?: boolean; +} +function Toast({ + id, + message, + onHide, + variant = "default", + duration = 3000, + showProgress = true, +}: ToastProps) { + const opacity = useRef(new Animated.Value(0)).current; + const progress = useRef(new Animated.Value(0)).current; + + useEffect(() => { + Animated.sequence([ + Animated.timing(opacity, { + toValue: 1, + duration: 500, + useNativeDriver: true, + }), + Animated.timing(progress, { + toValue: 1, + duration: duration - 1000, + useNativeDriver: false, + }), + Animated.timing(opacity, { + toValue: 0, + duration: 500, + useNativeDriver: true, + }), + ]).start(() => onHide(id)); + }, [duration]); + + return ( + + {message} + {showProgress && ( + + + + )} + + ); +} + +type ToastVariant = keyof typeof toastVariants; + +interface ToastMessage { + id: number; + text: string; + variant: ToastVariant; + duration?: number; + position?: string; + showProgress?: boolean; +} +interface ToastContextProps { + toast: (t: { + message: string; + variant?: keyof typeof toastVariants; + duration?: number; + position?: "top" | "bottom"; + showProgress?: boolean; + }) => void; + removeToast: (id: number) => void; +} +const ToastContext = createContext(undefined); + +// TODO: refactor to pass position to Toast instead of ToastProvider +function ToastProvider({ + children, + position = "top", +}: { + children: React.ReactNode; + position?: "top" | "bottom"; +}) { + const [messages, setMessages] = useState([]); + + const toast: ToastContextProps["toast"] = ({ + message, + variant = "default", + duration = 3000, + position = "top", + showProgress = true, + }: { + message: string; + variant?: ToastVariant; + duration?: number; + position?: "top" | "bottom"; + showProgress?: boolean; + }) => { + setMessages((prev) => [ + ...prev, + { + id: Date.now(), + text: message, + variant, + duration, + position, + showProgress, + }, + ]); + }; + + const removeToast = (id: number) => { + setMessages((prev) => prev.filter((message) => message.id !== id)); + }; + + return ( + + {children} + + {messages.map((message) => ( + + ))} + + + ); +} + +function useToast() { + const context = useContext(ToastContext); + if (!context) { + throw new Error("useToast must be used within ToastProvider"); + } + return context; +} + +export { ToastProvider, ToastVariant, Toast, toastVariants, useToast }; diff --git a/packages/mobile/lib/providers.tsx b/packages/mobile/lib/providers.tsx index 394bad28..1717afb2 100644 --- a/packages/mobile/lib/providers.tsx +++ b/packages/mobile/lib/providers.tsx @@ -6,6 +6,8 @@ import superjson from "superjson"; import useAppSettings, { getAppSettings } from "./settings"; import { api } from "./trpc"; +import { ToastProvider } from "@/components/ui/Toast"; + function getTRPCClient(address: string) { return api.createClient({ links: [ @@ -44,7 +46,9 @@ export function Providers({ children }: { children: React.ReactNode }) { client={trpcClient} queryClient={queryClient} > - {children} + + {children} + ); } -- cgit v1.2.3-70-g09d2