mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-19 01:40:15 +01:00
✨ feat: Refactor Favorites functionality to support new data structure and enhance UI interactions
This commit is contained in:
parent
d2faf9c67d
commit
4c10fcd118
13 changed files with 347 additions and 175 deletions
|
|
@ -1,7 +1,6 @@
|
|||
import { useEffect } from 'react';
|
||||
import { useRecoilState } from 'recoil';
|
||||
import store from '~/store';
|
||||
import type { FavoriteModel } from '~/store/favorites';
|
||||
import { useGetFavoritesQuery, useUpdateFavoritesMutation } from '~/data-provider';
|
||||
|
||||
export default function useFavorites() {
|
||||
|
|
@ -11,73 +10,71 @@ export default function useFavorites() {
|
|||
|
||||
useEffect(() => {
|
||||
if (getFavoritesQuery.data) {
|
||||
setFavorites({
|
||||
agents: getFavoritesQuery.data.agents || [],
|
||||
models: getFavoritesQuery.data.models || [],
|
||||
});
|
||||
if (Array.isArray(getFavoritesQuery.data)) {
|
||||
const mapped = getFavoritesQuery.data.map((f: any) => {
|
||||
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;
|
||||
});
|
||||
setFavorites(mapped);
|
||||
} else {
|
||||
// Handle legacy format or invalid data
|
||||
setFavorites([]);
|
||||
}
|
||||
}
|
||||
}, [getFavoritesQuery.data, setFavorites]);
|
||||
|
||||
const saveFavorites = (newFavorites: typeof favorites) => {
|
||||
setFavorites(newFavorites);
|
||||
updateFavoritesMutation.mutate(newFavorites);
|
||||
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);
|
||||
};
|
||||
|
||||
const addFavoriteAgent = (id: string) => {
|
||||
const agents = favorites?.agents || [];
|
||||
if (agents.includes(id)) return;
|
||||
const newFavorites = {
|
||||
...favorites,
|
||||
agents: [...agents, id],
|
||||
};
|
||||
const addFavoriteAgent = (agentId: string) => {
|
||||
if (favorites.some((f) => f.agentId === agentId)) return;
|
||||
const newFavorites = [...favorites, { agentId }];
|
||||
saveFavorites(newFavorites);
|
||||
};
|
||||
|
||||
const removeFavoriteAgent = (id: string) => {
|
||||
const agents = favorites?.agents || [];
|
||||
const newFavorites = {
|
||||
...favorites,
|
||||
agents: agents.filter((item) => item !== id),
|
||||
};
|
||||
const removeFavoriteAgent = (agentId: string) => {
|
||||
const newFavorites = favorites.filter((f) => f.agentId !== agentId);
|
||||
saveFavorites(newFavorites);
|
||||
};
|
||||
|
||||
const addFavoriteModel = (model: FavoriteModel) => {
|
||||
const models = favorites?.models || [];
|
||||
if (models.some((m) => m.model === model.model && m.endpoint === model.endpoint)) return;
|
||||
const newFavorites = {
|
||||
...favorites,
|
||||
models: [...models, model],
|
||||
};
|
||||
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 }];
|
||||
saveFavorites(newFavorites);
|
||||
};
|
||||
|
||||
const removeFavoriteModel = (model: string, endpoint: string) => {
|
||||
const models = favorites?.models || [];
|
||||
const newFavorites = {
|
||||
...favorites,
|
||||
models: models.filter((m) => !(m.model === model && m.endpoint === endpoint)),
|
||||
};
|
||||
const newFavorites = favorites.filter((f) => !(f.model === model && f.endpoint === endpoint));
|
||||
saveFavorites(newFavorites);
|
||||
};
|
||||
|
||||
const isFavoriteAgent = (id: string) => {
|
||||
return (favorites?.agents || []).includes(id);
|
||||
const isFavoriteAgent = (agentId: string) => {
|
||||
return favorites.some((f) => f.agentId === agentId);
|
||||
};
|
||||
|
||||
const isFavoriteModel = (model: string, endpoint: string) => {
|
||||
return (favorites?.models || []).some((m) => m.model === model && m.endpoint === endpoint);
|
||||
return favorites.some((f) => f.model === model && f.endpoint === endpoint);
|
||||
};
|
||||
|
||||
const toggleFavoriteAgent = (id: string) => {
|
||||
if (isFavoriteAgent(id)) {
|
||||
removeFavoriteAgent(id);
|
||||
const toggleFavoriteAgent = (agentId: string) => {
|
||||
if (isFavoriteAgent(agentId)) {
|
||||
removeFavoriteAgent(agentId);
|
||||
} else {
|
||||
addFavoriteAgent(id);
|
||||
addFavoriteAgent(agentId);
|
||||
}
|
||||
};
|
||||
|
||||
const toggleFavoriteModel = (model: FavoriteModel) => {
|
||||
const toggleFavoriteModel = (model: { model: string; endpoint: string }) => {
|
||||
if (isFavoriteModel(model.model, model.endpoint)) {
|
||||
removeFavoriteModel(model.model, model.endpoint);
|
||||
} else {
|
||||
|
|
@ -85,6 +82,14 @@ export default function useFavorites() {
|
|||
}
|
||||
};
|
||||
|
||||
const reorderFavorites = (newFavorites: typeof favorites) => {
|
||||
setFavorites(newFavorites);
|
||||
};
|
||||
|
||||
const persistFavorites = (newFavorites: typeof favorites) => {
|
||||
updateFavoritesMutation.mutate(newFavorites);
|
||||
};
|
||||
|
||||
return {
|
||||
favorites,
|
||||
addFavoriteAgent,
|
||||
|
|
@ -95,5 +100,7 @@ export default function useFavorites() {
|
|||
isFavoriteModel,
|
||||
toggleFavoriteAgent,
|
||||
toggleFavoriteModel,
|
||||
reorderFavorites,
|
||||
persistFavorites,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue