nomadro/src/modules/social/lib/useCaptureCapability.ts
Eric 3e8458581a Require album face photo first, then full-session live video for audit.
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>
2026-11-04 14:53:54 +08:00

92 lines
2.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"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;
}