Skip to content

Commit

Permalink
Merge pull request #694 from near/develop
Browse files Browse the repository at this point in the history
weekly promotion of develop to main
  • Loading branch information
charleslavon authored Oct 12, 2023
2 parents ec842f0 + 3bd00fc commit 55590ff
Show file tree
Hide file tree
Showing 16 changed files with 373 additions and 248 deletions.
126 changes: 126 additions & 0 deletions src/components/NotificationsAlert.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { useCallback, useEffect, useState } from 'react';

import { VmComponent } from '@/components/vm/VmComponent';
import { useBosComponents } from '@/hooks/useBosComponents';
import { useIosDevice } from '@/hooks/useIosDevice';
import { useAuthStore } from '@/stores/auth';
import {
handleOnCancel,
handleTurnOn,
recommendedIosVersionForNotifications,
showNotificationModal,
} from '@/utils/notifications';
import { isNotificationSupported, isPermisionGranted, isPushManagerSupported } from '@/utils/notificationsHelpers';
import { getNotificationLocalStorage, setNotificationsSessionStorage } from '@/utils/notificationsLocalStorage';
import type { TosData } from '@/utils/types';

type Props = {
tosData: TosData | null;
};

export const NotificationsAlert = ({ tosData }: Props) => {
const signedIn = useAuthStore((store) => store.signedIn);
const components = useBosComponents();
const [showNotificationModalState, setShowNotificationModalState] = useState(false);
const accountId = useAuthStore((store) => store.accountId);
const [isHomeScreenApp, setHomeScreenApp] = useState(false);
const [iosHomeScreenPrompt, setIosHomeScreenPrompt] = useState(false);
const { isIosDevice, versionOfIos } = useIosDevice();
const { showOnTS, subscribeStarted, subscribeError } = getNotificationLocalStorage() || {};

const handleModalCloseOnEsc = useCallback(() => {
setShowNotificationModalState(false);
}, []);

const handleHomeScreenClose = useCallback(() => {
setIosHomeScreenPrompt(false);
}, []);

const turnNotificationsOn = useCallback(() => {
// for iOS devices, show a different modal asking the user to add the app to their home screen
// if the user has already added the app to their home screen, show the regular notification modal
if (isIosDevice && !isHomeScreenApp) {
setIosHomeScreenPrompt(true);
setShowNotificationModalState(false);
return;
}
return handleTurnOn(accountId, () => {
setShowNotificationModalState(false);
});
}, [accountId, isIosDevice, isHomeScreenApp]);

const pauseNotifications = useCallback(() => {
handleOnCancel();
setShowNotificationModalState(false);
}, []);

const checkNotificationModal = useCallback(() => {
if (tosData && tosData.agreementsForUser.length > 0) {
// show notification modal for new users
const tosAccepted =
tosData.agreementsForUser[tosData.agreementsForUser.length - 1].value === tosData.latestTosVersion;
// check if user has already turned on notifications
const showNotificationPrompt = showNotificationModal();

if (!subscribeError && showNotificationPrompt && tosAccepted && (!showOnTS || !iosHomeScreenPrompt)) {
setTimeout(() => {
setShowNotificationModalState(showNotificationPrompt);
}, 3000);
}
}
}, [tosData, subscribeError, showOnTS, iosHomeScreenPrompt]);

useEffect(() => {
if (!signedIn) {
return;
}
checkNotificationModal();
}, [signedIn, checkNotificationModal]);

useEffect(() => {
if (isIosDevice) {
setHomeScreenApp(window.matchMedia('(display-mode: standalone)').matches);
}
}, [isIosDevice]);

useEffect(() => {
if (isIosDevice) {
window.matchMedia('(display-mode: standalone)').addEventListener('change', (e) => setHomeScreenApp(e.matches));
// Remove event listener
return () => {
window.matchMedia('(display-mode: standalone)').removeEventListener('change', () => setHomeScreenApp(false));
};
}
}, [isIosDevice]);

if (!signedIn) return null;

return (
<>
<VmComponent
src={components.nearOrg.notifications.alert}
props={{
open: showNotificationModalState,
handleTurnOn: turnNotificationsOn,
handleOnCancel: pauseNotifications,
isNotificationSupported,
isPermisionGranted,
isPushManagerSupported,
setNotificationsSessionStorage,
onOpenChange: handleModalCloseOnEsc,
iOSDevice: isIosDevice,
iOSVersion: versionOfIos,
recomendedIOSVersion: recommendedIosVersionForNotifications,
loading: subscribeStarted,
}}
/>
<VmComponent
src={components.nearOrg.notifications.iosHomeScreenAlert}
props={{
open: iosHomeScreenPrompt,
onOpenChange: handleHomeScreenClose,
}}
/>
</>
);
};
10 changes: 5 additions & 5 deletions src/components/navigation/CurrentComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { VmComponent } from '@/components/vm/VmComponent';
import { useBosComponents } from '@/hooks/useBosComponents';
import { useCurrentComponentStore } from '@/stores/current-component';

const StyledCurrentComponent = styled.div`
const Wrapper = styled.div`
border: 1px solid #eeeeec;
background-color: #f9f9f8;
border-radius: 4px;
min-height: 100%;
min-width: 253px;
.title {
color: #868682;
Expand All @@ -31,7 +31,7 @@ const StyledCurrentComponent = styled.div`
justify-content: center;
}
> div {
padding: 15px;
padding: 15px 15px 0;
div:nth-child(1) {
flex-direction: column;
text-align: center;
Expand Down Expand Up @@ -64,7 +64,7 @@ export const CurrentComponent = () => {
if (!src) return null;

return (
<StyledCurrentComponent className="current-component">
<Wrapper>
<div className="title">Current Component</div>
<VmComponent
src={components.componentSummary}
Expand All @@ -74,6 +74,6 @@ export const CurrentComponent = () => {
showTags: true,
}}
/>
</StyledCurrentComponent>
</Wrapper>
);
};
4 changes: 2 additions & 2 deletions src/components/navigation/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ export const Navigation = () => {
const [matches, setMatches] = useState(true);

useEffect(() => {
setMatches(window.matchMedia('(min-width: 1120px)').matches);
setMatches(window.matchMedia('(min-width: 1024px)').matches);
}, []);

useEffect(() => {
window.matchMedia('(min-width: 1120px)').addEventListener('change', (e) => setMatches(e.matches));
window.matchMedia('(min-width: 1024px)').addEventListener('change', (e) => setMatches(e.matches));
}, []);

return (
Expand Down
2 changes: 1 addition & 1 deletion src/components/navigation/desktop/DesktopNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const Search = styled.div`
border: 1px solid var(--sand6);
background-color: white;
font-size: 16px;
margin-left: 1rem;
margin-left: 2rem;
width: 200px;
transition: all 200ms;
Expand Down
61 changes: 35 additions & 26 deletions src/components/navigation/desktop/MainNavigationMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import * as NavigationMenu from '@radix-ui/react-navigation-menu';
import Link from 'next/link';
import styled from 'styled-components';

import { Button } from '@/components/lib/Button';
import { useCurrentComponentStore } from '@/stores/current-component';
import { recordMouseEnter } from '@/utils/analytics';

import { CurrentComponent } from '../CurrentComponent';
import { navigationCategories } from '../navigation-categories';

const Wrapper = styled.div`
position: relative;
display: flex;
justify-content: center;
z-index: 1;
flex-grow: 1;
padding: 0 1rem;
Expand Down Expand Up @@ -92,7 +94,7 @@ const Container = styled.div`
const NavLink = styled(NavigationMenu.Link)`
display: inline-block;
min-width: 120px;
padding: 8px 0;
padding: 7px 0;
font: var(--text-s);
color: var(--sand10);
transition: color 200ms;
Expand All @@ -109,31 +111,30 @@ const NavLink = styled(NavigationMenu.Link)`
const Section = styled.div`
display: flex;
flex-direction: column;
padding: 24px 24px 0;
&:nth-child(1),
&:nth-child(2) {
padding-top: 0;
}
&:nth-child(odd) {
border-right: 1px solid var(--sand4);
}
padding: 0 24px 0;
gap: 24px;
border-right: 1px solid var(--sand4);
&:first-child:last-child {
border-right: none;
}
`;

const CurrentComponentSection = styled.div`
padding: 0 24px 0;
`;

const SectionTitle = styled.p`
font: var(--text-s);
color: var(--sand12);
font-weight: 600;
padding: 8px 0;
padding: 7px 0;
margin: 0;
`;

export const MainNavigationMenu = () => {
const currentComponentSrc = useCurrentComponentStore((store) => store.src);

return (
<Wrapper>
<NavRoot delayDuration={0}>
Expand All @@ -146,19 +147,27 @@ export const MainNavigationMenu = () => {

<NavContent>
<Container>
{category.sections.map((section) => (
<Section key={section.title}>
{section.title && <SectionTitle>{section.title}</SectionTitle>}

{section.links.map((link) => (
<NavLink key={link.title} asChild>
<Link href={link.url} target={link.url.indexOf('http') === 0 ? '_blank' : undefined}>
{link.title}
</Link>
</NavLink>
))}
</Section>
))}
<Section>
{category.sections.map((section) => (
<div key={section.title}>
{section.title && <SectionTitle>{section.title}</SectionTitle>}

{section.links.map((link) => (
<NavLink key={link.title} asChild>
<Link href={link.url} target={link.url.indexOf('http') === 0 ? '_blank' : undefined}>
{link.title}
</Link>
</NavLink>
))}
</div>
))}
</Section>

{currentComponentSrc && category.title === 'Develop' && (
<CurrentComponentSection>
<CurrentComponent />
</CurrentComponentSection>
)}
</Container>
</NavContent>
</NavItem>
Expand Down
Loading

2 comments on commit 55590ff

@vercel
Copy link

@vercel vercel bot commented on 55590ff Oct 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 55590ff Oct 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.