Skip to content

Commit

Permalink
modified code so that the mobile camera will (hopefully) work
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeTrevor committed Sep 22, 2021
1 parent 904c56c commit 53f3321
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const App: FC = () => {
</Route>
<Route path="">
<InputOptionsBar
text={text}
setText={setText}
setCameraOpen={setCameraModalOpen}
setFile={(file: File) => {
Expand Down
15 changes: 12 additions & 3 deletions src/components/INPUTS/InputOptionsBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,40 @@ import CameraInput from "./camera/CameraInput";
import { FileInput } from "./file/FileInput";
import WriteInput from "./WriteInput";
import type { booleanSetter, fileSetter, stringSetter } from "src/projectTypes";
import CamMobile from "./camera/CamMobile";

interface inputBarProps {
text: string;
setText: stringSetter;
setCameraOpen: booleanSetter;
setFile: CallableFunction;
}

const InputOptionsBar: FC<inputBarProps> = ({
text,
setText,
setCameraOpen,
setFile,
}) => {
const [camera, setCamera] = useState(<div />);

async function detectCamera() {
//detect if we are on mobile.
let isMobile = /Mobi|Android/i.test(navigator.userAgent);

let camElement = isMobile ? (
<CamMobile setFile={setFile} />
) : (
<CameraInput setCameraOpen={setCameraOpen} />
);

return await navigator.mediaDevices
.getUserMedia({ video: true })
.then((stream) => {
if (stream.getVideoTracks().length > 0) {
setCamera(<CameraInput setCameraOpen={setCameraOpen} />);
setCamera(camElement);
}
});
}

useEffect(() => {
detectCamera();
});
Expand Down
35 changes: 35 additions & 0 deletions src/components/INPUTS/camera/CamMobile.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React from "react";
import type { FC } from "react";
import { misc } from "../../ICONS/ICONS";

interface camMobileProps {
setFile: CallableFunction;
}

const CamMobile: FC<camMobileProps> = ({ setFile }) => {
let hasCamera: boolean;

function handleInput(e: React.ChangeEvent<HTMLInputElement>) {
let files = e.target.files;
if (files) {
let file = files[0];
setFile(file);
}
}

return (
<div>
<input
type="file"
name="image"
accept="image/*"
capture="environment"
onInput={handleInput}
>
{misc.Camera}
</input>
</div>
);
};

export default CamMobile;
2 changes: 0 additions & 2 deletions src/components/INPUTS/camera/CameraInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ interface cameraInputProps {
}

const CameraInput: FC<cameraInputProps> = ({ setCameraOpen }) => {
let hasCamera: boolean;

return (
<div>
<button onClick={() => setCameraOpen(true)} className={"inputOption"}>
Expand Down
2 changes: 1 addition & 1 deletion src/components/INPUTS/file/FileInput.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { FC, useRef, useState } from "react";
import type { fileSetter, stringSetter } from "src/projectTypes";
import type { stringSetter } from "src/projectTypes";
import { misc } from "../../ICONS/ICONS";
import handleFile from "../FileHandler";

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"allowSyntheticDefaultImports": true,
"importsNotUsedAsValues": "error",
"importsNotUsedAsValues": "preserve",
"allowJs": false,
"esModuleInterop": true,
"noFallthroughCasesInSwitch": true,
Expand Down

0 comments on commit 53f3321

Please sign in to comment.