diff --git a/package.json b/package.json index b58be6591..73a583cdb 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,7 @@ "idb-keyval": "6.2.1", "immer": "10.1.1", "jotai": "2.8.2", - "jotai-dark": "0.3.1", + "jotai-dark": "0.3.2", "jotai-effect": "1.0.0", "lethargy": "1.0.9", "lodash-es": "4.17.21", @@ -75,6 +75,7 @@ "rehype-parse": "9.0.0", "rehype-sanitize": "6.0.0", "rehype-stringify": "10.0.0", + "sonner": "^1.5.0", "swiper": "11.1.4", "tailwind-merge": "2.3.0", "tailwindcss-animate": "1.0.7", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 00114646d..e8d59d2d1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -125,8 +125,8 @@ importers: specifier: 2.8.2 version: 2.8.2(@types/react@18.3.3)(react@18.3.1) jotai-dark: - specifier: 0.3.1 - version: 0.3.1(jotai-effect@1.0.0(jotai@2.8.2(@types/react@18.3.3)(react@18.3.1)))(jotai@2.8.2(@types/react@18.3.3)(react@18.3.1))(react@18.3.1) + specifier: 0.3.2 + version: 0.3.2(jotai-effect@1.0.0(jotai@2.8.2(@types/react@18.3.3)(react@18.3.1)))(jotai@2.8.2(@types/react@18.3.3)(react@18.3.1))(react@18.3.1) jotai-effect: specifier: 1.0.0 version: 1.0.0(jotai@2.8.2(@types/react@18.3.3)(react@18.3.1)) @@ -169,6 +169,9 @@ importers: rehype-stringify: specifier: 10.0.0 version: 10.0.0 + sonner: + specifier: ^1.5.0 + version: 1.5.0(react-dom@18.3.1(react@18.3.1))(react@18.3.1) swiper: specifier: 11.1.4 version: 11.1.4 @@ -3482,16 +3485,13 @@ packages: jose@5.3.0: resolution: {integrity: sha512-IChe9AtAE79ru084ow8jzkN2lNrG3Ntfiv65Cvj9uOCE2m5LNsdHG+9EbxWxAoWRF9TgDOqLN5jm08++owDVRg==} - jotai-dark@0.3.1: - resolution: {integrity: sha512-6yW4qk+xG9kv/YkDr4aCWHMPQZHd+q/p1orFzg5NFJwlBCfK/UBlUJfPTRLMdbMf1+U5u4pBQWPGa7ZHOqSFBQ==} + jotai-dark@0.3.2: + resolution: {integrity: sha512-bcxbfze/lQPC6Q7NmceMKmHBkW0ei1wbhEj5Fg0VebQMBeTYkqlzIPeLz8MEsRup/AiKN8mrwNbHLBh5456Yww==} peerDependencies: - foxact: ^0.2.33 jotai: '>=2.5.0' jotai-effect: '*' - react: ^18.2.0 + react: '>=18.2.0' peerDependenciesMeta: - foxact: - optional: true jotai: optional: true jotai-effect: @@ -4612,6 +4612,12 @@ packages: resolution: {integrity: sha512-l5x7VUUWbjVFbafGLxPWkYsHIhEvmF85tbIeFZWc8ZPtoMyybuEhL7Jye/ooC4/d48FgOjSJXgsF/AJPYCW8Zw==} engines: {node: '>= 10.0.0', npm: '>= 3.0.0'} + sonner@1.5.0: + resolution: {integrity: sha512-FBjhG/gnnbN6FY0jaNnqZOMmB73R+5IiyYAw8yBj7L54ER7HB3fOSE5OFiQiE2iXWxeXKvg6fIP4LtVppHEdJA==} + peerDependencies: + react: ^18.0.0 + react-dom: ^18.0.0 + sort-object-keys@1.1.3: resolution: {integrity: sha512-855pvK+VkU7PaKYPc+Jjnmt4EzejQHyhhF33q31qG8x7maDzkeFhAAThdCYay11CISO+qAMwjOBP+fPZe0IPyg==} @@ -8959,7 +8965,7 @@ snapshots: jose@5.3.0: {} - jotai-dark@0.3.1(jotai-effect@1.0.0(jotai@2.8.2(@types/react@18.3.3)(react@18.3.1)))(jotai@2.8.2(@types/react@18.3.3)(react@18.3.1))(react@18.3.1): + jotai-dark@0.3.2(jotai-effect@1.0.0(jotai@2.8.2(@types/react@18.3.3)(react@18.3.1)))(jotai@2.8.2(@types/react@18.3.3)(react@18.3.1))(react@18.3.1): optionalDependencies: jotai: 2.8.2(@types/react@18.3.3)(react@18.3.1) jotai-effect: 1.0.0(jotai@2.8.2(@types/react@18.3.3)(react@18.3.1)) @@ -10203,6 +10209,11 @@ snapshots: ip-address: 9.0.5 smart-buffer: 4.2.0 + sonner@1.5.0(react-dom@18.3.1(react@18.3.1))(react@18.3.1): + dependencies: + react: 18.3.1 + react-dom: 18.3.1(react@18.3.1) + sort-object-keys@1.1.3: {} sort-package-json@1.57.0: diff --git a/src/renderer/src/components/feed-column/item.tsx b/src/renderer/src/components/feed-column/item.tsx index 077afdaa6..a9beef486 100644 --- a/src/renderer/src/components/feed-column/item.tsx +++ b/src/renderer/src/components/feed-column/item.tsx @@ -1,5 +1,4 @@ import { FeedIcon } from "@renderer/components/feed-icon" -import { ToastAction } from "@renderer/components/ui/toast" import { Tooltip, TooltipContent, @@ -7,7 +6,6 @@ import { TooltipProvider, TooltipTrigger, } from "@renderer/components/ui/tooltip" -import { useToast } from "@renderer/components/ui/use-toast" import { apiClient } from "@renderer/lib/api-fetch" import { levels } from "@renderer/lib/constants" import dayjs from "@renderer/lib/dayjs" @@ -21,6 +19,7 @@ import { useUnreadStore, } from "@renderer/store" import { useMutation } from "@tanstack/react-query" +import { toast } from "sonner" export function FeedItem({ feed, @@ -45,8 +44,6 @@ export function FeedItem({ }) } - const { toast } = useToast() - const deleteMutation = useMutation({ mutationFn: async (feed: SubscriptionResponse[number]) => apiClient.subscriptions.$delete({ @@ -58,21 +55,19 @@ export function FeedItem({ onSuccess: (_, variables) => { Queries.subscription.byView(variables.view).invalidate() - toast({ - duration: 3000, - description: ( - <> - Feed - {" "} - {variables.feeds.title} - {" "} - has been unfollowed. - - ), - action: ( - { + toast( + <> + Feed + {" "} + {variables.feeds.title} + {" "} + has been unfollowed. + , + { + duration: 3000, + action: { + label: "Undo", + onClick: async () => { await apiClient.subscriptions.$post({ json: { url: variables.feeds.url, @@ -83,12 +78,10 @@ export function FeedItem({ }) Queries.subscription.byView(feed.view).invalidate() - }} - > - Undo - - ), - }) + }, + }, + }, + ) }, }) diff --git a/src/renderer/src/components/ui/image.tsx b/src/renderer/src/components/ui/image.tsx index bc8d379c8..5260ebf45 100644 --- a/src/renderer/src/components/ui/image.tsx +++ b/src/renderer/src/components/ui/image.tsx @@ -1,10 +1,10 @@ -import { useToast } from "@renderer/components/ui/use-toast" import { client } from "@renderer/lib/client" import { getProxyUrl } from "@renderer/lib/img-proxy" import { showNativeMenu } from "@renderer/lib/native-menu" import { cn } from "@renderer/lib/utils" import type { FC, ImgHTMLAttributes } from "react" import { memo, useState } from "react" +import { toast } from "sonner" import { useEventCallback } from "usehooks-ts" import { usePreviewImages } from "./image/hooks" @@ -57,7 +57,6 @@ const ImageImpl: FC = ({ props.onClick?.(e) }, ) - const { toast } = useToast() return (
@@ -92,9 +91,8 @@ const ImageImpl: FC = ({ click: () => { if (props.src) { navigator.clipboard.writeText(props.src) - toast({ + toast("Address copied to clipboard.", { duration: 1000, - description: "Address copied to clipboard.", }) } }, diff --git a/src/renderer/src/components/ui/sonner.tsx b/src/renderer/src/components/ui/sonner.tsx new file mode 100644 index 000000000..cb948ef68 --- /dev/null +++ b/src/renderer/src/components/ui/sonner.tsx @@ -0,0 +1,26 @@ +import { useDark } from "@renderer/hooks/useDark" +import { Toaster as Sonner } from "sonner" + +type ToasterProps = React.ComponentProps + +const Toaster = ({ ...props }: ToasterProps) => { + const { theme } = useDark() + + return ( + + ) +} + +export { Toaster } diff --git a/src/renderer/src/components/ui/toast.tsx b/src/renderer/src/components/ui/toast.tsx deleted file mode 100644 index 56d51c4d8..000000000 --- a/src/renderer/src/components/ui/toast.tsx +++ /dev/null @@ -1,125 +0,0 @@ -import * as ToastPrimitives from "@radix-ui/react-toast" -import { cn } from "@renderer/lib/utils" -import type { VariantProps } from "class-variance-authority" -import { cva } from "class-variance-authority" -import { X } from "lucide-react" -import * as React from "react" - -const ToastProvider = ToastPrimitives.Provider - -const ToastViewport = React.forwardRef< - React.ElementRef, - React.ComponentPropsWithoutRef ->(({ className, ...props }, ref) => ( - -)) -ToastViewport.displayName = ToastPrimitives.Viewport.displayName - -const toastVariants = cva( - "group pointer-events-auto relative flex w-full items-center justify-between space-x-4 overflow-hidden rounded-md border p-6 pr-8 shadow-lg transition-all data-[swipe=cancel]:translate-x-0 data-[swipe=end]:translate-x-[var(--radix-toast-swipe-end-x)] data-[swipe=move]:translate-x-[var(--radix-toast-swipe-move-x)] data-[swipe=move]:transition-none data-[state=open]:animate-in data-[state=closed]:animate-out data-[swipe=end]:animate-out data-[state=closed]:fade-out-80 data-[state=closed]:slide-out-to-right-full data-[state=open]:slide-in-from-top-full data-[state=open]:sm:slide-in-from-bottom-full", - { - variants: { - variant: { - default: "border bg-background text-foreground", - destructive: - "destructive group border-destructive bg-destructive text-destructive-foreground", - }, - }, - defaultVariants: { - variant: "default", - }, - }, -) - -const Toast = React.forwardRef< - React.ElementRef, - React.ComponentPropsWithoutRef & - VariantProps ->(({ className, variant, ...props }, ref) => ( - -)) -Toast.displayName = ToastPrimitives.Root.displayName - -const ToastAction = React.forwardRef< - React.ElementRef, - React.ComponentPropsWithoutRef ->(({ className, ...props }, ref) => ( - -)) -ToastAction.displayName = ToastPrimitives.Action.displayName - -const ToastClose = React.forwardRef< - React.ElementRef, - React.ComponentPropsWithoutRef ->(({ className, ...props }, ref) => ( - - - -)) -ToastClose.displayName = ToastPrimitives.Close.displayName - -const ToastTitle = React.forwardRef< - React.ElementRef, - React.ComponentPropsWithoutRef ->(({ className, ...props }, ref) => ( - -)) -ToastTitle.displayName = ToastPrimitives.Title.displayName - -const ToastDescription = React.forwardRef< - React.ElementRef, - React.ComponentPropsWithoutRef ->(({ className, ...props }, ref) => ( - -)) -ToastDescription.displayName = ToastPrimitives.Description.displayName - -type ToastProps = React.ComponentPropsWithoutRef - -type ToastActionElement = React.ReactElement - -export { - Toast, - ToastAction, - type ToastActionElement, - ToastClose, - ToastDescription, - type ToastProps, - ToastProvider, - ToastTitle, - ToastViewport, -} diff --git a/src/renderer/src/components/ui/toaster.tsx b/src/renderer/src/components/ui/toaster.tsx deleted file mode 100644 index abd489572..000000000 --- a/src/renderer/src/components/ui/toaster.tsx +++ /dev/null @@ -1,31 +0,0 @@ -import { - Toast, - ToastClose, - ToastDescription, - ToastProvider, - ToastTitle, - ToastViewport, -} from "@renderer/components/ui/toast" -import { useToast } from "@renderer/components/ui/use-toast" - -export function Toaster() { - const { toasts } = useToast() - - return ( - - {toasts.map(({ id, title, description, action, ...props }) => ( - -
- {title && {title}} - {description && ( - {description} - )} -
- {action} - -
- ))} - -
- ) -} diff --git a/src/renderer/src/components/ui/use-toast.ts b/src/renderer/src/components/ui/use-toast.ts deleted file mode 100644 index c26cfa80b..000000000 --- a/src/renderer/src/components/ui/use-toast.ts +++ /dev/null @@ -1,194 +0,0 @@ -// Inspired by react-hot-toast library -import type { - ToastActionElement, - ToastProps, -} from "@renderer/components/ui/toast" -import * as React from "react" - -const TOAST_LIMIT = 1 -const TOAST_REMOVE_DELAY = 1_000_000 - -type ToasterToast = ToastProps & { - id: string - title?: React.ReactNode - description?: React.ReactNode - action?: ToastActionElement -} - -const actionTypes = { - ADD_TOAST: "ADD_TOAST", - UPDATE_TOAST: "UPDATE_TOAST", - DISMISS_TOAST: "DISMISS_TOAST", - REMOVE_TOAST: "REMOVE_TOAST", -} as const - -let count = 0 - -function genId() { - count = (count + 1) % Number.MAX_SAFE_INTEGER - return count.toString() -} - -type ActionType = typeof actionTypes - -type Action = - | { - type: ActionType["ADD_TOAST"] - toast: ToasterToast - } - | { - type: ActionType["UPDATE_TOAST"] - toast: Partial - } - | { - type: ActionType["DISMISS_TOAST"] - toastId?: ToasterToast["id"] - } - | { - type: ActionType["REMOVE_TOAST"] - toastId?: ToasterToast["id"] - } - -interface State { - toasts: ToasterToast[] -} - -const toastTimeouts = new Map>() - -const addToRemoveQueue = (toastId: string) => { - if (toastTimeouts.has(toastId)) { - return - } - - const timeout = setTimeout(() => { - toastTimeouts.delete(toastId) - dispatch({ - type: "REMOVE_TOAST", - toastId, - }) - }, TOAST_REMOVE_DELAY) - - toastTimeouts.set(toastId, timeout) -} - -export const reducer = (state: State, action: Action): State => { - switch (action.type) { - case "ADD_TOAST": { - return { - ...state, - toasts: [action.toast, ...state.toasts].slice(0, TOAST_LIMIT), - } - } - - case "UPDATE_TOAST": { - return { - ...state, - toasts: state.toasts.map((t) => - t.id === action.toast.id ? { ...t, ...action.toast } : t, - ), - } - } - - case "DISMISS_TOAST": { - const { toastId } = action - - // ! Side effects ! - This could be extracted into a dismissToast() action, - // but I'll keep it here for simplicity - if (toastId) { - addToRemoveQueue(toastId) - } else { - for (const toast of state.toasts) { - addToRemoveQueue(toast.id) - } - } - - return { - ...state, - toasts: state.toasts.map((t) => - t.id === toastId || toastId === undefined ? - { - ...t, - open: false, - } : - t, - ), - } - } - case "REMOVE_TOAST": { - if (action.toastId === undefined) { - return { - ...state, - toasts: [], - } - } - return { - ...state, - toasts: state.toasts.filter((t) => t.id !== action.toastId), - } - } - } -} - -const listeners: Array<(state: State) => void> = [] - -let memoryState: State = { toasts: [] } - -function dispatch(action: Action) { - memoryState = reducer(memoryState, action) - for (const listener of listeners) { - listener(memoryState) - } -} - -type Toast = Omit - -function toast({ ...props }: Toast) { - const id = genId() - - const update = (props: ToasterToast) => - dispatch({ - type: "UPDATE_TOAST", - toast: { ...props, id }, - }) - const dismiss = () => dispatch({ type: "DISMISS_TOAST", toastId: id }) - - dispatch({ - type: "ADD_TOAST", - toast: { - ...props, - id, - open: true, - onOpenChange: (open) => { - if (!open) dismiss() - }, - }, - }) - - return { - id, - dismiss, - update, - } -} - -function useToast() { - const [state, setState] = React.useState(memoryState) - - React.useEffect(() => { - listeners.push(setState) - return () => { - const index = listeners.indexOf(setState) - if (index > -1) { - listeners.splice(index, 1) - } - } - }, [state]) - - return { - ...state, - toast, - dismiss: (toastId?: string) => dispatch({ type: "DISMISS_TOAST", toastId }), - } -} - -export { toast, useToast } diff --git a/src/renderer/src/hooks/useDark.ts b/src/renderer/src/hooks/useDark.ts index 50bdeeae7..e31964b1e 100644 --- a/src/renderer/src/hooks/useDark.ts +++ b/src/renderer/src/hooks/useDark.ts @@ -4,9 +4,7 @@ import { atomDark } from "jotai-dark" const isDarkAtom = atomDark({ storageKey: "theme", disableTransition: true, - applyDarkMode(isDark) { - document.documentElement.dataset.theme = isDark ? "dark" : "light" - }, + mode: "data-theme", }) export function useDark() { diff --git a/src/renderer/src/hooks/useEntryActions.tsx b/src/renderer/src/hooks/useEntryActions.tsx index b54755382..9070da7e0 100644 --- a/src/renderer/src/hooks/useEntryActions.tsx +++ b/src/renderer/src/hooks/useEntryActions.tsx @@ -1,4 +1,3 @@ -import { useToast } from "@renderer/components/ui/use-toast" import { apiClient } from "@renderer/lib/api-fetch" import { client } from "@renderer/lib/client" import type { EntryModel } from "@renderer/models" @@ -6,6 +5,7 @@ import { entryActions } from "@renderer/store/entry" import { useMutation, useQuery } from "@tanstack/react-query" import type { FetchError } from "ofetch" import { ofetch } from "ofetch" +import { toast } from "sonner" export const useEntryActions = ({ view, @@ -44,9 +44,8 @@ export const useEntryActions = ({ }) }, onSuccess: () => { - toast({ + toast("Collected.", { duration: 1000, - description: "Collected.", }) }, }) @@ -65,9 +64,8 @@ export const useEntryActions = ({ }) }, onSuccess: () => { - toast({ + toast("Uncollected.", { duration: 1000, - description: "Uncollected.", }) }, }) @@ -100,8 +98,6 @@ export const useEntryActions = ({ }, }) - const { toast } = useToast() - if (!entry?.entries.url || view === undefined) return { items: [] } const items = [ @@ -128,9 +124,8 @@ export const useEntryActions = ({ onClick: () => { if (!entry.entries.url) return navigator.clipboard.writeText(entry.entries.url) - toast({ + toast("Link copied to clipboard.", { duration: 1000, - description: "Link copied to clipboard.", }) }, }, @@ -155,14 +150,12 @@ export const useEntryActions = ({ images: entry.entries.images, }) if (response?.status === "success") { - toast({ + toast("Saved to Eagle.", { duration: 3000, - description: "Saved to Eagle.", }) } else { - toast({ + toast("Failed to save to Eagle.", { duration: 3000, - description: "Failed to save to Eagle.", }) } }, diff --git a/src/renderer/src/pages/add/index.tsx b/src/renderer/src/pages/add/index.tsx index 54ad7bef5..428a82d4d 100644 --- a/src/renderer/src/pages/add/index.tsx +++ b/src/renderer/src/pages/add/index.tsx @@ -20,7 +20,6 @@ import { SelectValue, } from "@renderer/components/ui/select" import { Switch } from "@renderer/components/ui/switch" -import { useToast } from "@renderer/components/ui/use-toast" import { useBizQuery } from "@renderer/hooks/useBizQuery" import { apiClient } from "@renderer/lib/api-fetch" import { client } from "@renderer/lib/client" @@ -32,6 +31,7 @@ import { useFeed } from "@renderer/queries/feed" import { useMutation } from "@tanstack/react-query" import { useEffect } from "react" import { useForm } from "react-hook-form" +import { toast } from "sonner" import { z } from "zod" const formSchema = z.object({ @@ -68,7 +68,6 @@ export function Component() { } }, [feed.data?.subscription]) - const { toast } = useToast() const followMutation = useMutation({ mutationFn: async (values: z.infer) => { const body = { @@ -103,10 +102,12 @@ export function Component() { client?.invalidateQuery( Queries.feed.byId({ id: feed.data?.feed.id }).key, ) - toast({ - duration: 1000, - description: isSubscribed ? "🎉 Updated." : "🎉 Followed.", - }) + toast( + isSubscribed ? "🎉 Updated." : "🎉 Followed.", + { + duration: 1000, + }, + ) if (!isSubscribed) { window.close() diff --git a/src/renderer/src/pages/settings/profile.tsx b/src/renderer/src/pages/settings/profile.tsx index c415150b5..24eaa6cf9 100644 --- a/src/renderer/src/pages/settings/profile.tsx +++ b/src/renderer/src/pages/settings/profile.tsx @@ -12,10 +12,10 @@ import { FormMessage, } from "@renderer/components/ui/form" import { Input } from "@renderer/components/ui/input" -import { useToast } from "@renderer/components/ui/use-toast" import { apiClient } from "@renderer/lib/api-fetch" import { useMutation } from "@tanstack/react-query" import { useForm } from "react-hook-form" +import { toast } from "sonner" import { z } from "zod" const formSchema = z.object({ @@ -36,17 +36,14 @@ export function Component() { }, }) - const { toast } = useToast() - const updateMutation = useMutation({ mutationFn: async (values: z.infer) => apiClient["auth-app"]["update-account"].$patch({ json: values, }), onSuccess: () => { - toast({ + toast("Profile updated.", { duration: 3000, - description: "Profile updated.", }) }, }) diff --git a/src/renderer/src/providers/root-providers.tsx b/src/renderer/src/providers/root-providers.tsx index 3e417be03..9c611e990 100644 --- a/src/renderer/src/providers/root-providers.tsx +++ b/src/renderer/src/providers/root-providers.tsx @@ -1,6 +1,6 @@ import { SessionProvider } from "@hono/auth-js/react" import { ModalStackProvider } from "@renderer/components/ui/modal" -import { Toaster } from "@renderer/components/ui/toaster" +import { Toaster } from "@renderer/components/ui/sonner" import { TooltipProvider } from "@renderer/components/ui/tooltip" import { jotaiStore } from "@renderer/lib/jotai" import { queryClient } from "@renderer/lib/query-client"