mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-16 12:53:32 +00:00
672267c0b3
* feat: operator key * chore: update demo * chore: version bump * fix: prevent stale keys * refactor: add new fields to validation * style: tweaks to modal arrangement * refactor: prevent parsing http * ux: click anywhere in event to edit * refactor: performance improvements to time input * refactor: performance improvements menu
39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import { PropsWithChildren, useCallback, useContext } from 'react';
|
|
|
|
import { AppContext } from '../../context/AppContext';
|
|
|
|
import PinPage from './PinPage';
|
|
|
|
interface ProtectRouteProps {
|
|
permission: 'editor' | 'operator';
|
|
}
|
|
|
|
export default function ProtectRoute({ permission, children }: PropsWithChildren<ProtectRouteProps>) {
|
|
const isLocal = window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1';
|
|
const { editorAuth, operatorAuth, validate } = useContext(AppContext);
|
|
|
|
const handleValidation = useCallback(
|
|
(pin: string) => {
|
|
return validate(pin, permission);
|
|
},
|
|
[permission, validate],
|
|
);
|
|
|
|
const hasRelevantAuth = () => {
|
|
if (permission === 'editor') {
|
|
return editorAuth;
|
|
}
|
|
if (permission === 'operator') {
|
|
return operatorAuth;
|
|
}
|
|
return false;
|
|
};
|
|
|
|
if (isLocal || hasRelevantAuth()) {
|
|
// eslint-disable-next-line react/jsx-no-useless-fragment -- trying to make typescript happy
|
|
return <>{children}</>;
|
|
}
|
|
|
|
return <PinPage permission={permission} handleValidation={handleValidation} />;
|
|
}
|