Store challenge frames plus MediaRecorder stream for human review, and soft-gate dating/coffee/meetup until liveness passes. Co-authored-by: Cursor <cursoragent@cursor.com>
92 lines
2.4 KiB
TypeScript
92 lines
2.4 KiB
TypeScript
"use client";
|
||
|
||
import { useEffect, useState } from "react";
|
||
|
||
export type CaptureCapability = {
|
||
ready: boolean;
|
||
/** phone / tablet-ish UA or narrow touch viewport */
|
||
isMobile: boolean;
|
||
/** MediaDevices API present */
|
||
canMedia: boolean;
|
||
/** at least one videoinput after permission/enumerate */
|
||
hasCamera: boolean;
|
||
/**
|
||
* album-first UX:
|
||
* - live-modal: can run getUserMedia + MediaRecorder for audit video
|
||
* - album-only: no camera API
|
||
*/
|
||
mode: "live-modal" | "album-only";
|
||
};
|
||
|
||
function detectMobile(): boolean {
|
||
if (typeof window === "undefined") return false;
|
||
const ua = navigator.userAgent || "";
|
||
const uaMobile = /Android|iPhone|iPad|iPod|Mobile|webOS|BlackBerry|IEMobile|Opera Mini/i.test(
|
||
ua,
|
||
);
|
||
const coarse =
|
||
typeof window.matchMedia === "function" &&
|
||
window.matchMedia("(pointer: coarse)").matches;
|
||
const narrow = window.innerWidth > 0 && window.innerWidth < 820;
|
||
return uaMobile || (coarse && narrow);
|
||
}
|
||
|
||
/**
|
||
* Soft probe only — never call getUserMedia here (avoids permission prompt on join page).
|
||
*/
|
||
async function probeCamera(): Promise<boolean> {
|
||
if (typeof navigator === "undefined") return false;
|
||
const md = navigator.mediaDevices;
|
||
if (!md?.getUserMedia) return false;
|
||
|
||
try {
|
||
if (md.enumerateDevices) {
|
||
const list = await md.enumerateDevices();
|
||
if (list.some((d) => d.kind === "videoinput")) return true;
|
||
}
|
||
} catch {
|
||
/* fall through */
|
||
}
|
||
|
||
// Optimistic: API exists → offer live modal; real open happens on click
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* PC/手机 + 是否有摄像头 → 头像采集 UX 分流。
|
||
* 照片优先走相册;真人核验统一走 live-modal(可录整段视频)。
|
||
*/
|
||
export function useCaptureCapability(): CaptureCapability {
|
||
const [state, setState] = useState<CaptureCapability>({
|
||
ready: false,
|
||
isMobile: false,
|
||
canMedia: false,
|
||
hasCamera: false,
|
||
mode: "album-only",
|
||
});
|
||
|
||
useEffect(() => {
|
||
let cancelled = false;
|
||
const isMobile = detectMobile();
|
||
const canMedia = Boolean(navigator.mediaDevices?.getUserMedia);
|
||
|
||
void (async () => {
|
||
const hasCamera = canMedia ? await probeCamera() : false;
|
||
if (cancelled) return;
|
||
setState({
|
||
ready: true,
|
||
isMobile,
|
||
canMedia,
|
||
hasCamera,
|
||
mode: hasCamera ? "live-modal" : "album-only",
|
||
});
|
||
})();
|
||
|
||
return () => {
|
||
cancelled = true;
|
||
};
|
||
}, []);
|
||
|
||
return state;
|
||
}
|