feat: feed edit dialog
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
import { useState } from "react"
|
||||
import {
|
||||
ContextMenu,
|
||||
ContextMenuContent,
|
||||
ContextMenuItem,
|
||||
ContextMenuTrigger,
|
||||
ContextMenuSeparator,
|
||||
} from "@renderer/components/ui/context-menu"
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query"
|
||||
import { useToast } from "@renderer/components/ui/use-toast"
|
||||
import { ToastAction } from "@renderer/components/ui/toast"
|
||||
import { SubscriptionResponse } from "@renderer/lib/types"
|
||||
import { apiFetch } from "@renderer/lib/queries/api-fetch"
|
||||
import { FollowDialog } from "@renderer/components/follow/dialog"
|
||||
import { Dialog, DialogTrigger } from "@renderer/components/ui/dialog"
|
||||
|
||||
export function FeedContextMenu({
|
||||
feed,
|
||||
children,
|
||||
}: {
|
||||
feed: SubscriptionResponse[number]
|
||||
children: React.ReactNode
|
||||
}) {
|
||||
const [dialogOpen, setDialogOpen] = useState(false)
|
||||
const { toast } = useToast()
|
||||
|
||||
const queryClient = useQueryClient()
|
||||
const deleteMutation = useMutation({
|
||||
mutationFn: async (feed: SubscriptionResponse[number]) =>
|
||||
apiFetch("/subscriptions", {
|
||||
method: "DELETE",
|
||||
body: {
|
||||
feedId: feed.feedId,
|
||||
},
|
||||
}),
|
||||
onSuccess: (_, variables) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ["subscriptions", feed.view],
|
||||
})
|
||||
toast({
|
||||
duration: 3000,
|
||||
description: (
|
||||
<>
|
||||
Feed <i className="font-semibold mr-px">{variables.feeds.title}</i>{" "}
|
||||
has been unfollowed.
|
||||
</>
|
||||
),
|
||||
action: (
|
||||
<ToastAction
|
||||
altText="Undo"
|
||||
onClick={async () => {
|
||||
await apiFetch("/subscriptions", {
|
||||
method: "POST",
|
||||
body: {
|
||||
url: variables.feeds.url,
|
||||
view: feed.view,
|
||||
category: variables.category,
|
||||
// private: variables.private,
|
||||
},
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ["subscriptions", feed.view],
|
||||
})
|
||||
}}
|
||||
>
|
||||
Undo
|
||||
</ToastAction>
|
||||
),
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
return (
|
||||
<Dialog key={feed.feedId} open={dialogOpen} onOpenChange={setDialogOpen}>
|
||||
<ContextMenu>
|
||||
<ContextMenuTrigger>{children}</ContextMenuTrigger>
|
||||
<ContextMenuContent onClick={(e) => e.stopPropagation()}>
|
||||
<DialogTrigger asChild>
|
||||
<ContextMenuItem>Edit</ContextMenuItem>
|
||||
</DialogTrigger>
|
||||
<ContextMenuItem onClick={() => deleteMutation.mutate(feed)}>
|
||||
Unfollow
|
||||
</ContextMenuItem>
|
||||
<ContextMenuSeparator />
|
||||
<ContextMenuItem>Open Feed in Browser</ContextMenuItem>
|
||||
<ContextMenuItem>Open Site in Browser</ContextMenuItem>
|
||||
</ContextMenuContent>
|
||||
</ContextMenu>
|
||||
<FollowDialog
|
||||
feed={feed}
|
||||
onSuccess={() => setDialogOpen(false)}
|
||||
isSubscribed={true}
|
||||
/>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
@@ -10,18 +10,7 @@ import { ActivedList } from "@renderer/lib/types"
|
||||
import { cn } from "@renderer/lib/utils"
|
||||
import { SiteIcon } from "../site-icon"
|
||||
import { Response as SubscriptionsResponse } from "@renderer/lib/queries/subscriptions"
|
||||
import {
|
||||
ContextMenu,
|
||||
ContextMenuContent,
|
||||
ContextMenuItem,
|
||||
ContextMenuTrigger,
|
||||
ContextMenuSeparator,
|
||||
} from "@renderer/components/ui/context-menu"
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query"
|
||||
import { useToast } from "@renderer/components/ui/use-toast"
|
||||
import { ToastAction } from "@renderer/components/ui/toast"
|
||||
import { SubscriptionResponse } from "@renderer/lib/types"
|
||||
import { apiFetch } from "@renderer/lib/queries/api-fetch"
|
||||
import { FeedContextMenu } from "./context-menu"
|
||||
|
||||
export function FeedList({
|
||||
className,
|
||||
@@ -89,53 +78,6 @@ function FeedCategory({
|
||||
view?: number
|
||||
}) {
|
||||
const [open, setOpen] = useState(false)
|
||||
const { toast } = useToast()
|
||||
|
||||
const queryClient = useQueryClient()
|
||||
const deleteMutation = useMutation({
|
||||
mutationFn: async (feed: SubscriptionResponse[number]) =>
|
||||
apiFetch("/subscriptions", {
|
||||
method: "DELETE",
|
||||
body: {
|
||||
feedId: feed.feedId,
|
||||
},
|
||||
}),
|
||||
onSuccess: (_, variables) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ["subscriptions", view],
|
||||
})
|
||||
toast({
|
||||
duration: 3000,
|
||||
description: (
|
||||
<>
|
||||
Feed <i className="font-semibold mr-px">{variables.feeds.title}</i>{" "}
|
||||
has been unfollowed.
|
||||
</>
|
||||
),
|
||||
action: (
|
||||
<ToastAction
|
||||
altText="Undo"
|
||||
onClick={async () => {
|
||||
await apiFetch("/subscriptions", {
|
||||
method: "POST",
|
||||
body: {
|
||||
url: variables.feeds.url,
|
||||
view,
|
||||
category: variables.category,
|
||||
// private: variables.private,
|
||||
},
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ["subscriptions", view],
|
||||
})
|
||||
}}
|
||||
>
|
||||
Undo
|
||||
</ToastAction>
|
||||
),
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
return (
|
||||
<Collapsible
|
||||
@@ -194,48 +136,37 @@ function FeedCategory({
|
||||
}}
|
||||
>
|
||||
{data.list.map((feed) => (
|
||||
<ContextMenu key={feed.feedId}>
|
||||
<ContextMenuTrigger>
|
||||
<div
|
||||
key={feed.feedId}
|
||||
className={cn(
|
||||
"flex items-center justify-between text-sm font-medium leading-loose w-full pl-6 pr-2.5 py-[2px] rounded-md cursor-pointer",
|
||||
activedList?.level === levels.feed &&
|
||||
activedList.id === feed.feedId &&
|
||||
"bg-[#C9C9C7]",
|
||||
)}
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
view !== undefined &&
|
||||
setActivedList?.({
|
||||
level: levels.feed,
|
||||
id: feed.feedId,
|
||||
name: feed.feeds.title,
|
||||
view,
|
||||
})
|
||||
}}
|
||||
>
|
||||
<div className="flex items-center min-w-0">
|
||||
<SiteIcon url={feed.feeds.siteUrl} className="w-4 h-4" />
|
||||
<div className="truncate">{feed.feeds.title}</div>
|
||||
</div>
|
||||
{!!feed.unread && (
|
||||
<div className="text-xs text-zinc-500 ml-2">
|
||||
{feed.unread}
|
||||
</div>
|
||||
)}
|
||||
<FeedContextMenu feed={feed} key={feed.feedId}>
|
||||
<div
|
||||
key={feed.feedId}
|
||||
className={cn(
|
||||
"flex items-center justify-between text-sm font-medium leading-loose w-full pl-6 pr-2.5 py-[2px] rounded-md cursor-pointer",
|
||||
activedList?.level === levels.feed &&
|
||||
activedList.id === feed.feedId &&
|
||||
"bg-[#C9C9C7]",
|
||||
)}
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
view !== undefined &&
|
||||
setActivedList?.({
|
||||
level: levels.feed,
|
||||
id: feed.feedId,
|
||||
name: feed.feeds.title || "",
|
||||
view,
|
||||
})
|
||||
}}
|
||||
>
|
||||
<div className="flex items-center min-w-0">
|
||||
<SiteIcon url={feed.feeds.siteUrl} className="w-4 h-4" />
|
||||
<div className="truncate">{feed.feeds.title}</div>
|
||||
</div>
|
||||
</ContextMenuTrigger>
|
||||
<ContextMenuContent onClick={(e) => e.stopPropagation()}>
|
||||
<ContextMenuItem>Edit</ContextMenuItem>
|
||||
<ContextMenuItem onClick={() => deleteMutation.mutate(feed)}>
|
||||
Unfollow
|
||||
</ContextMenuItem>
|
||||
<ContextMenuSeparator />
|
||||
<ContextMenuItem>Open Feed in Browser</ContextMenuItem>
|
||||
<ContextMenuItem>Open Site in Browser</ContextMenuItem>
|
||||
</ContextMenuContent>
|
||||
</ContextMenu>
|
||||
{!!feed.unread && (
|
||||
<div className="text-xs text-zinc-500 ml-2">
|
||||
{feed.unread}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</FeedContextMenu>
|
||||
))}
|
||||
</m.div>
|
||||
)}
|
||||
|
||||
@@ -4,7 +4,7 @@ import { FollowDialog } from "./dialog"
|
||||
import { FeedResponse } from "@renderer/lib/types"
|
||||
import { useState } from "react"
|
||||
|
||||
export function FollowButton({ feed }: { feed: Partial<FeedResponse> }) {
|
||||
export function FollowButton({ feed }: { feed: FeedResponse }) {
|
||||
const [open, setOpen] = useState(false)
|
||||
const [isSuccessful, setIsSuccessful] = useState(false)
|
||||
|
||||
@@ -20,11 +20,14 @@ export function FollowButton({ feed }: { feed: Partial<FeedResponse> }) {
|
||||
)}
|
||||
</DialogTrigger>
|
||||
<FollowDialog
|
||||
feed={feed}
|
||||
feed={{
|
||||
feeds: feed,
|
||||
}}
|
||||
onSuccess={() => {
|
||||
setOpen(false)
|
||||
setIsSuccessful(true)
|
||||
}}
|
||||
isSubscribed={false}
|
||||
/>
|
||||
</Dialog>
|
||||
)
|
||||
|
||||
@@ -18,7 +18,6 @@ import {
|
||||
DialogTitle,
|
||||
} from "@renderer/components/ui/dialog"
|
||||
import { Button } from "@renderer/components/ui/button"
|
||||
import { FeedResponse } from "@renderer/lib/types"
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
@@ -31,9 +30,10 @@ import { cn } from "@renderer/lib/utils"
|
||||
import { Switch } from "@renderer/components/ui/switch"
|
||||
import { FollowSummary } from "../feed-summary"
|
||||
import { apiFetch } from "@renderer/lib/queries/api-fetch"
|
||||
import { SubscriptionResponse } from "@renderer/lib/types"
|
||||
|
||||
const formSchema = z.object({
|
||||
view: z.enum(["0", "1", "2", "3", "4", "5"]),
|
||||
view: z.string(),
|
||||
category: z.string().optional(),
|
||||
private: z.boolean().optional(),
|
||||
})
|
||||
@@ -41,14 +41,27 @@ const formSchema = z.object({
|
||||
export function FollowDialog({
|
||||
feed,
|
||||
onSuccess,
|
||||
}: {
|
||||
feed: Partial<FeedResponse>
|
||||
onSuccess?: (values: z.infer<typeof formSchema>) => void
|
||||
}) {
|
||||
isSubscribed,
|
||||
}:
|
||||
| {
|
||||
feed:
|
||||
| SubscriptionResponse[number]
|
||||
| {
|
||||
feeds: SubscriptionResponse[number]["feeds"]
|
||||
}
|
||||
onSuccess?: (values: z.infer<typeof formSchema>) => void
|
||||
isSubscribed?: false
|
||||
}
|
||||
| {
|
||||
feed: SubscriptionResponse[number]
|
||||
onSuccess?: (values: z.infer<typeof formSchema>) => void
|
||||
isSubscribed: true
|
||||
}) {
|
||||
const form = useForm<z.infer<typeof formSchema>>({
|
||||
resolver: zodResolver(formSchema),
|
||||
defaultValues: {
|
||||
view: "0",
|
||||
view: isSubscribed ? feed.view + "" : "0",
|
||||
category: isSubscribed ? feed.category : undefined,
|
||||
},
|
||||
})
|
||||
|
||||
@@ -58,7 +71,7 @@ export function FollowDialog({
|
||||
apiFetch("/subscriptions", {
|
||||
method: "POST",
|
||||
body: {
|
||||
url: feed.url,
|
||||
url: feed.feeds.url,
|
||||
view: parseInt(values.view),
|
||||
category: values.category,
|
||||
private: values.private,
|
||||
@@ -81,7 +94,7 @@ export function FollowDialog({
|
||||
<DialogHeader>
|
||||
<DialogTitle>Follow</DialogTitle>
|
||||
</DialogHeader>
|
||||
<FollowSummary feed={feed} />
|
||||
<FollowSummary feed={feed.feeds} />
|
||||
<Form {...form}>
|
||||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
|
||||
<FormField
|
||||
@@ -156,7 +169,7 @@ export function FollowDialog({
|
||||
)}
|
||||
/>
|
||||
<Button type="submit" isLoading={followMutation.isPending}>
|
||||
Follow
|
||||
{isSubscribed ? "Update" : "Follow"}
|
||||
</Button>
|
||||
</form>
|
||||
</Form>
|
||||
|
||||
Reference in New Issue
Block a user