-
Notifications
You must be signed in to change notification settings - Fork 453
Move reports between dashboards #456
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
96d4584
feat: move a report to another dashboard
claude 5d79dd7
fix: guard dashboard creation against duplicate submits
d5180e6
fix: drop unused form state and expose dashboard selection to a11y
67afd93
fix: restore dashboard selection on cancel and label the create input
bec289b
fix: ignore Enter during IME composition in dashboard create input
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
138 changes: 138 additions & 0 deletions
138
apps/start/src/components/dashboards/select-dashboard.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,138 @@ | ||
| import { Button } from '@/components/ui/button'; | ||
| import { Input } from '@/components/ui/input'; | ||
| import { Label } from '@/components/ui/label'; | ||
| import { handleError, useTRPC } from '@/integrations/trpc/react'; | ||
| import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; | ||
| import { ArrowLeftIcon, PlusIcon, SaveIcon } from 'lucide-react'; | ||
| import { useId, useState } from 'react'; | ||
|
|
||
| export function SelectDashboard({ | ||
| value, | ||
| onChange, | ||
| projectId, | ||
| excludeDashboardId, | ||
| }: { | ||
| value: string; | ||
| onChange: (value: string) => void; | ||
| projectId: string; | ||
| excludeDashboardId?: string; | ||
| }) { | ||
| const trpc = useTRPC(); | ||
| const queryClient = useQueryClient(); | ||
| const [isCreatingNew, setIsCreatingNew] = useState(false); | ||
| const [newDashboardName, setNewDashboardName] = useState(''); | ||
| const [previousValue, setPreviousValue] = useState(''); | ||
| const newDashboardNameId = useId(); | ||
|
|
||
| const dashboardQuery = useQuery( | ||
| trpc.dashboard.list.queryOptions({ | ||
| projectId, | ||
| }), | ||
| ); | ||
|
|
||
| const dashboardMutation = useMutation( | ||
| trpc.dashboard.create.mutationOptions({ | ||
| onError: handleError, | ||
| async onSuccess(res) { | ||
| queryClient.invalidateQueries(trpc.dashboard.list.pathFilter()); | ||
| await dashboardQuery.refetch(); | ||
| onChange(res.id); | ||
| setIsCreatingNew(false); | ||
| setNewDashboardName(''); | ||
| }, | ||
| }), | ||
| ); | ||
|
|
||
| const handleCreateDashboard = () => { | ||
| const name = newDashboardName.trim(); | ||
| if (!name || dashboardMutation.isPending) { | ||
| return; | ||
| } | ||
|
|
||
| dashboardMutation.mutate({ | ||
| name, | ||
| projectId, | ||
| }); | ||
| }; | ||
|
|
||
| const dashboards = (dashboardQuery.data ?? []).filter( | ||
| (dashboard) => dashboard.id !== excludeDashboardId, | ||
| ); | ||
|
|
||
| return ( | ||
| <div className="space-y-3"> | ||
| <Label htmlFor={isCreatingNew ? newDashboardNameId : undefined}> | ||
| Dashboard | ||
| </Label> | ||
|
|
||
| {!isCreatingNew ? ( | ||
| <div className="row gap-2 flex-wrap"> | ||
| {dashboards.map((dashboard) => ( | ||
| <Button | ||
| type="button" | ||
| key={dashboard.id} | ||
| variant={value === dashboard.id ? 'default' : 'outline'} | ||
| aria-pressed={value === dashboard.id} | ||
| onClick={() => onChange(dashboard.id)} | ||
| > | ||
| {dashboard.name} | ||
| </Button> | ||
| ))} | ||
| <Button | ||
| type="button" | ||
| variant="outline" | ||
| onClick={() => { | ||
| setPreviousValue(value); | ||
| setIsCreatingNew(true); | ||
| onChange(''); | ||
| }} | ||
| icon={PlusIcon} | ||
| > | ||
| Create new dashboard | ||
| </Button> | ||
| </div> | ||
| ) : ( | ||
| <div className="flex gap-2"> | ||
| <Button | ||
| type="button" | ||
| variant="outline" | ||
| size="icon" | ||
| icon={ArrowLeftIcon} | ||
| aria-label="Back to dashboard selection" | ||
| onClick={() => { | ||
| setIsCreatingNew(false); | ||
| setNewDashboardName(''); | ||
| onChange(previousValue); | ||
| }} | ||
| /> | ||
| <Input | ||
| id={newDashboardNameId} | ||
| placeholder="Enter dashboard name" | ||
| value={newDashboardName} | ||
| onChange={(e) => setNewDashboardName(e.target.value)} | ||
| onKeyDown={(e) => { | ||
| if (e.key !== 'Enter') { | ||
| return; | ||
| } | ||
| // Enter confirms an IME candidate, it should not submit. | ||
| if (e.nativeEvent.isComposing || e.keyCode === 229) { | ||
| return; | ||
| } | ||
| e.preventDefault(); | ||
| handleCreateDashboard(); | ||
| }} | ||
| /> | ||
| <Button | ||
| type="button" | ||
| onClick={handleCreateDashboard} | ||
| disabled={!newDashboardName.trim() || dashboardMutation.isPending} | ||
| variant="outline" | ||
| icon={SaveIcon} | ||
| > | ||
| {dashboardMutation.isPending ? 'Creating...' : 'Create'} | ||
| </Button> | ||
| </div> | ||
| )} | ||
| </div> | ||
| ); | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import { ButtonContainer } from '@/components/button-container'; | ||
| import { SelectDashboard } from '@/components/dashboards/select-dashboard'; | ||
| import { Button } from '@/components/ui/button'; | ||
| import { useAppParams } from '@/hooks/use-app-params'; | ||
| import { handleError, useTRPC } from '@/integrations/trpc/react'; | ||
| import { zodResolver } from '@hookform/resolvers/zod'; | ||
| import { useMutation, useQueryClient } from '@tanstack/react-query'; | ||
| import { Controller, useForm } from 'react-hook-form'; | ||
| import { toast } from 'sonner'; | ||
| import { z } from 'zod'; | ||
|
|
||
| import { popModal } from '.'; | ||
| import { ModalContent, ModalHeader } from './Modal/Container'; | ||
|
|
||
| type MoveReportProps = { | ||
| reportId: string; | ||
| dashboardId: string; | ||
| }; | ||
|
|
||
| const validator = z.object({ | ||
| dashboardId: z.string().min(1, 'Required'), | ||
| }); | ||
|
|
||
| type IForm = z.infer<typeof validator>; | ||
|
|
||
| export default function MoveReport({ | ||
| reportId, | ||
| dashboardId, | ||
| }: MoveReportProps) { | ||
| const queryClient = useQueryClient(); | ||
| const { projectId } = useAppParams(); | ||
|
|
||
| const trpc = useTRPC(); | ||
| const move = useMutation( | ||
| trpc.report.move.mutationOptions({ | ||
| onError: handleError, | ||
| onSuccess() { | ||
| queryClient.invalidateQueries(trpc.report.list.pathFilter()); | ||
| queryClient.invalidateQueries(trpc.dashboard.list.pathFilter()); | ||
| toast('Report moved'); | ||
| popModal(); | ||
| }, | ||
| }), | ||
| ); | ||
|
|
||
| const { handleSubmit, formState, control } = useForm<IForm>({ | ||
| resolver: zodResolver(validator), | ||
| defaultValues: { | ||
| dashboardId: '', | ||
| }, | ||
| }); | ||
|
|
||
| return ( | ||
| <ModalContent> | ||
| <ModalHeader title="Move report" /> | ||
| <form | ||
| className="flex flex-col gap-4" | ||
| onSubmit={handleSubmit((values) => { | ||
| move.mutate({ | ||
| reportId, | ||
| dashboardId: values.dashboardId, | ||
| }); | ||
| })} | ||
| > | ||
| <Controller | ||
| control={control} | ||
| name="dashboardId" | ||
| render={({ field }) => { | ||
| return ( | ||
| <SelectDashboard | ||
| value={field.value} | ||
| onChange={field.onChange} | ||
| projectId={projectId!} | ||
| excludeDashboardId={dashboardId} | ||
| /> | ||
| ); | ||
| }} | ||
| /> | ||
| <ButtonContainer> | ||
| <Button | ||
| type="button" | ||
| variant="outline" | ||
| onClick={() => popModal()} | ||
| size="default" | ||
| > | ||
| Cancel | ||
| </Button> | ||
| <Button | ||
| type="submit" | ||
| disabled={!formState.isValid || move.isPending} | ||
| size="default" | ||
| > | ||
| Move | ||
| </Button> | ||
| </ButtonContainer> | ||
| </form> | ||
| </ModalContent> | ||
| ); | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.