Skip to content

Commit 1cce722

Browse files
committed
fix: resolve Supabase UUID from clerk_id before all snippet and review queries
1 parent 321ee8e commit 1cce722

3 files changed

Lines changed: 43 additions & 14 deletions

File tree

apps/web/hooks/use-review.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
"use client"
22

33
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"
4-
import { useAuth } from "@clerk/nextjs"
54
import { createClient } from "@/lib/supabase/client"
65
import { api } from "@/lib/api"
76
import { snippetKeys } from "@/hooks/use-snippets"
7+
import { useSupabaseUserId } from "@/hooks/use-user"
88
import type { ReviewLog, ReviewLogInsert } from "@/types/snippet"
99

1010
export const reviewKeys = {
1111
logs: (userId: string) => ["review-logs", userId] as const,
1212
}
1313

1414
export function useReviewLogs() {
15-
const { userId } = useAuth()
15+
const { data: userId } = useSupabaseUserId()
1616
const supabase = createClient()
1717

1818
return useQuery({
@@ -33,7 +33,7 @@ export function useReviewLogs() {
3333
}
3434

3535
export function useSubmitReview() {
36-
const { userId } = useAuth()
36+
const { data: userId } = useSupabaseUserId()
3737
const queryClient = useQueryClient()
3838
const supabase = createClient()
3939

@@ -51,7 +51,8 @@ export function useSubmitReview() {
5151
currentInterval: number
5252
currentReps: number
5353
}): Promise<void> => {
54-
// Call SM-2 algorithm via FastAPI
54+
if (!userId) throw new Error("User not ready")
55+
5556
const schedule = await api.review.schedule({
5657
snippet_id: snippetId,
5758
rating,
@@ -60,7 +61,6 @@ export function useSubmitReview() {
6061
current_reps: currentReps,
6162
})
6263

63-
// Update snippet SM-2 state in Supabase
6464
const { error: snippetError } = await supabase
6565
.from("snippets")
6666
.update({
@@ -74,10 +74,9 @@ export function useSubmitReview() {
7474

7575
if (snippetError) throw snippetError
7676

77-
// Log the review event
7877
const log: ReviewLogInsert = {
7978
snippet_id: snippetId,
80-
user_id: userId!,
79+
user_id: userId,
8180
rating,
8281
ease_factor_after: schedule.ease_factor,
8382
interval_after: schedule.interval_days,

apps/web/hooks/use-snippets.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"use client"
22

33
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"
4-
import { useAuth } from "@clerk/nextjs"
54
import { createClient } from "@/lib/supabase/client"
5+
import { useSupabaseUserId } from "@/hooks/use-user"
66
import type { Snippet, SnippetInsert, SnippetUpdate } from "@/types/snippet"
77

88
export const snippetKeys = {
@@ -12,7 +12,7 @@ export const snippetKeys = {
1212
}
1313

1414
export function useSnippets() {
15-
const { userId } = useAuth()
15+
const { data: userId } = useSupabaseUserId()
1616
const supabase = createClient()
1717

1818
return useQuery({
@@ -32,7 +32,7 @@ export function useSnippets() {
3232
}
3333

3434
export function useDueSnippets() {
35-
const { userId } = useAuth()
35+
const { data: userId } = useSupabaseUserId()
3636
const supabase = createClient()
3737
const today = new Date().toISOString().split("T")[0]
3838

@@ -73,15 +73,17 @@ export function useSnippet(id: string) {
7373
}
7474

7575
export function useCreateSnippet() {
76-
const { userId } = useAuth()
76+
const { data: userId } = useSupabaseUserId()
7777
const queryClient = useQueryClient()
7878
const supabase = createClient()
7979

8080
return useMutation({
8181
mutationFn: async (input: Omit<SnippetInsert, "user_id">): Promise<Snippet> => {
82+
if (!userId) throw new Error("User not ready")
83+
8284
const { data, error } = await supabase
8385
.from("snippets")
84-
.insert({ ...input, user_id: userId! })
86+
.insert({ ...input, user_id: userId })
8587
.select()
8688
.single()
8789

@@ -96,7 +98,7 @@ export function useCreateSnippet() {
9698
}
9799

98100
export function useUpdateSnippet() {
99-
const { userId } = useAuth()
101+
const { data: userId } = useSupabaseUserId()
100102
const queryClient = useQueryClient()
101103
const supabase = createClient()
102104

@@ -121,7 +123,7 @@ export function useUpdateSnippet() {
121123
}
122124

123125
export function useDeleteSnippet() {
124-
const { userId } = useAuth()
126+
const { data: userId } = useSupabaseUserId()
125127
const queryClient = useQueryClient()
126128
const supabase = createClient()
127129

apps/web/hooks/use-user.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"use client"
2+
3+
import { useQuery } from "@tanstack/react-query"
4+
import { useAuth } from "@clerk/nextjs"
5+
import { createClient } from "@/lib/supabase/client"
6+
7+
/** Resolves the internal Supabase UUID for the current Clerk user.
8+
* Cached with staleTime: Infinity — the UUID never changes. */
9+
export function useSupabaseUserId() {
10+
const { userId: clerkId } = useAuth()
11+
const supabase = createClient()
12+
13+
return useQuery({
14+
queryKey: ["supabase-user-id", clerkId],
15+
enabled: !!clerkId,
16+
staleTime: Infinity,
17+
queryFn: async (): Promise<string> => {
18+
const { data, error } = await supabase
19+
.from("users")
20+
.select("id")
21+
.eq("clerk_id", clerkId!)
22+
.single()
23+
24+
if (error) throw error
25+
return data.id
26+
},
27+
})
28+
}

0 commit comments

Comments
 (0)