Fix read notes
This commit is contained in:
parent
63aa7505ea
commit
78d5c3e45c
1 changed files with 7 additions and 82 deletions
|
|
@ -3,6 +3,7 @@ import { Database } from "@xtablo/shared/types/database.types";
|
|||
import { useNavigate } from "react-router-dom";
|
||||
import { supabase } from "../lib/supabase";
|
||||
import { useUser } from "../providers/UserStoreProvider";
|
||||
import { useAuthedApi } from "./auth";
|
||||
|
||||
type Note = Database["public"]["Tables"]["notes"]["Row"];
|
||||
type CreateNoteInput = Pick<Database["public"]["Tables"]["notes"]["Insert"], "title" | "content">;
|
||||
|
|
@ -296,99 +297,23 @@ export function usePublicNote(noteId: string | undefined) {
|
|||
return { note, isLoading };
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to fetch notes shared with user's tablos
|
||||
*/
|
||||
export function useSharedTabloNotes() {
|
||||
const user = useUser();
|
||||
const { data: notes, isLoading } = useQuery<Note[]>({
|
||||
queryKey: ["shared-tablo-notes"],
|
||||
queryFn: async () => {
|
||||
// Find notes shared with tablos the user has access to
|
||||
const { data, error } = await supabase
|
||||
.from("note_access")
|
||||
.select(
|
||||
`
|
||||
note_id,
|
||||
notes!inner (
|
||||
id,
|
||||
title,
|
||||
content,
|
||||
user_id,
|
||||
created_at,
|
||||
updated_at,
|
||||
deleted_at
|
||||
)
|
||||
`
|
||||
)
|
||||
.eq("is_active", true)
|
||||
.neq("user_id", user.id) // Don't include user's own notes
|
||||
.is("notes.deleted_at", null);
|
||||
|
||||
if (error) throw error;
|
||||
|
||||
// Extract notes from the join result
|
||||
type JoinedResult = { note_id: string; notes: Note | Note[] };
|
||||
const extractedNotes = (data as JoinedResult[])
|
||||
.map((item) => (Array.isArray(item.notes) ? item.notes[0] : item.notes))
|
||||
.filter((note) => note !== null && note !== undefined) as Note[];
|
||||
|
||||
return extractedNotes;
|
||||
},
|
||||
enabled: !!user.id,
|
||||
});
|
||||
|
||||
return { notes, isLoading };
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to fetch notes shared with a specific tablo
|
||||
*/
|
||||
export function useTabloNotes(tabloId: string | undefined) {
|
||||
const user = useUser();
|
||||
const { data: notes, isLoading } = useQuery<Note[]>({
|
||||
const api = useAuthedApi();
|
||||
const { data, isLoading } = useQuery<{ notes: Note[] }>({
|
||||
queryKey: ["tablo-notes", tabloId],
|
||||
queryFn: async () => {
|
||||
if (!tabloId) return [];
|
||||
if (!tabloId) return { notes: [] };
|
||||
|
||||
// Find notes shared with this specific tablo or all tablos
|
||||
const { data, error } = await supabase
|
||||
.from("note_access")
|
||||
.select(
|
||||
`
|
||||
note_id,
|
||||
notes!inner (
|
||||
id,
|
||||
title,
|
||||
content,
|
||||
user_id,
|
||||
created_at,
|
||||
updated_at,
|
||||
deleted_at
|
||||
)
|
||||
`
|
||||
)
|
||||
.eq("is_active", true)
|
||||
.or(`tablo_id.eq.${tabloId},tablo_id.is.null`)
|
||||
.is("notes.deleted_at", null);
|
||||
const { data } = await api.get<{ notes: Note[] }>(`/api/v1/notes/${tabloId}`);
|
||||
|
||||
if (error) throw error;
|
||||
|
||||
// Extract notes from the join result and remove duplicates
|
||||
type JoinedResult = { note_id: string; notes: Note | Note[] };
|
||||
const extractedNotes = (data as JoinedResult[])
|
||||
.map((item) => (Array.isArray(item.notes) ? item.notes[0] : item.notes))
|
||||
.filter((note) => note !== null && note !== undefined) as Note[];
|
||||
|
||||
// Remove duplicates by note id (in case a note is shared both with all tablos and this specific tablo)
|
||||
const uniqueNotes = Array.from(
|
||||
new Map(extractedNotes.map((note) => [note.id, note])).values()
|
||||
);
|
||||
|
||||
return uniqueNotes;
|
||||
return data;
|
||||
},
|
||||
enabled: !!tabloId && !!user.id,
|
||||
});
|
||||
|
||||
return { notes, isLoading };
|
||||
return { notes: data?.notes, isLoading };
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue