This commit is contained in:
louiscklaw
2025-04-28 08:03:21 +08:00
parent d0ea7e5452
commit 4c72861eda
20 changed files with 877 additions and 237 deletions

View File

@@ -0,0 +1,29 @@
import { usePocketBase } from './usePocketBase.tsx';
import type LessonsTypes from '../types/LessonsTypes';
import { useQuery } from '@tanstack/react-query';
import Vocabularies from '../types/Vocabularies.tsx';
const useGetVocabularyRoute = (lessonType: string, catId: string) => {
const { user, pb } = usePocketBase();
return useQuery({
queryKey: ['useGetVocabularyRoute', lessonType, catId, 'feeds', 'all', user?.id || ''],
staleTime: 60 * 1000,
queryFn: async ({
queryKey,
}: {
queryKey: ['useGetVocabularyRoute', string, string, 'feeds', 'all', string | null];
}) => {
console.log('calling useGetLessonCategoriesRoute');
return await pb.collection('Vocabularies').getList<Vocabularies[]>(1, 9999, {
// TODO: sort by field -> pos
sort: 'id',
filter: `lesson_type_id = "${lessonType}" && cat_id = "${catId}"`,
$autoCancel: false,
expand: 'lesson_type_id,cat_id',
});
},
// enabled: !!user?.id,
});
};
export default useGetVocabularyRoute;

View File

@@ -0,0 +1,15 @@
import React from 'react'
export default function useHelloworld() {
const [count, setCount] = React.useState(0)
const handleIncrement = () => {
// setCount(count + 1)
console.log("helloworld")
}
return {
count,
handleIncrement,
}
}

View File

@@ -0,0 +1,38 @@
import { usePocketBase } from './usePocketBase.tsx';
import { useQuery } from '@tanstack/react-query';
import { LessonsType } from '../types/LessonsTypes';
const useListAllLessonTypes = () => {
const { user, pb } = usePocketBase();
return useQuery({
queryKey: [
'useListAllLessonTypes',
'feeds',
'all',
user?.id || '',
//
],
staleTime: 60 * 1000,
queryFn: async ({
queryKey,
}: {
queryKey: [
'useListAllLessonTypes',
'feeds',
'all',
string | null,
//
];
}) => {
console.log('calling useListAllLessonTypes');
return await pb.collection('LessonsTypes').getFullList<LessonsType>({
// TODO: sort by field -> pos
sort: 'id',
$autoCancel: false,
});
},
// enabled: !!user?.id,
});
};
export default useListAllLessonTypes;

View File

@@ -0,0 +1,49 @@
import { useCallback, useState, useEffect, createContext, useContext } from 'react';
import PocketBase, { AuthRecord } from 'pocketbase';
type PocketBaseContextValue = {
pb: PocketBase;
user: AuthRecord;
logout: () => void;
};
const PocketBaseContext = createContext<PocketBaseContextValue | null>(null);
const POCKETBASE_URL = import.meta.env.VITE_POCKETBASE_URL;
export const PocketBaseProvider = ({ children }: { children: any }) => {
const [pb, _] = useState(new PocketBase(POCKETBASE_URL));
const [user, setUser] = useState(pb.authStore.record);
useEffect(() => {
// Update user state when auth store changes
const unsubscribe = pb.authStore.onChange((_, model) => {
setUser(model);
});
return unsubscribe;
}, [pb.authStore]);
const logout = useCallback(() => pb.authStore.clear(), [pb.authStore]);
return <PocketBaseContext.Provider value={{ pb, user, logout }}>{children}</PocketBaseContext.Provider>;
};
export const usePocketBase = () => {
const context = useContext(PocketBaseContext);
if (context === null) {
throw new Error('usePocketBase must be used within a PocketBaseProvider');
}
return context;
};
export const useRequireAuth = () => {
const { pb, user } = usePocketBase();
const navigate = useNavigate();
useEffect(() => {
if (!pb.authStore.isValid) {
navigate(URLS.LOGIN);
}
}, [pb.authStore.isValid, navigate]);
return user;
};