mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:46:29 +00:00
116 lines
6.6 KiB
TypeScript
116 lines
6.6 KiB
TypeScript
import { MouseEvent, useCallback, useEffect, useRef, useState } from "react";
|
|
import Webcam from "react-webcam";
|
|
import { RefreshCw, SwitchCamera } from 'lucide-react'
|
|
import useMobileView from "@/hooks/useMobileView";
|
|
|
|
|
|
const VIDEO_WIDTH = 480
|
|
const VIDEO_HEIGHT = 720
|
|
const MOBILE_VIDEO_WIDTH = 360
|
|
const MOBILE_VIDEO_HEIGHT = 540
|
|
const FACING_MODE_USER = "user";
|
|
const FACING_MODE_ENVIRONMENT = "environment";
|
|
|
|
type Props = {
|
|
onClose: (e: MouseEvent<HTMLElement>) => any,
|
|
onSubmit: (photo: string, landscape?: boolean, w?: number, h?: number) => any
|
|
}
|
|
|
|
export default function CameraV2({ onClose, onSubmit }: Props) {
|
|
const [streamWidth, setStreamWidth] = useState(VIDEO_WIDTH)
|
|
const [streamHeight, setStreamHeight] = useState(VIDEO_HEIGHT)
|
|
const [isMounted, setIsMounted] = useState(false)
|
|
const [photo, setPhoto] = useState('')
|
|
const [facingMode, setFacingMode] = useState(FACING_MODE_USER);
|
|
|
|
const { windowWidth, isMobile } = useMobileView()
|
|
const webcamRef = useRef<Webcam>(null)
|
|
|
|
const videoWidth = windowWidth < 480 ? { exact: VIDEO_HEIGHT, min: VIDEO_HEIGHT, max: VIDEO_HEIGHT } : VIDEO_WIDTH
|
|
const videoHeight = windowWidth < 480 ? { exact: VIDEO_WIDTH, min: VIDEO_WIDTH, max: VIDEO_WIDTH } : VIDEO_HEIGHT
|
|
const videoConstraint: MediaTrackConstraints = {
|
|
// facingMode: { exact: 'user' },
|
|
facingMode: facingMode,
|
|
width: videoWidth,
|
|
height: videoHeight, // REQUIRED VALUE: 480 720 1080
|
|
}
|
|
|
|
const handleSwitchCamera = useCallback(() => {
|
|
setFacingMode(prevState => prevState === FACING_MODE_USER ? FACING_MODE_ENVIRONMENT : FACING_MODE_USER);
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
setIsMounted(true)
|
|
|
|
return () => setIsMounted(false)
|
|
}, [])
|
|
|
|
const capture = async () => {
|
|
const ss = webcamRef.current?.getScreenshot()!
|
|
|
|
const stream_settings = webcamRef.current?.stream?.getVideoTracks()[0].getSettings()
|
|
setStreamWidth(stream_settings?.width!)
|
|
setStreamHeight(stream_settings?.height!)
|
|
|
|
setPhoto(ss)
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<div className={`${isMounted ? 'open' : 'close'} custom-modal-background`} onClick={onClose}>
|
|
<div className={`${isMounted ? 'open' : 'close'} custom-modal w-11/12 md:w-max!`} style={{ translate: '-50% -50%' }}>
|
|
<div className="w-full overflow-y-auto" style={{ maxHeight: '95vh' }}>
|
|
<section className='flex flex-col items-center justify-center relative' onClick={(e) => e.stopPropagation()}>
|
|
<div className={`relative bg-zinc-800 shadow-lg rounded-2xl ${streamWidth >= streamHeight && 'flex justify-center items-center'}`} style={{ width: windowWidth < VIDEO_WIDTH ? MOBILE_VIDEO_WIDTH : VIDEO_WIDTH, height: windowWidth < VIDEO_WIDTH ? MOBILE_VIDEO_HEIGHT : VIDEO_HEIGHT }}>
|
|
{photo ?
|
|
<img alt="" src={photo} className={`${streamWidth < streamHeight ? 'h-full w-full object-cover' : 'w-full'} rounded-2xl`} />
|
|
:
|
|
<Webcam
|
|
ref={webcamRef}
|
|
className="h-full w-full rounded-2xl"
|
|
mirrored={facingMode === FACING_MODE_ENVIRONMENT ? false : true}
|
|
screenshotQuality={1}
|
|
screenshotFormat="image/jpeg"
|
|
forceScreenshotSourceSize
|
|
videoConstraints={videoConstraint}
|
|
/>
|
|
}
|
|
<div className="absolute top-4 md:top-5 w-full flex justify-between items-center px-5">
|
|
<button className="bg-black bg-opacity-40 w-12 h-12 flex justify-center items-center rounded-full hover:text-zinc-400 duration-300 sm:ml-0" onClick={onClose as any}>
|
|
<i className="ti ti-chevron-left icon text-4xl"></i>
|
|
</button>
|
|
{photo &&
|
|
<button className="bg-his bg-opacity-90 text-white w-12 h-12 flex justify-center items-center rounded-full hover:bg-his/70 duration-300 sm:mr-0" onClick={() => onSubmit(photo, streamWidth > streamHeight, streamWidth, streamHeight)}>
|
|
<i className="ti ti-send icon text-2xl"></i>
|
|
</button>
|
|
// <Button size='icon' variant='secondary' className="mr-16 sm:mr-0" onClick={() => onSubmit(photo)}><Send /></Button>
|
|
}
|
|
</div>
|
|
<div className="flex justify-center items-center absolute bottom-5 w-full">
|
|
{!photo ?
|
|
<>
|
|
<div className="bg-white rounded-full w-[88px] h-[88px] flex items-center justify-center cursor-pointer" onClick={capture}>
|
|
<div className="bg-zinc-950 rounded-full w-20 h-20 flex items-center justify-center">
|
|
<div className="bg-white rounded-full w-[70px] h-[70px] flex justify-center items-center"></div>
|
|
</div>
|
|
</div>
|
|
{isMobile &&
|
|
<button className="absolute right-[22px] bg-black bg-opacity-40 w-12 h-12 flex justify-center items-center rounded-full" onClick={handleSwitchCamera}>
|
|
<SwitchCamera />
|
|
</button>
|
|
}
|
|
</>
|
|
:
|
|
<div className="bg-black bg-opacity-40 w-16 h-16 flex justify-center items-center rounded-full hover:text-zinc-400 mb-3 cursor-pointer duration-300">
|
|
<RefreshCw size={40} onClick={() => setPhoto('')} />
|
|
</div>
|
|
}
|
|
</div>
|
|
</div>
|
|
</section>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</>
|
|
);
|
|
} |