mirror of
https://github.com/supermemoryai/supermemory.git
synced 2026-05-17 12:20:04 +00:00
## Changes: - Added token validation logic that checks if the current auth token is still valid - Implemented automatic logout when token is invalidated with appropriate user feedback - Added an Account section in the popup that displays the user's email - Improved error toast messages with clearer formatting and helper text 
76 lines
1.7 KiB
TypeScript
76 lines
1.7 KiB
TypeScript
/**
|
|
* React Query hooks for supermemory API
|
|
*/
|
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"
|
|
import {
|
|
fetchProjects,
|
|
getDefaultProject,
|
|
getUserData,
|
|
saveMemory,
|
|
searchMemories,
|
|
setDefaultProject,
|
|
} from "./api"
|
|
import type { MemoryPayload } from "./types"
|
|
|
|
// Query Keys
|
|
export const queryKeys = {
|
|
projects: ["projects"] as const,
|
|
defaultProject: ["defaultProject"] as const,
|
|
userData: ["userData"] as const,
|
|
}
|
|
|
|
// Projects Query
|
|
export function useProjects(options?: { enabled?: boolean }) {
|
|
return useQuery({
|
|
queryKey: queryKeys.projects,
|
|
queryFn: fetchProjects,
|
|
staleTime: 5 * 60 * 1000, // 5 minutes
|
|
enabled: options?.enabled ?? true,
|
|
})
|
|
}
|
|
|
|
// Default Project Query
|
|
export function useDefaultProject(options?: { enabled?: boolean }) {
|
|
return useQuery({
|
|
queryKey: queryKeys.defaultProject,
|
|
queryFn: getDefaultProject,
|
|
staleTime: 2 * 60 * 1000, // 2 minutes
|
|
enabled: options?.enabled ?? true,
|
|
})
|
|
}
|
|
|
|
// User Data Query
|
|
export function useUserData(options?: { enabled?: boolean }) {
|
|
return useQuery({
|
|
queryKey: queryKeys.userData,
|
|
queryFn: getUserData,
|
|
staleTime: 5 * 60 * 1000, // 5 minutes
|
|
enabled: options?.enabled ?? true,
|
|
})
|
|
}
|
|
|
|
// Set Default Project Mutation
|
|
export function useSetDefaultProject() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: setDefaultProject,
|
|
onSuccess: (_, project) => {
|
|
queryClient.setQueryData(queryKeys.defaultProject, project)
|
|
},
|
|
})
|
|
}
|
|
|
|
// Save Memory Mutation
|
|
export function useSaveMemory() {
|
|
return useMutation({
|
|
mutationFn: (payload: MemoryPayload) => saveMemory(payload),
|
|
})
|
|
}
|
|
|
|
// Search Memories Mutation
|
|
export function useSearchMemories() {
|
|
return useMutation({
|
|
mutationFn: (query: string) => searchMemories(query),
|
|
})
|
|
}
|