2025-11-23 00:27:41 +01:00
|
|
|
import { useEffect } from 'react';
|
|
|
|
|
import { useRecoilState } from 'recoil';
|
|
|
|
|
import store from '~/store';
|
|
|
|
|
import { useGetFavoritesQuery, useUpdateFavoritesMutation } from '~/data-provider';
|
|
|
|
|
|
2025-11-25 22:07:14 +01:00
|
|
|
/**
|
|
|
|
|
* Hook for managing user favorites (pinned agents and models).
|
|
|
|
|
*
|
|
|
|
|
* Favorites are synchronized with the server via `/api/user/settings/favorites`.
|
|
|
|
|
* Each favorite is either:
|
|
|
|
|
* - An agent: `{ agentId: string }`
|
|
|
|
|
* - A model: `{ model: string, endpoint: string }`
|
|
|
|
|
*
|
|
|
|
|
* @returns Object containing favorites state and helper methods for
|
|
|
|
|
* adding, removing, toggling, reordering, and checking favorites.
|
|
|
|
|
*/
|
2025-11-23 00:27:41 +01:00
|
|
|
export default function useFavorites() {
|
|
|
|
|
const [favorites, setFavorites] = useRecoilState(store.favorites);
|
|
|
|
|
const getFavoritesQuery = useGetFavoritesQuery();
|
|
|
|
|
const updateFavoritesMutation = useUpdateFavoritesMutation();
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (getFavoritesQuery.data) {
|
2025-11-23 02:48:19 +01:00
|
|
|
if (Array.isArray(getFavoritesQuery.data)) {
|
2025-11-25 22:07:14 +01:00
|
|
|
setFavorites(getFavoritesQuery.data);
|
2025-11-23 02:48:19 +01:00
|
|
|
} else {
|
|
|
|
|
setFavorites([]);
|
|
|
|
|
}
|
2025-11-23 00:27:41 +01:00
|
|
|
}
|
|
|
|
|
}, [getFavoritesQuery.data, setFavorites]);
|
|
|
|
|
|
|
|
|
|
const saveFavorites = (newFavorites: typeof favorites) => {
|
2025-11-23 02:48:19 +01:00
|
|
|
const cleaned = newFavorites.map((f) => {
|
|
|
|
|
if (f.agentId) return { agentId: f.agentId };
|
|
|
|
|
if (f.model && f.endpoint) return { model: f.model, endpoint: f.endpoint };
|
|
|
|
|
return f;
|
|
|
|
|
});
|
|
|
|
|
setFavorites(cleaned);
|
|
|
|
|
updateFavoritesMutation.mutate(cleaned);
|
2025-11-23 00:27:41 +01:00
|
|
|
};
|
|
|
|
|
|
2025-11-23 02:48:19 +01:00
|
|
|
const addFavoriteAgent = (agentId: string) => {
|
|
|
|
|
if (favorites.some((f) => f.agentId === agentId)) return;
|
|
|
|
|
const newFavorites = [...favorites, { agentId }];
|
2025-11-23 00:27:41 +01:00
|
|
|
saveFavorites(newFavorites);
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-23 02:48:19 +01:00
|
|
|
const removeFavoriteAgent = (agentId: string) => {
|
|
|
|
|
const newFavorites = favorites.filter((f) => f.agentId !== agentId);
|
2025-11-23 00:27:41 +01:00
|
|
|
saveFavorites(newFavorites);
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-23 02:48:19 +01:00
|
|
|
const addFavoriteModel = (model: { model: string; endpoint: string }) => {
|
|
|
|
|
if (favorites.some((f) => f.model === model.model && f.endpoint === model.endpoint)) return;
|
|
|
|
|
const newFavorites = [...favorites, { model: model.model, endpoint: model.endpoint }];
|
2025-11-23 00:27:41 +01:00
|
|
|
saveFavorites(newFavorites);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const removeFavoriteModel = (model: string, endpoint: string) => {
|
2025-11-23 02:48:19 +01:00
|
|
|
const newFavorites = favorites.filter((f) => !(f.model === model && f.endpoint === endpoint));
|
2025-11-23 00:27:41 +01:00
|
|
|
saveFavorites(newFavorites);
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-25 22:07:14 +01:00
|
|
|
const isFavoriteAgent = (agentId: string | undefined | null) => {
|
|
|
|
|
if (!agentId) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-11-23 02:48:19 +01:00
|
|
|
return favorites.some((f) => f.agentId === agentId);
|
2025-11-23 00:27:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const isFavoriteModel = (model: string, endpoint: string) => {
|
2025-11-23 02:48:19 +01:00
|
|
|
return favorites.some((f) => f.model === model && f.endpoint === endpoint);
|
2025-11-23 00:27:41 +01:00
|
|
|
};
|
|
|
|
|
|
2025-11-23 02:48:19 +01:00
|
|
|
const toggleFavoriteAgent = (agentId: string) => {
|
|
|
|
|
if (isFavoriteAgent(agentId)) {
|
|
|
|
|
removeFavoriteAgent(agentId);
|
2025-11-23 00:27:41 +01:00
|
|
|
} else {
|
2025-11-23 02:48:19 +01:00
|
|
|
addFavoriteAgent(agentId);
|
2025-11-23 00:27:41 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-23 02:48:19 +01:00
|
|
|
const toggleFavoriteModel = (model: { model: string; endpoint: string }) => {
|
2025-11-23 00:27:41 +01:00
|
|
|
if (isFavoriteModel(model.model, model.endpoint)) {
|
|
|
|
|
removeFavoriteModel(model.model, model.endpoint);
|
|
|
|
|
} else {
|
|
|
|
|
addFavoriteModel(model);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-25 22:07:14 +01:00
|
|
|
/**
|
|
|
|
|
* Reorder favorites and persist the new order to the server.
|
|
|
|
|
* This combines state update and persistence to avoid race conditions
|
|
|
|
|
* where the closure captures stale state.
|
|
|
|
|
*/
|
|
|
|
|
const reorderFavorites = (newFavorites: typeof favorites, persist = false) => {
|
|
|
|
|
const cleaned = newFavorites.map((f) => {
|
|
|
|
|
if (f.agentId) {
|
|
|
|
|
return { agentId: f.agentId };
|
|
|
|
|
}
|
|
|
|
|
if (f.model && f.endpoint) {
|
|
|
|
|
return { model: f.model, endpoint: f.endpoint };
|
|
|
|
|
}
|
|
|
|
|
return f;
|
|
|
|
|
});
|
|
|
|
|
setFavorites(cleaned);
|
|
|
|
|
if (persist) {
|
|
|
|
|
updateFavoritesMutation.mutate(cleaned);
|
|
|
|
|
}
|
2025-11-23 02:48:19 +01:00
|
|
|
};
|
|
|
|
|
|
2025-11-23 00:27:41 +01:00
|
|
|
return {
|
|
|
|
|
favorites,
|
|
|
|
|
addFavoriteAgent,
|
|
|
|
|
removeFavoriteAgent,
|
|
|
|
|
addFavoriteModel,
|
|
|
|
|
removeFavoriteModel,
|
|
|
|
|
isFavoriteAgent,
|
|
|
|
|
isFavoriteModel,
|
|
|
|
|
toggleFavoriteAgent,
|
|
|
|
|
toggleFavoriteModel,
|
2025-11-23 02:48:19 +01:00
|
|
|
reorderFavorites,
|
2025-11-25 22:07:14 +01:00
|
|
|
/** Whether the favorites query is currently loading */
|
|
|
|
|
isLoading: getFavoritesQuery.isLoading,
|
|
|
|
|
/** Whether there was an error fetching favorites */
|
|
|
|
|
isError: getFavoritesQuery.isError,
|
|
|
|
|
/** Whether the update mutation is in progress */
|
|
|
|
|
isUpdating: updateFavoritesMutation.isLoading,
|
|
|
|
|
/** Error from fetching favorites, if any */
|
|
|
|
|
fetchError: getFavoritesQuery.error,
|
|
|
|
|
/** Error from updating favorites, if any */
|
|
|
|
|
updateError: updateFavoritesMutation.error,
|
2025-11-23 00:27:41 +01:00
|
|
|
};
|
|
|
|
|
}
|