-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: #163: pair selector #204
Changes from 14 commits
2e19515
3ff27f5
95371ee
3c81cfc
ee230a2
70668e4
11e69eb
70fc5c2
0ac5e99
c5fe6b2
0347176
a0f7496
9935846
b24d374
a87bee6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { GET } from '@/shared/api/server/summary/pairs'; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { StarButton } from './star-button'; | ||
export { starStore } from './store'; | ||
export type { Pair } from './storage'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { FC, MouseEventHandler } from 'react'; | ||
import { observer } from 'mobx-react-lite'; | ||
import { Star } from 'lucide-react'; | ||
import { Button } from '@penumbra-zone/ui/Button'; | ||
import { Density } from '@penumbra-zone/ui/Density'; | ||
import StarFilled from './star-filled.svg'; | ||
import type { Pair } from './storage'; | ||
import { starStore } from './store'; | ||
|
||
export interface StarButtonProps { | ||
pair: Pair; | ||
adornment?: boolean; | ||
} | ||
|
||
export const StarButton = observer(({ pair, adornment }: StarButtonProps) => { | ||
const { star, unstar, isStarred } = starStore; | ||
const starred = isStarred(pair); | ||
|
||
const onClick: MouseEventHandler<HTMLButtonElement> = event => { | ||
event.stopPropagation(); | ||
if (starred) { | ||
unstar(pair); | ||
} else { | ||
star(pair); | ||
} | ||
}; | ||
|
||
return ( | ||
<Density compact> | ||
<Button | ||
icon={starred ? (StarFilled as FC) : Star} | ||
priority={adornment ? 'primary' : 'secondary'} | ||
iconOnly={adornment ? 'adornment' : true} | ||
onClick={onClick} | ||
> | ||
Favorite | ||
</Button> | ||
</Density> | ||
); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Metadata } from '@penumbra-zone/protobuf/penumbra/core/asset/v1/asset_pb'; | ||
|
||
export interface Pair { | ||
base: Metadata; | ||
quote: Metadata; | ||
} | ||
|
||
const STAR_STORE_LS_KEY = 'star-pairs-store'; | ||
|
||
export const getStarredPairs = (): Pair[] => { | ||
try { | ||
const data = JSON.parse(localStorage.getItem(STAR_STORE_LS_KEY) ?? '[]') as { | ||
base: string; | ||
quote: string; | ||
}[]; | ||
return data.map(pair => ({ | ||
base: Metadata.fromJson(pair.base), | ||
quote: Metadata.fromJson(pair.quote), | ||
})); | ||
} catch (_) { | ||
return []; | ||
} | ||
}; | ||
|
||
export const setStarredPairs = (pairs: Pair[]): void => { | ||
localStorage.setItem( | ||
STAR_STORE_LS_KEY, | ||
JSON.stringify( | ||
pairs.map(pair => ({ | ||
base: pair.base.toJson(), | ||
quote: pair.quote.toJson(), | ||
})), | ||
), | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { makeAutoObservable } from 'mobx'; | ||
import { Pair, getStarredPairs, setStarredPairs } from './storage'; | ||
|
||
class StarStateStore { | ||
pairs: Pair[] = []; | ||
|
||
constructor() { | ||
makeAutoObservable(this); | ||
|
||
if (typeof window !== 'undefined') { | ||
this.setup(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. possible issue: see |
||
} | ||
|
||
star = (pair: Pair) => { | ||
this.pairs = [...this.pairs, pair]; | ||
setStarredPairs(this.pairs); | ||
}; | ||
|
||
unstar = (pair: Pair) => { | ||
this.pairs = this.pairs.filter( | ||
value => !value.base.equals(pair.base) || !value.quote.equals(pair.quote), | ||
); | ||
setStarredPairs(this.pairs); | ||
}; | ||
|
||
setup() { | ||
this.pairs = getStarredPairs(); | ||
} | ||
|
||
isStarred = (pair: Pair): boolean => { | ||
return this.pairs.some( | ||
value => value.base.symbol === pair.base.symbol && value.quote.symbol === pair.quote.symbol, | ||
); | ||
}; | ||
} | ||
|
||
export const starStore = new StarStateStore(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import type { PairData } from '@/shared/api/server/summary/pairs'; | ||
import { apiFetch } from '@/shared/utils/api-fetch'; | ||
|
||
// Fetches the array of popular (sorted by liquidity) pairs | ||
export const usePairs = () => { | ||
return useQuery({ | ||
queryKey: ['pairs'], | ||
queryFn: async () => { | ||
return apiFetch<PairData[]>('/api/pairs'); | ||
}, | ||
}); | ||
}; |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: would it be easier if we just stored symbol pairs versus the whole metadata objects?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this would delay showing data to user. Idk, for me it's more complicated to calculate the Metadata from symbol than storing it like this