1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-06-30 05:41:41 +02:00

network agnostic bookmarks (#162)

* network agnostic bookmarks
* bookmark data structure migration
This commit is contained in:
Matthias Kretschmann 2020-10-29 11:39:42 +01:00 committed by GitHub
parent 86ca66d2cb
commit c849fd0396
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 23 deletions

View File

@ -7,20 +7,21 @@ import { Link } from 'gatsby'
import styles from './Bookmarks.module.css' import styles from './Bookmarks.module.css'
import Price from '../atoms/Price' import Price from '../atoms/Price'
import Tooltip from '../atoms/Tooltip' import Tooltip from '../atoms/Tooltip'
import { ConfigHelperConfig } from '@oceanprotocol/lib/dist/node/utils/ConfigHelper'
async function getAssetsBookmarked(pins: string[], metadataCacheUri: string) { async function getAssetsBookmarked(
try { bookmarks: string[],
const metadataCache = new MetadataCache(metadataCacheUri, Logger) metadataCacheUri: string
const result: DDO[] = [] ) {
const metadataCache = new MetadataCache(metadataCacheUri, Logger)
const result: DDO[] = []
for (const pin of pins) { for (const bookmark of bookmarks) {
result.push(await metadataCache.retrieveDDO(pin)) const ddo = bookmark && (await metadataCache.retrieveDDO(bookmark))
} ddo && result.push(ddo)
return result
} catch (error) {
Logger.error(error.message)
} }
return result
} }
const columns = [ const columns = [
@ -63,19 +64,32 @@ export default function Bookmarks(): ReactElement {
const [isLoading, setIsLoading] = useState<boolean>() const [isLoading, setIsLoading] = useState<boolean>()
useEffect(() => { useEffect(() => {
if (!bookmarks || !bookmarks.length) return if (!config || bookmarks === {}) return
const networkName = (config as ConfigHelperConfig).network
async function init() { async function init() {
if (!bookmarks[networkName]?.length) {
setPinned([])
return
}
setIsLoading(true) setIsLoading(true)
const resultPinned = await getAssetsBookmarked(
bookmarks, try {
config.metadataCacheUri const resultPinned = await getAssetsBookmarked(
) bookmarks[networkName],
setPinned(resultPinned) config.metadataCacheUri
)
setPinned(resultPinned)
} catch (error) {
Logger.error(error.message)
}
setIsLoading(false) setIsLoading(false)
} }
init() init()
}, [bookmarks, config.metadataCacheUri]) }, [bookmarks, config.metadataCacheUri, config])
return ( return (
<Table <Table

View File

@ -2,10 +2,15 @@ import { useUserPreferences } from '../../../providers/UserPreferences'
import React, { ReactElement } from 'react' import React, { ReactElement } from 'react'
import styles from './Bookmark.module.css' import styles from './Bookmark.module.css'
import { ReactComponent as BookmarkIcon } from '../../../images/bookmark.svg' import { ReactComponent as BookmarkIcon } from '../../../images/bookmark.svg'
import { useOcean } from '@oceanprotocol/react'
import { ConfigHelperConfig } from '@oceanprotocol/lib/dist/node/utils/ConfigHelper'
export default function Bookmark({ did }: { did: string }): ReactElement { export default function Bookmark({ did }: { did: string }): ReactElement {
const { config } = useOcean()
const { bookmarks, addBookmark, removeBookmark } = useUserPreferences() const { bookmarks, addBookmark, removeBookmark } = useUserPreferences()
const isBookmarked = bookmarks?.includes(did) const isBookmarked =
bookmarks &&
bookmarks[(config as ConfigHelperConfig).network]?.includes(did)
function handleBookmark() { function handleBookmark() {
isBookmarked ? removeBookmark(did) : addBookmark(did) isBookmarked ? removeBookmark(did) : addBookmark(did)

View File

@ -8,12 +8,17 @@ import React, {
} from 'react' } from 'react'
import { Logger } from '@oceanprotocol/lib' import { Logger } from '@oceanprotocol/lib'
import { LogLevel } from '@oceanprotocol/lib/dist/node/utils/Logger' import { LogLevel } from '@oceanprotocol/lib/dist/node/utils/Logger'
import { useOcean } from '@oceanprotocol/react'
import { getNetworkName } from '../utils/wallet'
import { ConfigHelperConfig } from '@oceanprotocol/lib/dist/node/utils/ConfigHelper'
interface UserPreferencesValue { interface UserPreferencesValue {
debug: boolean debug: boolean
currency: string currency: string
locale: string locale: string
bookmarks: string[] bookmarks: {
[network: string]: string[]
}
setDebug: (value: boolean) => void setDebug: (value: boolean) => void
setCurrency: (value: string) => void setCurrency: (value: string) => void
addBookmark: (did: string) => void addBookmark: (did: string) => void
@ -43,6 +48,8 @@ function UserPreferencesProvider({
}: { }: {
children: ReactNode children: ReactNode
}): ReactElement { }): ReactElement {
const { config } = useOcean()
const networkName = (config as ConfigHelperConfig).network
const localStorage = getLocalStorage() const localStorage = getLocalStorage()
// Set default values from localStorage // Set default values from localStorage
@ -51,7 +58,7 @@ function UserPreferencesProvider({
localStorage?.currency || 'EUR' localStorage?.currency || 'EUR'
) )
const [locale, setLocale] = useState<string>() const [locale, setLocale] = useState<string>()
const [bookmarks, setBookmarks] = useState(localStorage?.bookmarks || []) const [bookmarks, setBookmarks] = useState(localStorage?.bookmarks || {})
// Write values to localStorage on change // Write values to localStorage on change
useEffect(() => { useEffect(() => {
@ -72,15 +79,30 @@ function UserPreferencesProvider({
}, []) }, [])
function addBookmark(didToAdd: string): void { function addBookmark(didToAdd: string): void {
const newPinned = [didToAdd].concat(bookmarks) const newPinned = {
...bookmarks,
[networkName]: [didToAdd].concat(bookmarks[networkName])
}
setBookmarks(newPinned) setBookmarks(newPinned)
} }
function removeBookmark(didToAdd: string): void { function removeBookmark(didToAdd: string): void {
const newPinned = bookmarks.filter((did: string) => did !== didToAdd) const newPinned = {
...bookmarks,
[networkName]: bookmarks[networkName].filter(
(did: string) => did !== didToAdd
)
}
setBookmarks(newPinned) setBookmarks(newPinned)
} }
// Bookmarks old data structure migration
useEffect(() => {
if (!bookmarks.length) return
const newPinned = { mainnet: bookmarks as any }
setBookmarks(newPinned)
}, [bookmarks])
return ( return (
<UserPreferencesContext.Provider <UserPreferencesContext.Provider
value={ value={