Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add warning for mobile devices #32

Merged
merged 2 commits into from
Oct 14, 2021
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
22 changes: 15 additions & 7 deletions src/components/basket/export-options/crn-script.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ type CRNScriptProps = {
onClose: () => void;
};

type OS = 'Windows' | 'Linux' | 'macOS';
enum OS {
WINDOWS = 'Windows',
LINUX = 'Linux',
MACOS = 'macOS',
}

type Software = {
label: SupportedSoftware;
Expand Down Expand Up @@ -67,28 +71,32 @@ const SOFTWARES: Record<OS, Software[]> = {
const CRNScript = ({isOpen, onClose}: CRNScriptProps) => {
const {allBasketsState: {currentBasket}} = useStore();
const [shortcutKey, setShortcutKey] = useState('c');
const [platform, setPlatform] = useState<OS>('Windows');
const [platform, setPlatform] = useState<OS | undefined>(undefined);
const [softwareLabel, setSoftwareLabel] = useState<Software['label']>();

useEffect(() => {
const browser = Bowser.getParser(window.navigator.userAgent);
const {name} = browser.getOS();
if (name) {
if (name && Object.values(OS).includes(name as OS)) {
setPlatform(name as OS);
}
}, []);

// Update selected software when changing platforms
const previousPlatform = usePrevious(platform);
useEffect(() => {
if (previousPlatform && previousPlatform !== platform && !SOFTWARES[platform].some(s => s.label === softwareLabel)) {
if (
previousPlatform
&& platform
&& previousPlatform !== platform
&& !SOFTWARES[platform].some(s => s.label === softwareLabel)) {
setSoftwareLabel(SOFTWARES[platform][0]?.label ?? undefined);
}
}, [previousPlatform, platform, softwareLabel]);

const isFormValid = useMemo(() => platform && softwareLabel, [platform, softwareLabel]);

const currentSoftware = SOFTWARES[platform].find(s => s.label === softwareLabel);
const currentSoftware = platform ? SOFTWARES[platform].find(s => s.label === softwareLabel) : undefined;

// We're just using this for UI
const {onCopy, hasCopied} = useClipboard('');
Expand Down Expand Up @@ -218,7 +226,7 @@ const CRNScript = ({isOpen, onClose}: CRNScriptProps) => {
}}
>
{
SOFTWARES[platform].map(software => (
platform && SOFTWARES[platform].map(software => (
<Radio key={software.label} value={software.label}>
<WrappedLink href={software.href}>
{software.label}
Expand All @@ -229,7 +237,7 @@ const CRNScript = ({isOpen, onClose}: CRNScriptProps) => {
</RadioGroup>

{
SOFTWARES[platform].length === 0 && (
(!platform || SOFTWARES[platform].length === 0) && (
<Text>Not currently supported. 😔</Text>
)
}
Expand Down
44 changes: 44 additions & 0 deletions src/components/mobile-device-warning.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React, {useEffect} from 'react';
import {
Button,
Modal,
ModalBody,
ModalContent,
ModalFooter,
ModalHeader,
ModalOverlay,
useDisclosure,
} from '@chakra-ui/react';
import Bowser from 'bowser';

const MobileDeviceWarning = () => {
const {isOpen, onClose, onOpen} = useDisclosure();

useEffect(() => {
const browser = Bowser.getParser(window.navigator.userAgent);

if (browser.getPlatform().type !== 'desktop') {
onOpen();
}
}, [onOpen]);

return (
<Modal isOpen={isOpen} onClose={onClose}>
<ModalOverlay/>
<ModalContent>
<ModalHeader>Warning</ModalHeader>
<ModalBody>
This site is primarily made for laptop and desktop use. There's just too much information to effectively display it on mobile devices.
</ModalBody>

<ModalFooter>
<Button colorScheme="blue" variant="ghost" onClick={onClose}>
I understand
</Button>
</ModalFooter>
</ModalContent>
</Modal>
);
};

export default MobileDeviceWarning;
3 changes: 3 additions & 0 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Navbar from 'src/components/navbar';
import RevisionToaster from 'src/components/revision-toaster';
import useRevalidation from 'src/lib/hooks/use-revalidation';
import {CustomNextPage} from 'src/lib/types';
import MobileDeviceWarning from 'src/components/mobile-device-warning';

const theme = extendTheme({
colors: {
Expand Down Expand Up @@ -95,6 +96,8 @@ const MyApp = ({Component, pageProps}: AppProps & {Component: CustomNextPage<any
</Box>
</StateProvider>

<MobileDeviceWarning/>

<RevisionToaster/>
</ChakraProvider>
);
Expand Down