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: add a modal to show node qrcode #99

Merged
merged 1 commit into from
Jun 23, 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"nanostores": "^0.9.2",
"prettier": "^2.8.8",
"prettier-plugin-tailwindcss": "^0.3.0",
"qrcode.react": "^3.1.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-i18next": "^13.0.0",
Expand Down
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/components/ConfigFormModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const InputList = <T extends z.infer<typeof schema>>({
)
}

export type ConfigFormDrawerRef = {
export type ConfigFormModalRef = {
form: UseFormReturnType<z.infer<typeof schema>>
setEditingID: (id: string) => void
initOrigins: (origins: z.infer<typeof schema>) => void
Expand Down
2 changes: 1 addition & 1 deletion src/components/GroupFormModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const schema = z.object({
policy: z.nativeEnum(Policy),
})

export type GroupFormDrawerRef = {
export type GroupFormModalRef = {
form: UseFormReturnType<z.infer<typeof schema>>
setEditingID: (id: string) => void
initOrigins: (origins: z.infer<typeof schema>) => void
Expand Down
33 changes: 33 additions & 0 deletions src/components/NodeQRCodeModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Center, Modal } from '@mantine/core'
import { QRCodeSVG } from 'qrcode.react'
import { forwardRef, useImperativeHandle, useState } from 'react'

type Props = {
name: string
link: string
}

export type QRCodeModalRef = {
props: Props
setProps: (props: Props) => void
}

export const QRCodeModal = forwardRef(({ opened, onClose }: { opened: boolean; onClose: () => void }, ref) => {
const [props, setProps] = useState<Props>({
name: '',
link: '',
})

useImperativeHandle(ref, () => ({
props,
setProps,
}))

return (
<Modal opened={opened} onClose={onClose} title={props.name}>
<Center>
<QRCodeSVG size={320} value={props.link} />
</Center>
</Modal>
)
})
10 changes: 5 additions & 5 deletions src/pages/Orchestrate/Config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Fragment, useRef } from 'react'
import { useTranslation } from 'react-i18next'

import { useConfigsQuery, useRemoveConfigMutation, useSelectConfigMutation } from '~/apis'
import { ConfigFormDrawer, ConfigFormDrawerRef } from '~/components/ConfigFormModal'
import { ConfigFormDrawer, ConfigFormModalRef } from '~/components/ConfigFormModal'
import { RenameFormModal, RenameFormModalRef } from '~/components/RenameFormModal'
import { Section } from '~/components/Section'
import { SimpleCard } from '~/components/SimpleCard'
Expand All @@ -23,7 +23,7 @@ export const Config = () => {
const { data: configsQuery } = useConfigsQuery()
const selectConfigMutation = useSelectConfigMutation()
const removeConfigMutation = useRemoveConfigMutation()
const updateConfigFormDrawerRef = useRef<ConfigFormDrawerRef>(null)
const updateConfigFormModalRef = useRef<ConfigFormModalRef>(null)

const [openedRenameFormModal, { open: openRenameFormModal, close: closeRenameFormModal }] = useDisclosure(false)
const renameFormModalRef = useRef<RenameFormModalRef>(null)
Expand Down Expand Up @@ -60,14 +60,14 @@ export const Config = () => {
<ActionIcon
size="xs"
onClick={() => {
updateConfigFormDrawerRef.current?.setEditingID(config.id)
updateConfigFormModalRef.current?.setEditingID(config.id)

const { checkInterval, checkTolerance, sniffingTimeout, logLevel, ...global } = config.global

const logLevelSteps = GET_LOG_LEVEL_STEPS(t)
const logLevelNumber = logLevelSteps.findIndex(([, l]) => l === logLevel)

updateConfigFormDrawerRef.current?.initOrigins({
updateConfigFormModalRef.current?.initOrigins({
name: config.name,
logLevelNumber,
checkIntervalSeconds: deriveTime(checkInterval, 's'),
Expand All @@ -93,7 +93,7 @@ export const Config = () => {

<ConfigFormDrawer opened={openedCreateConfigFormDrawer} onClose={closeCreateConfigFormDrawer} />
<ConfigFormDrawer
ref={updateConfigFormDrawerRef}
ref={updateConfigFormModalRef}
opened={openedUpdateConfigFormDrawer}
onClose={closeUpdateConfigFormDrawer}
/>
Expand Down
4 changes: 2 additions & 2 deletions src/pages/Orchestrate/Group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from '~/apis'
import { DraggableResourceBadge } from '~/components/DraggableResourceBadge'
import { DroppableGroupCard } from '~/components/DroppableGroupCard'
import { GroupFormDrawerRef, GroupFormModal } from '~/components/GroupFormModal'
import { GroupFormModal, GroupFormModalRef } from '~/components/GroupFormModal'
import { RenameFormModal, RenameFormModalRef } from '~/components/RenameFormModal'
import { Section } from '~/components/Section'
import { DraggableResourceType, RuleType } from '~/constants'
Expand All @@ -34,7 +34,7 @@ export const GroupResource = ({ highlight }: { highlight?: boolean }) => {
const groupDelSubscriptionsMutation = useGroupDelSubscriptionsMutation()
const [droppableGroupCardAccordionValues, setDroppableGroupCardAccordionValues] = useState<string[]>([])
const renameFormModalRef = useRef<RenameFormModalRef>(null)
const updateGroupFormModalRef = useRef<GroupFormDrawerRef>(null)
const updateGroupFormModalRef = useRef<GroupFormModalRef>(null)
const { data: subscriptionsQuery } = useSubscriptionsQuery()

return (
Expand Down
26 changes: 23 additions & 3 deletions src/pages/Orchestrate/Node.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import { Spoiler, Text, useMantineTheme } from '@mantine/core'
import { ActionIcon, Spoiler, Text, useMantineTheme } from '@mantine/core'
import { useDisclosure } from '@mantine/hooks'
import { IconCloud } from '@tabler/icons-react'
import { IconCloud, IconDetails } from '@tabler/icons-react'
import { useRef } from 'react'
import { useTranslation } from 'react-i18next'

import { useImportNodesMutation, useNodesQuery, useRemoveNodesMutation } from '~/apis'
import { DraggableResourceCard } from '~/components/DraggableResourceCard'
import { ImportResourceFormModal } from '~/components/ImportResourceFormModal'
import { QRCodeModal, QRCodeModalRef } from '~/components/NodeQRCodeModal'
import { Section } from '~/components/Section'
import { DraggableResourceType } from '~/constants'

export const NodeResource = () => {
const { t } = useTranslation()
const theme = useMantineTheme()

const [openedQRCodeModal, { open: openQRCodeModal, close: closeQRCodeModal }] = useDisclosure(false)
const [openedImportNodeFormModal, { open: openImportNodeFormModal, close: closeImportNodeFormModal }] =
useDisclosure(false)
const qrCodeModalRef = useRef<QRCodeModalRef>(null)
const { data: nodesQuery } = useNodesQuery()
const removeNodesMutation = useRemoveNodesMutation()
const importNodesMutation = useImportNodesMutation()
Expand All @@ -33,9 +37,23 @@ export const NodeResource = () => {
{protocol}
</Text>
}
actions={
<ActionIcon
size="xs"
onClick={() => {
qrCodeModalRef.current?.setProps({
name: name || tag!,
link,
})
openQRCodeModal()
}}
>
<IconDetails />
</ActionIcon>
}
onRemove={() => removeNodesMutation.mutate([id])}
>
<Text fw={600} color={theme.primaryColor}>
<Text fw={600} color={theme.primaryColor} sx={{ wordBreak: 'break-all' }}>
{name}
</Text>

Expand All @@ -56,6 +74,8 @@ export const NodeResource = () => {
</DraggableResourceCard>
))}

<QRCodeModal ref={qrCodeModalRef} opened={openedQRCodeModal} onClose={closeQRCodeModal} />

<ImportResourceFormModal
title={t('node')}
opened={openedImportNodeFormModal}
Expand Down
27 changes: 25 additions & 2 deletions src/pages/Orchestrate/Subscription.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Accordion, ActionIcon, Group, Spoiler, Text } from '@mantine/core'
import { useDisclosure } from '@mantine/hooks'
import { IconCloudComputing, IconDownload } from '@tabler/icons-react'
import { IconCloudComputing, IconDetails, IconDownload } from '@tabler/icons-react'
import dayjs from 'dayjs'
import { Fragment, useRef } from 'react'
import { useTranslation } from 'react-i18next'

import {
Expand All @@ -13,16 +14,20 @@ import {
import { DraggableResourceBadge } from '~/components/DraggableResourceBadge'
import { DraggableResourceCard } from '~/components/DraggableResourceCard'
import { ImportResourceFormModal } from '~/components/ImportResourceFormModal'
import { QRCodeModal, QRCodeModalRef } from '~/components/NodeQRCodeModal'
import { Section } from '~/components/Section'
import { UpdateSubscriptionAction } from '~/components/UpdateSubscriptionAction'
import { DraggableResourceType } from '~/constants'

export const SubscriptionResource = () => {
const { t } = useTranslation()

const [openedQRCodeModal, { open: openQRCodeModal, close: closeQRCodeModal }] = useDisclosure(false)
const [
openedImportSubscriptionFormModal,
{ open: openImportSubscriptionFormModal, close: closeImportSubscriptionFormModal },
] = useDisclosure(false)
const qrCodeModalRef = useRef<QRCodeModalRef>(null)
const { data: subscriptionsQuery } = useSubscriptionsQuery()
const removeSubscriptionsMutation = useRemoveSubscriptionsMutation()
const importSubscriptionsMutation = useImportSubscriptionsMutation()
Expand Down Expand Up @@ -55,7 +60,23 @@ export const SubscriptionResource = () => {
subscriptionID={subscriptionID}
type={DraggableResourceType.subscription}
name={tag || link}
actions={<UpdateSubscriptionAction id={subscriptionID} loading={updateSubscriptionsMutation.isLoading} />}
actions={
<Fragment>
<ActionIcon
size="xs"
onClick={() => {
qrCodeModalRef.current?.setProps({
name: tag!,
link,
})
openQRCodeModal()
}}
>
<IconDetails />
</ActionIcon>
<UpdateSubscriptionAction id={subscriptionID} loading={updateSubscriptionsMutation.isLoading} />
</Fragment>
}
onRemove={() => removeSubscriptionsMutation.mutate([subscriptionID])}
>
<Text fw={600}>{dayjs(updatedAt).format('YYYY-MM-DD HH:mm:ss')}</Text>
Expand Down Expand Up @@ -101,6 +122,8 @@ export const SubscriptionResource = () => {
</DraggableResourceCard>
))}

<QRCodeModal ref={qrCodeModalRef} opened={openedQRCodeModal} onClose={closeQRCodeModal} />

<ImportResourceFormModal
title={t('subscription')}
opened={openedImportSubscriptionFormModal}
Expand Down