Refactor button component and theme switcher

This commit is contained in:
skidoodle 2024-02-10 18:45:31 +01:00
parent 1a4ad1f97e
commit 66c6e0a439
2 changed files with 16 additions and 25 deletions

View file

@ -1,13 +1,13 @@
import { Button } from '@nextui-org/react' import { Button } from '@nextui-org/react'
import { useState, useEffect } from 'react' import React, { useState, useEffect, useCallback } from 'react'
import type { ButtonProps } from '@/utils/props' import type { ButtonProps } from '@/utils/props'
import type { ButtonColor } from '@/utils/types' import type { ButtonColor } from '@/utils/types'
const CustomButton: React.FC<ButtonProps> = ({ label, link }) => { const CustomButton: React.FC<ButtonProps> = React.memo(({ label, link }) => {
const [status, setStatus] = useState<number>(0) const [status, setStatus] = useState<number>()
const [isLoading, setIsLoading] = useState<boolean>(false) const [isLoading, setIsLoading] = useState<boolean>(false)
const checkLinkStatus = async (): Promise<void> => { const checkLinkStatus = useCallback(async (): Promise<void> => {
if (link) { if (link) {
try { try {
setIsLoading(true) setIsLoading(true)
@ -20,13 +20,13 @@ const CustomButton: React.FC<ButtonProps> = ({ label, link }) => {
setIsLoading(false) setIsLoading(false)
} }
} }
} }, [link])
useEffect(() => { useEffect(() => {
void checkLinkStatus() void checkLinkStatus()
}, [link]) }, [checkLinkStatus])
const getColor = (): ButtonColor => { const getColor = useCallback((): ButtonColor => {
switch (true) { switch (true) {
case isLoading: case isLoading:
return 'default' return 'default'
@ -37,15 +37,15 @@ const CustomButton: React.FC<ButtonProps> = ({ label, link }) => {
default: default:
return 'default' return 'default'
} }
} }, [isLoading, status])
const handleClick = () => { const handleClick = useCallback(() => {
if (status === 200 && link) { if (status === 200 && link) {
window.open(link) window.open(link)
} else { } else {
console.error('A hivatkozás nem elérhető.') console.error('A hivatkozás nem elérhető.')
} }
} }, [status, link])
return ( return (
<Button <Button
@ -58,12 +58,12 @@ const CustomButton: React.FC<ButtonProps> = ({ label, link }) => {
{label} {label}
</Button> </Button>
) )
} })
export const PdfButton: React.FC<ButtonProps> = ({ label, link }) => ( export const PdfButton: React.FC<ButtonProps> = React.memo(
<CustomButton label={label} link={link} /> ({ label, link }) => <CustomButton label={label} link={link} />
) )
export const ZipButton: React.FC<ButtonProps> = ({ label, link }) => ( export const ZipButton: React.FC<ButtonProps> = React.memo(
<CustomButton label={label} link={link} /> ({ label, link }) => <CustomButton label={label} link={link} />
) )

View file

@ -6,16 +6,7 @@ export const ThemeSwitcher = () => {
const { theme, setTheme } = useTheme() const { theme, setTheme } = useTheme()
const toggle = () => { const toggle = () => {
switch (theme) { setTheme(theme === 'light' ? 'dark' : 'light')
case 'dark':
setTheme('light')
break
case 'light':
setTheme('dark')
break
default:
break
}
} }
return ( return (