useCameraViewerId.ts 630 B

123456789101112131415
  1. import { useState } from 'react';
  2. /**
  3. * A random id for one camera viewer, stable while it is mounted. It goes on
  4. * the viewer's stream URL and on its /camera/stop. Every camera, external ones
  5. * included, now shares one upstream per printer, and the stop leaves it running
  6. * while another viewer still watches; the id names the viewer in the request.
  7. *
  8. * Math.random, not crypto.randomUUID: that needs a secure context, and
  9. * Bambuddy is often opened over plain http on the LAN.
  10. */
  11. export function useCameraViewerId(): string {
  12. const [id] = useState(() => `v${Math.random().toString(36).slice(2, 12)}`);
  13. return id;
  14. }