Skip to content
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: drag nodes and subscriptions between groups #79

Merged
merged 1 commit into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/components/DraggableResourceBadge.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { useDraggable } from '@dnd-kit/core'
import { ActionIcon, Badge, Text, Tooltip } from '@mantine/core'
import { IconX } from '@tabler/icons-react'

import { DraggableResourceType } from '~/constants'

export const DraggableResourceBadge = ({
id,
name,
type,
nodeID,
groupID,
subscriptionID,
onRemove,
dragDisabled,
children,
}: {
id: string
name: string
type: DraggableResourceType
nodeID?: string
groupID?: string
subscriptionID?: string
onRemove?: () => void
dragDisabled?: boolean
children?: React.ReactNode
}) => {
const { attributes, listeners, setNodeRef, isDragging } = useDraggable({
id,
data: {
type,
nodeID,
groupID,
subscriptionID,
},
disabled: dragDisabled,
})

return (
<Tooltip disabled={!children} label={<Text fz="xs">{children}</Text>} withArrow>
<Badge
ref={setNodeRef}
pr={onRemove ? 3 : undefined}
rightSection={
onRemove && (
<ActionIcon color="blue" size="xs" radius="xl" variant="transparent" onClick={onRemove}>
<IconX size={12} />
</ActionIcon>
)
}
style={{
zIndex: isDragging ? 10 : 0,
}}
opacity={isDragging ? 0.5 : undefined}
>
<Text {...listeners} {...attributes} truncate>
{name}
</Text>
</Badge>
</Tooltip>
)
}
6 changes: 5 additions & 1 deletion src/components/DraggableResourceCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,25 @@ import { DraggableResourceType } from '~/constants'

export const DraggableResourceCard = ({
id,
nodeID,
subscriptionID,
type,
name,
onRemove,
actions,
children,
}: {
id: string
nodeID?: string
subscriptionID?: string
type: DraggableResourceType
name: string
onRemove: () => void
actions?: React.ReactNode
children: React.ReactNode
}) => {
const { t } = useTranslation()
const { attributes, listeners, setNodeRef, isDragging } = useDraggable({ id, data: { type } })
const { attributes, listeners, setNodeRef, isDragging } = useDraggable({ id, data: { type, nodeID, subscriptionID } })

return (
<Card
Expand Down
41 changes: 0 additions & 41 deletions src/components/DraggableSubscriptionNodeBadge.tsx

This file was deleted.

12 changes: 7 additions & 5 deletions src/components/RenameFormModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ export type RenameFormModalRef = {
setProps: (props: Props) => void
}

export type HandleRenameSubmit = (
type: RuleType | undefined,
id: string | undefined
) => (values: z.infer<typeof schema>) => Promise<void>

export const RenameFormModal = forwardRef(
(
{
Expand All @@ -32,10 +37,7 @@ export const RenameFormModal = forwardRef(
}: {
opened: boolean
onClose: () => void
handleSubmit: (
type: RuleType | undefined,
id: string | undefined
) => (values: z.infer<typeof schema>) => Promise<void>
handleSubmit: HandleRenameSubmit
},
ref
) => {
Expand All @@ -55,7 +57,7 @@ export const RenameFormModal = forwardRef(
if (props.type === RuleType.routing) {
return t('routing')
}
}, [props.type])
}, [props.type, t])

useImperativeHandle(ref, () => ({
props,
Expand Down
46 changes: 0 additions & 46 deletions src/components/SortableResourceBadge.tsx

This file was deleted.

2 changes: 2 additions & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ export enum DraggableResourceType {
node = 'node',
subscription = 'subscription',
subscription_node = 'subscription_node',
groupNode = 'group_node',
groupSubscription = 'group_subscription',
}

export enum RuleType {
Expand Down
6 changes: 3 additions & 3 deletions src/pages/Experiment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
} from '~/apis'
import { ConfigFormDrawer } from '~/components/ConfigFormModal'
import { CreateNodeFormModal } from '~/components/CreateNodeFormModal'
import { DraggableResourceBadge } from '~/components/DraggableResourceBadge'
import { DraggableResourceCard } from '~/components/DraggableResourceCard'
import { DroppableGroupCard } from '~/components/DroppableGroupCard'
import { GroupFormModal } from '~/components/GroupFormModal'
Expand All @@ -45,7 +46,6 @@ import { PlainTextFormModal } from '~/components/PlainTextFormModal'
import { RenameFormModal, RenameFormModalRef } from '~/components/RenameFormModal'
import { Section } from '~/components/Section'
import { SimpleCard } from '~/components/SimpleCard'
import { SortableResourceBadge } from '~/components/SortableResourceBadge'
import { DialMode, DraggableResourceType, LogLevel, RuleType } from '~/constants'
import { Policy } from '~/schemas/gql/graphql'

Expand Down Expand Up @@ -438,7 +438,7 @@ export const ExperimentPage = () => {
>
<SortableContext items={nodes} strategy={rectSwappingStrategy}>
{nodes.map(({ id: nodeId, name }) => (
<SortableResourceBadge
<DraggableResourceBadge
key={nodeId}
id={nodeId}
name={name}
Expand Down Expand Up @@ -471,7 +471,7 @@ export const ExperimentPage = () => {
<DndContext modifiers={[restrictToParentElement]}>
<SortableContext items={subscriptions} strategy={rectSwappingStrategy}>
{subscriptions.map(({ id: subscriptionId, name }) => (
<SortableResourceBadge
<DraggableResourceBadge
key={subscriptionId}
id={subscriptionId}
name={name}
Expand Down
Loading