2025-11-23 00:27:41 +01:00
|
|
|
import { useEffect } from 'react';
|
|
|
|
|
import { useRecoilState } from 'recoil';
|
2025-11-24 21:18:16 +01:00
|
|
|
import type { Favorite } from '~/store/favorites';
|
2025-11-23 00:27:41 +01:00
|
|
|
import store from '~/store';
|
|
|
|
|
import { useGetFavoritesQuery, useUpdateFavoritesMutation } from '~/data-provider';
|
|
|
|
|
|
|
|
|
|
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-24 21:18:16 +01:00
|
|
|
const mapped = getFavoritesQuery.data.map(
|
|
|
|
|
(f: Favorite & { type?: string; id?: string }) => {
|
|
|
|
|
if (f.agentId || (f.model && f.endpoint)) return f;
|
|
|
|
|
if (f.type === 'agent' && f.id) return { agentId: f.id };
|
|
|
|
|
// Drop label and map legacy model format
|
|
|
|
|
if (f.type === 'model') return { model: f.model, endpoint: f.endpoint };
|
|
|
|
|
return f;
|
|
|
|
|
},
|
|
|
|
|
);
|
2025-11-23 02:48:19 +01:00
|
|
|
setFavorites(mapped);
|
|
|
|
|
} else {
|
|
|
|
|
// Handle legacy format or invalid data
|
|
|
|
|
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-23 02:48:19 +01:00
|
|
|
const isFavoriteAgent = (agentId: string) => {
|
|
|
|
|
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-23 02:48:19 +01:00
|
|
|
const reorderFavorites = (newFavorites: typeof favorites) => {
|
|
|
|
|
setFavorites(newFavorites);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const persistFavorites = (newFavorites: typeof favorites) => {
|
|
|
|
|
updateFavoritesMutation.mutate(newFavorites);
|
|
|
|
|
};
|
|
|
|
|
|
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,
|
|
|
|
|
persistFavorites,
|
2025-11-23 00:27:41 +01:00
|
|
|
};
|
|
|
|
|
}
|