-
Notifications
You must be signed in to change notification settings - Fork 308
feat(xc_admin_frontend): add more details to summary view #1612
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
Merged
guibescos
merged 1 commit into
pyth-network:main
from
cprussin:summarized-view-for-price-feed-proposals
May 28, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
202 changes: 194 additions & 8 deletions
202
...nce/xc_admin/packages/xc_admin_frontend/components/tabs/Proposals/InstructionsSummary.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,211 @@ | ||
| import { Listbox, Transition } from '@headlessui/react' | ||
| import { PythCluster } from '@pythnetwork/client' | ||
| import { MultisigInstruction } from '@pythnetwork/xc-admin-common' | ||
| import { getInstructionsSummary } from './utils' | ||
| import { getMappingCluster } from '../../InstructionViews/utils' | ||
| import CopyText from '../../common/CopyText' | ||
| import Arrow from '@images/icons/down.inline.svg' | ||
| import { Fragment, useState, useMemo, useContext } from 'react' | ||
| import { usePythContext } from '../../../contexts/PythContext' | ||
| import { ClusterContext } from '../../../contexts/ClusterContext' | ||
|
|
||
| export const InstructionsSummary = ({ | ||
| instructions, | ||
| cluster, | ||
| }: { | ||
| instructions: MultisigInstruction[] | ||
| cluster: PythCluster | ||
| }) => ( | ||
| <div className="space-y-4"> | ||
| {getInstructionsSummary({ instructions, cluster }).map((instruction) => ( | ||
| <SummaryItem instruction={instruction} key={instruction.name} /> | ||
| ))} | ||
| </div> | ||
| ) | ||
|
|
||
| const SummaryItem = ({ | ||
| instruction, | ||
| }: { | ||
| instruction: ReturnType<typeof getInstructionsSummary>[number] | ||
| }) => { | ||
| const instructionsCount = getInstructionsSummary({ instructions, cluster }) | ||
| switch (instruction.name) { | ||
| case 'addPublisher': | ||
| case 'delPublisher': { | ||
| return ( | ||
| <div className="grid grid-cols-4 justify-between"> | ||
| <div className="col-span-4 lg:col-span-1"> | ||
| {instruction.name}: {instruction.count} | ||
| </div> | ||
| <AddRemovePublisherDetails | ||
| isAdd={instruction.name === 'addPublisher'} | ||
| summaries={ | ||
| instruction.summaries as AddRemovePublisherDetailsProps['summaries'] | ||
| } | ||
| /> | ||
| </div> | ||
| ) | ||
| } | ||
| default: { | ||
| return ( | ||
| <div> | ||
| {instruction.name}: {instruction.count} | ||
| </div> | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| type AddRemovePublisherDetailsProps = { | ||
| isAdd: boolean | ||
| summaries: { | ||
| readonly priceAccount: string | ||
| readonly pub: string | ||
| }[] | ||
| } | ||
|
|
||
| const AddRemovePublisherDetails = ({ | ||
| isAdd, | ||
| summaries, | ||
| }: AddRemovePublisherDetailsProps) => { | ||
| const { cluster } = useContext(ClusterContext) | ||
| const { priceAccountKeyToSymbolMapping, publisherKeyToNameMapping } = | ||
| usePythContext() | ||
| const publisherKeyToName = | ||
| publisherKeyToNameMapping[getMappingCluster(cluster)] | ||
| const [groupBy, setGroupBy] = useState<'publisher' | 'price account'>( | ||
| 'publisher' | ||
| ) | ||
| const grouped = useMemo( | ||
| () => | ||
| Object.groupBy(summaries, (summary) => | ||
| groupBy === 'publisher' ? summary.pub : summary.priceAccount | ||
| ), | ||
| [groupBy, summaries] | ||
| ) | ||
|
|
||
| return ( | ||
| <div className="space-y-4"> | ||
| {Object.entries(instructionsCount).map(([name, count]) => { | ||
| return ( | ||
| <div key={name}> | ||
| {name}: {count} | ||
| <div className="col-span-4 mt-2 bg-[#444157] p-4 lg:col-span-3 lg:mt-0"> | ||
| <div className="flex flex-row gap-4 items-center pb-4 mb-4 border-b border-light/50 justify-end"> | ||
| <div className="font-semibold">Group by</div> | ||
| <Select | ||
| items={['publisher', 'price account']} | ||
| value={groupBy} | ||
| onChange={setGroupBy} | ||
| /> | ||
| </div> | ||
| <div className="base16 flex justify-between pt-2 pb-6 font-semibold opacity-60"> | ||
| <div>{groupBy === 'publisher' ? 'Publisher' : 'Price Account'}</div> | ||
| <div> | ||
| {groupBy === 'publisher' | ||
| ? isAdd | ||
| ? 'Added To' | ||
| : 'Removed From' | ||
| : `${isAdd ? 'Added' : 'Removed'} Publishers`} | ||
| </div> | ||
| </div> | ||
| {Object.entries(grouped).map(([groupKey, summaries = []]) => ( | ||
| <> | ||
| <div | ||
| key={groupKey} | ||
| className="flex justify-between border-t border-beige-300 py-3" | ||
| > | ||
| <div> | ||
| <KeyAndName | ||
| mapping={ | ||
| groupBy === 'publisher' | ||
| ? publisherKeyToName | ||
| : priceAccountKeyToSymbolMapping | ||
| } | ||
| > | ||
| {groupKey} | ||
| </KeyAndName> | ||
| </div> | ||
| <ul className="flex flex-col gap-2"> | ||
| {summaries.map((summary, index) => ( | ||
| <li key={index}> | ||
| <KeyAndName | ||
| mapping={ | ||
| groupBy === 'publisher' | ||
| ? priceAccountKeyToSymbolMapping | ||
| : publisherKeyToName | ||
| } | ||
| > | ||
| {groupBy === 'publisher' | ||
| ? summary.priceAccount | ||
| : summary.pub} | ||
| </KeyAndName> | ||
| </li> | ||
| ))} | ||
| </ul> | ||
| </div> | ||
| ) | ||
| })} | ||
| </> | ||
| ))} | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| const KeyAndName = ({ | ||
| mapping, | ||
| children, | ||
| }: { | ||
| mapping: { [key: string]: string } | ||
| children: string | ||
| }) => { | ||
| const name = useMemo(() => mapping[children], [mapping, children]) | ||
|
|
||
| return ( | ||
| <div> | ||
| <CopyText text={children} /> | ||
| {name && <div className="ml-4 text-xs opacity-80"> ⤷ {name} </div>} | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| type SelectProps<T extends string> = { | ||
| items: T[] | ||
| value: T | ||
| onChange: (newValue: T) => void | ||
| } | ||
|
|
||
| const Select = <T extends string>({ | ||
| items, | ||
| value, | ||
| onChange, | ||
| }: SelectProps<T>) => ( | ||
| <Listbox | ||
| as="div" | ||
| className="relative z-[3] block w-[180px] text-left" | ||
| value={value} | ||
| onChange={onChange} | ||
| > | ||
| {({ open }) => ( | ||
| <> | ||
| <Listbox.Button className="inline-flex w-full items-center justify-between py-3 px-6 text-sm outline-0 bg-light/20"> | ||
| <span className="mr-3">{value}</span> | ||
| <Arrow className={`${open && 'rotate-180'}`} /> | ||
| </Listbox.Button> | ||
| <Transition | ||
| as={Fragment} | ||
| enter="transition ease-out duration-100" | ||
| enterFrom="transform opacity-0 scale-95" | ||
| enterTo="transform opacity-100 scale-100" | ||
| leave="transition ease-in duration-75" | ||
| leaveFrom="transform opacity-100 scale-100" | ||
| leaveTo="transform opacity-0 scale-95" | ||
| > | ||
| <Listbox.Options className="absolute right-0 mt-2 w-full origin-top-right"> | ||
| {items.map((item) => ( | ||
| <Listbox.Option | ||
| key={item} | ||
| value={item} | ||
| className="block w-full py-3 px-6 text-left text-sm bg-darkGray hover:bg-darkGray2 cursor-pointer" | ||
| > | ||
| {item} | ||
| </Listbox.Option> | ||
| ))} | ||
| </Listbox.Options> | ||
| </Transition> | ||
| </> | ||
| )} | ||
| </Listbox> | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What is the point of the "as const"
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.
as constdoes two things:{ name: 'addPublisher' }is interpreted with thenamefield's type as the literal"addPublisher"instead of the typestring.readonlyIt's a bit of a shame that typescript doesn't have a nice syntax for narrowly inferring types that does only #1 without #2 since the
readonlymodifier is, IMO, quite clunky and not very useful.I did this for #1 -- it actually doesn't matter in this code because the fact that
parsedInstruction.namecan be any arbitrary string means that the inference engine actually can't match the literaladdPublisherto the case on line 106 (as far as I know typescript is not smart enough to infer that the case here only matches for any string exceptaddPublisheranddelPublisher). However, it doesn't make sense to me that an instruction can be an arbitrary name, that seems like something that eventually will be enumerated, and the idea is that by doing this now, eventually if we do enumerate the names, it would be very very easy to update this function so that type inference can correctly match the shape of the return value based on the instruction name. This would be especially valuable if we added more bespoke summaries for different instruction types.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.
In case you're curious, here's a good illustration in the typescript playground of the difference between using the const assertion vs not: https://www.typescriptlang.org/play/?#code/MYewdgzgLgBAJiAyiAtgUygCwJZgOYwC8MAFAIYBOeAXDNBbngJREB8MA3gLABQMdAd2xRgmUpWade-fsDIQ0MAOQAjMgC8ltChgCuFMJxhgy6Wqo1KANHVQYc+WgFYYAXwDc0mXDQAzMroANlDaegZGJmbKEHZYjACigQrWtuhx+IkKtFAUuooeXq68RTy8oJCwCMhpDngA6sKYAMLg0ETiVLT0jCyE7Nx8gsKiHZIDMjByCspqmqFQ+oYcxqZo5rMpMTWMzm4w8pOtUJ6D-D7+QSEwOgvhy5Fr0bG1mWibzwlJjzl5ewfl0BO-BKriAA.
If you take a look at the
.d.tstab on the right side, you'll see how typescript infers the types. If you see howdoSomethingWithConstgets inferred with literals, it essentially allows any consuming code that matchesnametobazto automatically infer correctly thatsomethingis 5 andsomethingElseisundefined.