-
Notifications
You must be signed in to change notification settings - Fork 31
Updated kci-dev command generator #2084
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
Open
aliceinwire
wants to merge
5
commits into
kernelci:main
Choose a base branch
from
aliceinwire:kcidev
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
10fec8b
Updated kci-dev command generator
aliceinwire 8e96f59
Replaced the terminal tooltip
aliceinwire 39e1fab
Make dashboard-generated kci-dev commands correct
aliceinwire 14f3676
Added a central dashboard-filter translator
aliceinwire cde88a1
Split the former KcidevFooter into a focused KcidevCommandButton
aliceinwire 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
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
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
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
106 changes: 106 additions & 0 deletions
106
dashboard/src/components/Footer/KcidevCommandButton.stories.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 |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import type { Meta, StoryObj } from '@storybook/react'; | ||
| import { expect, fn, userEvent, within } from '@storybook/test'; | ||
| import { IntlProvider } from 'react-intl'; | ||
|
|
||
| import type { JSX } from 'react'; | ||
|
|
||
| import { LOCALES } from '@/locales/constants'; | ||
| import { messages } from '@/locales/messages'; | ||
|
|
||
| import { MemoizedKcidevCommandButton } from './KcidevCommandButton'; | ||
|
|
||
| const completeCommand = | ||
| "kci-dev results trees --origin 'unsafe origin' --days 7"; | ||
|
|
||
| const meta: Meta<typeof MemoizedKcidevCommandButton> = { | ||
| title: 'Components/Footer/KcidevCommandButton', | ||
| component: MemoizedKcidevCommandButton, | ||
| decorators: [ | ||
| (story): JSX.Element => ( | ||
| <IntlProvider messages={messages[LOCALES.EN_US]} locale={LOCALES.EN_US}> | ||
| {story()} | ||
| </IntlProvider> | ||
| ), | ||
| ], | ||
| args: { | ||
| command: { | ||
| id: 'trees', | ||
| label: 'Tree listing', | ||
| argv: [ | ||
| 'kci-dev', | ||
| 'results', | ||
| 'trees', | ||
| '--origin', | ||
| 'unsafe origin', | ||
| '--days', | ||
| '7', | ||
| ], | ||
| omittedFilters: ['tree search'], | ||
| reproduction: 'partial', | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| export default meta; | ||
| type Story = StoryObj<typeof meta>; | ||
|
|
||
| export const Interaction: Story = { | ||
| play: async ({ canvasElement, step }) => { | ||
| const canvas = within(canvasElement); | ||
| const page = within(canvasElement.ownerDocument.body); | ||
| const writeText = fn<Clipboard['writeText']>().mockResolvedValue(undefined); | ||
| Object.defineProperty(navigator, 'clipboard', { | ||
| configurable: true, | ||
| value: { writeText }, | ||
| }); | ||
| const trigger = canvas.getByRole('button', { name: 'CLI command' }); | ||
|
|
||
| await step( | ||
| 'opens by keyboard and exposes the complete command', | ||
| async () => { | ||
| trigger.focus(); | ||
| await userEvent.keyboard('{Enter}'); | ||
| await expect( | ||
| page.getByRole('heading', { name: 'Run this query with kci-dev' }), | ||
| ).toBeVisible(); | ||
| await expect( | ||
| page.getByLabelText('Tree listing: Human-readable'), | ||
| ).toHaveTextContent(completeCommand); | ||
| await expect(page.getByRole('note')).toHaveTextContent('tree search'); | ||
| }, | ||
| ); | ||
|
|
||
| await step( | ||
| 'reports copy success only after the clipboard resolves', | ||
| async () => { | ||
| await userEvent.click( | ||
| page.getByRole('button', { name: 'Copy command: Human-readable' }), | ||
| ); | ||
| await expect(writeText).toHaveBeenCalledWith(completeCommand); | ||
| await expect(page.getByText('Copied')).toBeVisible(); | ||
| }, | ||
| ); | ||
|
|
||
| await step('Escape closes the popover and returns focus', async () => { | ||
| await userEvent.keyboard('{Escape}'); | ||
| await expect(trigger).toHaveFocus(); | ||
| }); | ||
|
|
||
| await step( | ||
| 'keeps the command selectable and reports clipboard errors', | ||
| async () => { | ||
| writeText.mockRejectedValueOnce(new Error('Clipboard denied')); | ||
| await userEvent.keyboard('{Enter}'); | ||
| await userEvent.click( | ||
| page.getByRole('button', { name: 'Copy command: Human-readable' }), | ||
| ); | ||
| await expect(page.getByRole('alert')).toHaveTextContent( | ||
| 'Select it above and copy it manually.', | ||
| ); | ||
| await expect( | ||
| page.getByLabelText('Tree listing: Human-readable'), | ||
| ).toHaveTextContent(completeCommand); | ||
| }, | ||
| ); | ||
| }, | ||
| }; |
149 changes: 149 additions & 0 deletions
149
dashboard/src/components/Footer/KcidevCommandButton.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 |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| import { memo, useMemo, useState, type JSX } from 'react'; | ||
| import { FormattedMessage } from 'react-intl'; | ||
|
|
||
| import { TbTerminal2 } from 'react-icons/tb'; | ||
|
|
||
| import { Button } from '@/components/ui/button'; | ||
| import { | ||
| Popover, | ||
| PopoverContent, | ||
| PopoverTrigger, | ||
| } from '@/components/ui/popover'; | ||
|
|
||
| import { | ||
| serializeShellArgv, | ||
| type KcidevCommand, | ||
| type KcidevCommandVariant, | ||
| } from './kcidevCommand'; | ||
|
|
||
| type CopyStatus = 'idle' | 'copied' | 'error'; | ||
|
|
||
| const KcidevCommandButton = ({ | ||
| command, | ||
| }: { | ||
| command?: KcidevCommand | readonly KcidevCommand[]; | ||
| }): JSX.Element => { | ||
| const [copyStatus, setCopyStatus] = useState<CopyStatus>('idle'); | ||
| const commands = useMemo( | ||
| () => (command ? (Array.isArray(command) ? command : [command]) : []), | ||
| [command], | ||
| ); | ||
| const displayedCommands = useMemo( | ||
| () => | ||
| commands.flatMap(value => | ||
| ( | ||
| value.variants ?? | ||
| ([ | ||
| { id: 'human' as const, label: 'Human-readable', argv: value.argv }, | ||
| ] satisfies KcidevCommandVariant[]) | ||
| ).map((variant: KcidevCommandVariant) => ({ command: value, variant })), | ||
| ), | ||
| [commands], | ||
| ); | ||
|
|
||
| const copyCommand = async (argv: readonly string[]): Promise<void> => { | ||
| setCopyStatus('idle'); | ||
| try { | ||
| await navigator.clipboard.writeText(serializeShellArgv(argv)); | ||
| setCopyStatus('copied'); | ||
| } catch { | ||
| setCopyStatus('error'); | ||
| } | ||
| }; | ||
|
|
||
| if (commands.length === 0) { | ||
| return <></>; | ||
| } | ||
|
|
||
| return ( | ||
| <div className="shrink-0 text-sm"> | ||
| <Popover | ||
| onOpenChange={open => { | ||
| if (open) { | ||
| setCopyStatus('idle'); | ||
| } | ||
| }} | ||
| > | ||
| <PopoverTrigger asChild> | ||
| <Button type="button" variant="outline"> | ||
| <TbTerminal2 aria-hidden="true" className="mr-2 size-5" /> | ||
| <FormattedMessage id="footer.cliCommand" /> | ||
| </Button> | ||
| </PopoverTrigger> | ||
| <PopoverContent | ||
| className="w-[calc(100vw-2rem)] max-w-xl text-left" | ||
| collisionPadding={16} | ||
| > | ||
| <h2 className="mb-3 text-base font-semibold"> | ||
| <FormattedMessage id="footer.commandTitle" /> | ||
| </h2> | ||
| {displayedCommands.map(({ command: value, variant }) => ( | ||
| <div | ||
| className="mb-3" | ||
| key={`${value.id}-${variant.id}-${variant.label}`} | ||
| > | ||
| <h3 className="mb-1 text-sm font-medium"> | ||
| {commands.length > 1 && `${value.label}: `} | ||
| {variant.label} | ||
| </h3> | ||
| <pre | ||
| aria-label={`${value.label}: ${variant.label}`} | ||
| className="max-w-full cursor-text overflow-x-auto rounded-md bg-slate-100 p-3 text-sm select-text" | ||
| tabIndex={0} | ||
| > | ||
| <code>{serializeShellArgv(variant.argv)}</code> | ||
| </pre> | ||
| {value.omittedFilters.length > 0 && ( | ||
| <div | ||
| className="mt-2 rounded-md border border-amber-300 bg-amber-50 p-3 text-sm text-amber-950" | ||
| role="note" | ||
| > | ||
| <FormattedMessage | ||
| id="footer.unsupportedFilters" | ||
| values={{ filters: value.omittedFilters.join(', ') }} | ||
| /> | ||
| </div> | ||
| )} | ||
| <Button | ||
| className="mt-2" | ||
| type="button" | ||
| onClick={() => copyCommand(variant.argv)} | ||
| > | ||
| <FormattedMessage id="footer.copyCommand" /> | ||
| {`: ${variant.label}`} | ||
| </Button> | ||
| </div> | ||
| ))} | ||
| <div className="flex flex-wrap items-center gap-3"> | ||
| <a | ||
| className="text-dark-blue underline" | ||
| href="https://kci.dev" | ||
| rel="noreferrer" | ||
| target="_blank" | ||
| > | ||
| <FormattedMessage id="footer.installKcidev" /> | ||
| </a> | ||
| <a | ||
| className="text-dark-blue underline" | ||
| href="https://kci.dev/results/" | ||
| rel="noreferrer" | ||
| target="_blank" | ||
| > | ||
| <FormattedMessage id="footer.commandDocumentation" /> | ||
| </a> | ||
| </div> | ||
| <div aria-live="polite" className="mt-3 min-h-5 text-sm"> | ||
| {copyStatus === 'copied' && <FormattedMessage id="footer.copied" />} | ||
| {copyStatus === 'error' && ( | ||
| <span className="text-red" role="alert"> | ||
| <FormattedMessage id="footer.copyError" /> | ||
| </span> | ||
| )} | ||
| </div> | ||
| </PopoverContent> | ||
| </Popover> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export const MemoizedKcidevCommandButton = memo(KcidevCommandButton); | ||
Oops, something went wrong.
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.
I think we can present this information in a more concise way, especially by using icons in the buttons.
Also, the install kci-dev command is always present, so we could take it off the Popover.
This is just a proposal, there might be better ways to present the popover.