MCPcopy
hub / github.com/CapSoftware/Cap / createCameraMutation

Function createCameraMutation

apps/desktop/src/utils/queries.ts:273–330  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

271}
272
273export function createCameraMutation() {
274 const { setOptions, rawOptions } = useRecordingOptions();
275
276 const rawMutate = async (
277 model: DeviceOrModelID | null,
278 skipCameraWindow?: boolean,
279 ) => {
280 const before = rawOptions.cameraID ? { ...rawOptions.cameraID } : null;
281 setOptions("cameraID", reconcile(model));
282 await commands
283 .setCameraInput(model, skipCameraWindow ?? null)
284 .catch(async (e) => {
285 const message =
286 typeof e === "string"
287 ? e
288 : e instanceof Error
289 ? e.message
290 : String(e);
291
292 if (
293 message.includes("DeviceNotFound") ||
294 message.includes("CameraTimeout") ||
295 message.includes("Failed to initialize camera")
296 ) {
297 setOptions("cameraID", null);
298 console.warn("Selected camera is unavailable.");
299 return;
300 }
301
302 if (JSON.stringify(before) === JSON.stringify(model) || !before) {
303 setOptions("cameraID", null);
304 } else setOptions("cameraID", reconcile(before));
305
306 throw e;
307 });
308
309 if (model && !skipCameraWindow) {
310 getCurrentWindow().setFocus();
311 }
312 };
313
314 const setCameraInput = useMutation(() => ({
315 mutationFn: (args: {
316 model: DeviceOrModelID | null;
317 skipCameraWindow?: boolean;
318 }) => rawMutate(args.model, args.skipCameraWindow),
319 }));
320
321 return new Proxy(
322 setCameraInput as typeof setCameraInput & { rawMutate: typeof rawMutate },
323 {
324 get(target, key) {
325 if (key === "rawMutate") return rawMutate;
326 return Reflect.get(target, key);
327 },
328 },
329 );
330}

Callers 4

NativeCameraPreviewPageFunction · 0.90
RecordingControlsFunction · 0.90
ClipsSidebarInnerFunction · 0.90
PageFunction · 0.90

Calls 2

useRecordingOptionsFunction · 0.90
rawMutateFunction · 0.85

Tested by

no test coverage detected