diff options
Diffstat (limited to 'apps/web/lib/queries/use-custom-feed-mutations.ts')
| -rw-r--r-- | apps/web/lib/queries/use-custom-feed-mutations.ts | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/apps/web/lib/queries/use-custom-feed-mutations.ts b/apps/web/lib/queries/use-custom-feed-mutations.ts new file mode 100644 index 0000000..f0751db --- /dev/null +++ b/apps/web/lib/queries/use-custom-feed-mutations.ts @@ -0,0 +1,122 @@ +"use client" + +import { useMutation, useQueryClient } from "@tanstack/react-query" +import { createSupabaseBrowserClient } from "@/lib/supabase/client" +import { queryKeys } from "./query-keys" +import { notify } from "@/lib/notify" + +export function useCreateCustomFeed() { + const supabaseClient = createSupabaseBrowserClient() + const queryClient = useQueryClient() + + return useMutation({ + mutationFn: async ({ + name, + query, + matchMode, + sourceFolderIdentifier, + }: { + name: string + query: string + matchMode: "and" | "or" + sourceFolderIdentifier: string | null + }) => { + const { + data: { user }, + } = await supabaseClient.auth.getUser() + + if (!user) throw new Error("Not authenticated") + + const { error } = await supabaseClient.from("custom_feeds").insert({ + user_id: user.id, + name, + query, + match_mode: matchMode, + source_folder_id: sourceFolderIdentifier, + position: 0, + }) + + if (error) throw error + }, + onSuccess: () => { + queryClient.invalidateQueries({ queryKey: queryKeys.customFeeds.all }) + queryClient.invalidateQueries({ queryKey: queryKeys.userProfile.all }) + notify("custom feed created") + }, + onError: (error: Error) => { + notify( + error.message.includes("limit") + ? "custom feed limit reached for your plan" + : "failed to create custom feed: " + error.message + ) + }, + }) +} + +export function useUpdateCustomFeed() { + const supabaseClient = createSupabaseBrowserClient() + const queryClient = useQueryClient() + + return useMutation({ + mutationFn: async ({ + customFeedIdentifier, + name, + query, + matchMode, + sourceFolderIdentifier, + }: { + customFeedIdentifier: string + name: string + query: string + matchMode: "and" | "or" + sourceFolderIdentifier: string | null + }) => { + const { error } = await supabaseClient + .from("custom_feeds") + .update({ + name, + query, + match_mode: matchMode, + source_folder_id: sourceFolderIdentifier, + }) + .eq("id", customFeedIdentifier) + + if (error) throw error + }, + onSuccess: () => { + queryClient.invalidateQueries({ queryKey: queryKeys.customFeeds.all }) + notify("custom feed updated") + }, + onError: (error: Error) => { + notify("failed to update custom feed: " + error.message) + }, + }) +} + +export function useDeleteCustomFeed() { + const supabaseClient = createSupabaseBrowserClient() + const queryClient = useQueryClient() + + return useMutation({ + mutationFn: async ({ + customFeedIdentifier, + }: { + customFeedIdentifier: string + }) => { + const { error } = await supabaseClient + .from("custom_feeds") + .delete() + .eq("id", customFeedIdentifier) + + if (error) throw error + }, + onSuccess: () => { + queryClient.invalidateQueries({ queryKey: queryKeys.customFeeds.all }) + queryClient.invalidateQueries({ queryKey: queryKeys.userProfile.all }) + notify("custom feed deleted") + }, + onError: (error: Error) => { + notify("failed to delete custom feed: " + error.message) + }, + }) +} |