ghost-portal/packages/react-hooks/src/useFavorites.tsx
Jaco Greeff c167227845
Add useApiContext (#1890)
* Add useApiContext

* Add missing withApiContext import

* useCacheKey, rename headers

* Adjustment on sessionRewards naming

* Renames based on defacto standards
2019-11-15 10:51:12 +01:00

28 lines
991 B
TypeScript

// Copyright 2017-2019 @polkadot/react-hooks authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { useState } from 'react';
import store from 'store';
import useCacheKey from './useCacheKey';
// hook for favorites with local storage
export default function useFavorites (storageKeyBase: string): [string[], (address: string) => void] {
const [getCache, setCache] = useCacheKey<string[]>(storageKeyBase);
// retrieve from the new style first, if not available, fallback to old-style
const [favorites, setFavorites] = useState<string[]>(getCache() || store.get(storageKeyBase, []));
const _toggleFavorite = (address: string): void =>
setFavorites(
setCache(
favorites.includes(address)
? favorites.filter((accountId): boolean => address !== accountId)
: [...favorites, address]
)
);
return [favorites, _toggleFavorite];
}