mirror of
https://github.com/skidoodle/erettsegi-browser.git
synced 2025-02-15 05:39:15 +01:00
71 lines
1.7 KiB
TypeScript
71 lines
1.7 KiB
TypeScript
import { Button } from '@nextui-org/react'
|
|
import { useState, useEffect } from 'react'
|
|
import type { ButtonProps } from '@/utils/props'
|
|
import type { ButtonColor } from '@/utils/types'
|
|
|
|
const CustomButton: React.FC<ButtonProps> = ({ label, link }) => {
|
|
const [status, setStatus] = useState<number>(0)
|
|
const [isLoading, setIsLoading] = useState<boolean>(false)
|
|
|
|
const checkLinkStatus = async (): Promise<void> => {
|
|
if (link) {
|
|
try {
|
|
setIsLoading(true)
|
|
const response = await fetch(
|
|
`/api/validate?link=${encodeURIComponent(link)}`
|
|
)
|
|
const data = (await response.json()) as { status: number }
|
|
setStatus(data.status)
|
|
} catch (error) {
|
|
setStatus(500)
|
|
} finally {
|
|
setIsLoading(false)
|
|
}
|
|
}
|
|
}
|
|
|
|
useEffect(() => {
|
|
void checkLinkStatus()
|
|
}, [link])
|
|
|
|
const getColor = (): ButtonColor => {
|
|
switch (true) {
|
|
case isLoading:
|
|
return 'default'
|
|
case status === 200:
|
|
return 'primary'
|
|
case status === 404:
|
|
return 'danger'
|
|
default:
|
|
return 'default'
|
|
}
|
|
}
|
|
|
|
const handleClick = () => {
|
|
if (status === 200 && link) {
|
|
window.open(link)
|
|
} else {
|
|
console.error('A hivatkozás nem elérhető.')
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Button
|
|
isDisabled={status !== 200 || !link || isLoading}
|
|
isLoading={isLoading}
|
|
className='w-28 mt-3 text-sm font-bold py-2 px-2'
|
|
color={getColor()}
|
|
onClick={handleClick}
|
|
>
|
|
{label}
|
|
</Button>
|
|
)
|
|
}
|
|
|
|
export const PdfButton: React.FC<ButtonProps> = ({ label, link }) => (
|
|
<CustomButton label={label} link={link} />
|
|
)
|
|
|
|
export const ZipButton: React.FC<ButtonProps> = ({ label, link }) => (
|
|
<CustomButton label={label} link={link} />
|
|
)
|