Skip to content

Commit

Permalink
refactor: improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackGlory committed Dec 23, 2023
1 parent 0befe33 commit f8a3de8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/components/modal.tsx → src/components/message-box.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { Dialog } from '@headlessui/react'
import { Button } from '@components/button'

export interface IModalProps {
message: string
export interface IMessageBoxProps {
children: React.ReactNode

isOpen: boolean
onClose: () => void
}

export function Modal({ message, isOpen, onClose }: IModalProps) {
export function MessageBox({ children, isOpen, onClose }: IMessageBoxProps) {
return (
<Dialog open={isOpen} onClose={onClose}>
<div className='fixed inset-0 flex items-center justify-center bg-black bg-opacity-50 p-8'>
<Dialog.Panel className='border rounded px-8 py-4 bg-white shadow flex flex-col items-center space-y-4'>
<Dialog.Description className='text-base'>
{message}
{children}
</Dialog.Description>

<Button onClick={onClose}>OK</Button>
Expand Down
25 changes: 13 additions & 12 deletions src/components/options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ import { AdvancedOptions } from '@components/advanced-options'
import { i18n } from '@utils/i18n'
import { Base64 } from 'js-base64'
import { isRuleArray } from '@utils/validator'
import { Modal } from '@components/modal'
import { MessageBox } from '@components/message-box'
import { useImmer } from 'use-immer'
import { compareStringsAscending } from 'extra-sort'
import LoadingIcons from 'react-loading-icons'
import { ConfigStoreContext } from '@utils/config-store'
import { useSelector, useUpdater } from 'extra-react-store'

interface IModal {
interface IMessageBoxState {
isOpen: boolean
message: string
content: React.ReactNode
}

export function Options() {
Expand All @@ -33,9 +33,9 @@ export function Options() {
const [loading, setLoading] = useState<boolean>(true)
const [allFontList, setAllFontList] = useState<chrome.fontSettings.FontName[]>([])
const [monospaceFontList, setMonospaceFontList] = useState<chrome.fontSettings.FontName[]>([])
const [modal, setModal] = useImmer<IModal>({
const [messageBox, setMessageBox] = useImmer<IMessageBoxState>({
isOpen: false
, message: ''
, content: null
})

useMountAsync(async () => {
Expand All @@ -58,13 +58,14 @@ export function Options() {
)
: (
<>
<Modal
message={modal.message}
isOpen={modal.isOpen}
onClose={() => setModal(modal => {
<MessageBox
isOpen={messageBox.isOpen}
onClose={() => setMessageBox(modal => {
modal.isOpen = false
})}
/>
>
{messageBox.content}
</MessageBox>

<nav className='bg-gray-50 px-4 py-2 border-y sticky top-0 flex justify-between'>
<div className='space-x-2'>
Expand Down Expand Up @@ -129,9 +130,9 @@ export function Options() {
})
} catch (e) {
console.warn(e)
setModal({
setMessageBox({
isOpen: true
, message: i18n('alertInvalidRuleFile')
, content: i18n('alertInvalidRuleFile')
})
}
}, { once: true })
Expand Down

0 comments on commit f8a3de8

Please sign in to comment.