src/platform/packages/shared/content-management/favorites/README.mdx
The Favorites service provides a way to add favorites feature to your content. It includes a service for managing the list of favorites and a set of components for displaying and interacting with the list.
// client side
import {
FavoritesClient,
FavoritesContextProvider,
useFavorites,
FavoriteButton,
} from '@kbn/content-management-favorites-public';
const appName = 'my-app';
const favoriteObjectType = 'dashboard';
const favoritesClient = new FavoritesClient(appName, favoriteObjectType, {
http: core.http,
usageCollection: plugins.usageCollection,
});
// wrap your content with the favorites context provider
const myApp = () => {
<FavoritesContextProvider favoritesClient={favoritesClient}>
<App />
</FavoritesContextProvider>;
};
const App = () => {
// get the favorites list
const favoritesQuery = useFavorites();
// display favorite state and toggle button for an object
return <FavoriteButton id={'some-object-id'} />;
};
Internally the favorites list is backed by a saved object. A saved object of type "favorites" is created for each user (user profile id) and space (space id) and object type (e.g. dashboard) combination when a user for the first time favorites an object. The saved object contains a list of favorite objects of the type.
{
"_index": ".kibana_8.16.0_001",
"_id": "spaceid:favorites:object_type:u_profile_id",
"_source": {
"favorites": {
"userId": "u_profile_id",
"type: "dashboard",
"favoriteIds": [
"dashboard_id_1",
"dashboard_id_2",
]
},
"type": "favorites",
"references": [],
"namespace": "spaceid",
}
},
The service doesn't track the favorite object itself, only the object id. When the object is deleted, the favorite isn't removed from the list automatically.