Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,67 @@
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import { BrowserRouter as Router, Routes, Route, useLocation } from 'react-router-dom';
import { useEffect } from 'react';

import LoginSwitcher from './components/switcher/LoginSwitcher';
import BuildingSelectPage from './pages/BuildingSelectPage';
import ZoneSelectPage from './pages/ZoneSelectPage';
import QRCodeScanner from './pages/QRCodeScanner';

function GlobalPopStateHandler() {
useEffect(() => {
const handlePopState = () => {
const currentPath = window.location.pathname;
console.log("[Global] popstate 발생:", currentPath);

const scannerInstance = (window as any).scannerRef?.current;
if (scannerInstance?.clear) {
scannerInstance.clear()
.then(() => {
console.log("[Global] 카메라 정리 완료 (popstate)");
})
.catch(console.error)
.finally(() => {
(window as any).scannerRef = null;
});
} else {
console.log("[Global] scannerRef가 없음");
}
};

window.addEventListener("popstate", handlePopState);
return () => window.removeEventListener("popstate", handlePopState);
}, []);

return null;
}

function LocationWatcher() {
const location = useLocation();

useEffect(() => {
if (!location.pathname.startsWith('/qr')) {
const scannerInstance = (window as any).scannerRef?.current;
if (scannerInstance?.clear) {
scannerInstance.clear()
.then(() => {
console.log("[Global] 카메라 정리 완료 (location change)");
})
.catch(console.error)
.finally(() => {
(window as any).scannerRef = null;
});
}
}
}, [location.pathname]);

return null;
}

function App() {

return (
<Router>
<GlobalPopStateHandler />
<LocationWatcher />
<Routes>
<Route path="/" element={<LoginSwitcher />} />
<Route path="/login" element={<LoginSwitcher />} />
Expand Down
19 changes: 3 additions & 16 deletions src/pages/QRCodeScanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ const QRCodeScanner = () => {
}
};

scannerRef.current = scanner;
(window as any).scannerRef = scannerRef;

showMessage(STATUS_MESSAGES.SCANNING);

scanner.render(
Expand Down Expand Up @@ -118,22 +121,6 @@ const QRCodeScanner = () => {
() => {}
);

scannerRef.current = scanner;

return () => {
const instance = scannerRef.current;
if (instance && instance.qrCodeScanner) {
const html5QrInstance = instance.qrCodeScanner;

html5QrInstance
.stop()
.then(() => html5QrInstance.clear())
.catch(console.error)
.finally(() => {
scannerRef.current = null;
});
}
};
}, []);

useEffect(() => {
Expand Down