Skip to content

Commit

Permalink
Support different behavior when embedded
Browse files Browse the repository at this point in the history
  • Loading branch information
diegocouto committed Jan 30, 2022
1 parent bea26a7 commit 50b3390
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
4 changes: 3 additions & 1 deletion components/containers/Screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useRouter } from 'next/router';
import React from 'react';
import styled from 'styled-components';

import { useEmbeddingState } from '../../hooks/useEmbeddingState';
import { Brand } from '../../utils/constants/app';
import AppFooter from '../navigation/AppFooter';
import AppHeader from '../navigation/AppHeader';
Expand All @@ -18,6 +19,7 @@ interface Props {
export default function Screen({ children, description, ...props }: React.PropsWithChildren<Props>) {
const { t } = useTranslation('common');
const { asPath } = useRouter();
const { isEmbedded } = useEmbeddingState();

const basePath = process.env.NEXT_PUBLIC_URL || '';
const basePageTitle = t('pageTitle', { brand: Brand.name });
Expand Down Expand Up @@ -60,7 +62,7 @@ export default function Screen({ children, description, ...props }: React.PropsW
<AppWrapper>
<AppHeader isCompact={props.isCompact} isMinimal={props.isMinimal} />
<AppContentWrapper>{children}</AppContentWrapper>
<AppFooter />
{!isEmbedded && <AppFooter />}
</AppWrapper>
</>
);
Expand Down
10 changes: 7 additions & 3 deletions components/navigation/AppHeader/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Link from 'next/link';
import React from 'react';
import styled from 'styled-components';

import { useEmbeddingState } from '../../../hooks/useEmbeddingState';
import { Brand } from '../../../utils/constants/app';
import SearchInput from './SearchInput';

Expand All @@ -13,13 +14,14 @@ interface Props {

export default function AppHeader({ isCompact, isMinimal }: Props) {
const { t } = useTranslation('common');
const { isEmbedded } = useEmbeddingState();

return (
<Wrapper>
<Container>
<NavigationBar>
<Link href="/" passHref>
<LogoLink>
<LogoLink hidden={isEmbedded}>
<Logo />
</LogoLink>
</Link>
Expand Down Expand Up @@ -60,7 +62,9 @@ const NavigationBar = styled.div.attrs({

const SearchBar = styled.div.attrs({ className: 'space-y-2' })``;

const LogoLink = styled.a``;
const LogoLink = styled.a.attrs<{ hidden?: boolean }>(({ hidden }) => ({
className: hidden ? 'invisible' : '',
}))``;

const Logo = styled.img.attrs({
alt: Brand.name,
Expand All @@ -69,7 +73,7 @@ const Logo = styled.img.attrs({
})``;

const NavigationActions = styled.div.attrs({
className: 'flex items-center text-sm space-x-4',
className: 'flex items-center text-sm ml-auto space-x-4',
})``;

const ExternalLink = styled.a.attrs({
Expand Down
31 changes: 31 additions & 0 deletions hooks/useEmbeddingState.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { useRouter } from 'next/router';
import React, { createContext, useContext, useEffect, useState } from 'react';

const embeddingContext = createContext({ isEmbedded: false });

export function EmbeddingStateProvider({ children }: React.PropsWithChildren<unknown>) {
const state = useProvideEmbeddingState();

return <embeddingContext.Provider value={state}>{children}</embeddingContext.Provider>;
}

export const useEmbeddingState = () => {
return useContext(embeddingContext);
};

function useProvideEmbeddingState() {
const [isEmbedded, setIsEmbedded] = useState(false);
const { query } = useRouter();

const currentEmbeddingState = query.embedded;

useEffect(() => {
if (currentEmbeddingState) {
setIsEmbedded(currentEmbeddingState == 'true');
}
}, [currentEmbeddingState]);

return {
isEmbedded,
};
}
10 changes: 7 additions & 3 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import type { AppProps } from 'next/app';
import React from 'react';
import { QueryClient, QueryClientProvider } from 'react-query';

import { EmbeddingStateProvider } from '../hooks/useEmbeddingState';

const queryClient = new QueryClient();

function App({ Component, pageProps }: AppProps) {
return (
<QueryClientProvider client={queryClient}>
<Component {...pageProps} />
</QueryClientProvider>
<EmbeddingStateProvider>
<QueryClientProvider client={queryClient}>
<Component {...pageProps} />
</QueryClientProvider>
</EmbeddingStateProvider>
);
}

Expand Down

1 comment on commit 50b3390

@vercel
Copy link

@vercel vercel bot commented on 50b3390 Jan 30, 2022

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.