Files
wehub-resource-sync 555e282cc4
pi-agent-plugin checks / lint (push) Has been cancelled
pi-agent-plugin checks / test (20) (push) Has been cancelled
pi-agent-plugin checks / test (22) (push) Has been cancelled
pi-agent-plugin checks / build (push) Has been cancelled
TypeScript SDK CI / check_changes (push) Has been cancelled
TypeScript SDK CI / changelog_check (push) Has been cancelled
ci / changelog_check (push) Has been cancelled
ci / check_changes (push) Has been cancelled
ci / build_mem0 (3.10) (push) Has been cancelled
ci / build_mem0 (3.11) (push) Has been cancelled
ci / build_mem0 (3.12) (push) Has been cancelled
CLI Node CI / lint (push) Has been cancelled
CLI Node CI / test (20) (push) Has been cancelled
CLI Node CI / test (22) (push) Has been cancelled
CLI Node CI / build (push) Has been cancelled
CLI Python CI / lint (push) Has been cancelled
CLI Python CI / test (3.10) (push) Has been cancelled
CLI Python CI / test (3.11) (push) Has been cancelled
CLI Python CI / test (3.12) (push) Has been cancelled
CLI Python CI / build (push) Has been cancelled
openclaw checks / lint (push) Has been cancelled
openclaw checks / test (20) (push) Has been cancelled
openclaw checks / test (22) (push) Has been cancelled
openclaw checks / build (push) Has been cancelled
opencode-plugin checks / build (push) Has been cancelled
TypeScript SDK CI / build_ts_sdk (20) (push) Has been cancelled
TypeScript SDK CI / build_ts_sdk (22) (push) Has been cancelled
TypeScript SDK CI / integration_ts_sdk (20) (push) Has been cancelled
TypeScript SDK CI / integration_ts_sdk (22) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:03:45 +08:00

59 lines
1.7 KiB
TypeScript

import { useState } from 'react';
import axios from 'axios';
import { useDispatch, useSelector } from 'react-redux';
import { AppDispatch, RootState } from '@/store/store';
import { setApps, setTotalApps } from '@/store/profileSlice';
import { setTotalMemories } from '@/store/profileSlice';
// Define the new simplified memory type
export interface SimpleMemory {
id: string;
text: string;
created_at: string;
state: string;
categories: string[];
app_name: string;
}
// Define the shape of the API response item
interface APIStatsResponse {
total_memories: number;
total_apps: number;
apps: any[];
}
interface UseMemoriesApiReturn {
fetchStats: () => Promise<void>;
isLoading: boolean;
error: string | null;
}
export const useStats = (): UseMemoriesApiReturn => {
const [isLoading, setIsLoading] = useState<boolean>(false);
const [error, setError] = useState<string | null>(null);
const dispatch = useDispatch<AppDispatch>();
const user_id = useSelector((state: RootState) => state.profile.userId);
const URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8765";
const fetchStats = async () => {
setIsLoading(true);
setError(null);
try {
const response = await axios.get<APIStatsResponse>(
`${URL}/api/v1/stats?user_id=${user_id}`
);
dispatch(setTotalMemories(response.data.total_memories));
dispatch(setTotalApps(response.data.total_apps));
dispatch(setApps(response.data.apps));
} catch (err: any) {
const errorMessage = err.message || 'Failed to fetch stats';
setError(errorMessage);
setIsLoading(false);
throw new Error(errorMessage);
}
};
return { fetchStats, isLoading, error };
};