refactor: use persistant layout

This commit is contained in:
Sonny
2024-05-27 00:28:36 +02:00
committed by Sonny
parent e03952de1c
commit 76847ff76b
9 changed files with 169 additions and 142 deletions

View File

@@ -52,7 +52,7 @@ export default class UsersController {
avatarUrl, avatarUrl,
token, token,
providerType: 'google', providerType: 'google',
isAdmin: userCount[0].total === '0', isAdmin: userCount[0].total === '0' ? true : undefined,
} }
); );

View File

@@ -1,5 +1,5 @@
import ReactDOMServer from 'react-dom/server';
import { createInertiaApp } from '@inertiajs/react'; import { createInertiaApp } from '@inertiajs/react';
import ReactDOMServer from 'react-dom/server';
export default function render(page: any) { export default function render(page: any) {
return createInertiaApp({ return createInertiaApp({

View File

@@ -0,0 +1,90 @@
import { router } from '@inertiajs/react';
import { route } from '@izzyjs/route/client';
import { ReactNode, useMemo, useState } from 'react';
import { ActiveCollectionContext } from '~/contexts/active_collection_context';
import CollectionsContext from '~/contexts/collections_context';
import FavoritesContext from '~/contexts/favorites_context';
import GlobalHotkeysContext from '~/contexts/global_hotkeys_context';
import useShortcut from '~/hooks/use_shortcut';
import { appendCollectionId } from '~/lib/navigation';
import { CollectionWithLinks, Link } from '~/types/app';
export default function DashboardProviders(
props: Readonly<{
children: ReactNode;
collections: CollectionWithLinks[];
activeCollection: CollectionWithLinks;
}>
) {
const [globalHotkeysEnabled, setGlobalHotkeysEnabled] =
useState<boolean>(true);
const [collections, setCollections] = useState<CollectionWithLinks[]>(
props.collections
);
const [activeCollection, setActiveCollection] =
useState<CollectionWithLinks | null>(
props.activeCollection || collections?.[0]
);
const handleChangeCollection = (collection: CollectionWithLinks) => {
setActiveCollection(collection);
router.visit(appendCollectionId(route('dashboard').url, collection.id));
};
const favorites = useMemo<Link[]>(
() =>
collections.reduce((acc, collection) => {
collection.links.forEach((link) =>
link.favorite ? acc.push(link) : null
);
return acc;
}, [] as Link[]),
[collections]
);
const collectionsContextValue = useMemo(
() => ({ collections, setCollections }),
[collections]
);
const activeCollectionContextValue = useMemo(
() => ({ activeCollection, setActiveCollection: handleChangeCollection }),
[activeCollection, handleChangeCollection]
);
const favoritesContextValue = useMemo(() => ({ favorites }), [favorites]);
const globalHotkeysContextValue = useMemo(
() => ({
globalHotkeysEnabled,
setGlobalHotkeysEnabled,
}),
[globalHotkeysEnabled]
);
useShortcut(
'OPEN_CREATE_LINK_KEY',
() =>
router.visit(
appendCollectionId(route('link.create-form').url, activeCollection?.id)
),
{
enabled: globalHotkeysEnabled,
}
);
useShortcut(
'OPEN_CREATE_COLLECTION_KEY',
() => router.visit(route('collection.create-form').url),
{
enabled: globalHotkeysEnabled,
}
);
return (
<CollectionsContext.Provider value={collectionsContextValue}>
<ActiveCollectionContext.Provider value={activeCollectionContextValue}>
<FavoritesContext.Provider value={favoritesContextValue}>
<GlobalHotkeysContext.Provider value={globalHotkeysContextValue}>
{props.children}
</GlobalHotkeysContext.Provider>
</FavoritesContext.Provider>
</ActiveCollectionContext.Provider>
</CollectionsContext.Provider>
);
}

View File

@@ -7,8 +7,9 @@ import DarkThemeContextProvider from '~/contexts/dark_theme_context';
export default function BaseLayout({ children }: { children: ReactNode }) { export default function BaseLayout({ children }: { children: ReactNode }) {
const { i18n } = useTranslation(); const { i18n } = useTranslation();
dayjs.locale(i18n.language); dayjs.locale(i18n.language);
console.log('a');
return ( return (
<DarkThemeContextProvider> <DarkThemeContextProvider key="a">
<ContextThemeProvider>{children}</ContextThemeProvider> <ContextThemeProvider>{children}</ContextThemeProvider>
</DarkThemeContextProvider> </DarkThemeContextProvider>
); );

View File

@@ -1,22 +1,15 @@
import styled from '@emotion/styled'; import styled from '@emotion/styled';
import { router } from '@inertiajs/react'; import { ReactNode, useEffect } from 'react';
import { route } from '@izzyjs/route/client';
import { ReactNode, useEffect, useMemo, useState } from 'react';
import { useSwipeable } from 'react-swipeable'; import { useSwipeable } from 'react-swipeable';
import CollectionContainer from '~/components/dashboard/collection/collection_container'; import CollectionContainer from '~/components/dashboard/collection/collection_container';
import CollectionList from '~/components/dashboard/collection/list/collection_list'; import CollectionList from '~/components/dashboard/collection/list/collection_list';
import DashboardProviders from '~/components/dashboard/dashboard_provider';
import SideNavigation from '~/components/dashboard/side_nav/side_navigation'; import SideNavigation from '~/components/dashboard/side_nav/side_navigation';
import SwiperHandler from '~/components/dashboard/swiper_handler'; import SwiperHandler from '~/components/dashboard/swiper_handler';
import DashboardLayout from '~/components/layouts/dashboard_layout'; import DashboardLayout from '~/components/layouts/dashboard_layout';
import { ActiveCollectionContext } from '~/contexts/active_collection_context';
import CollectionsContext from '~/contexts/collections_context';
import FavoritesContext from '~/contexts/favorites_context';
import GlobalHotkeysContext from '~/contexts/global_hotkeys_context';
import { useMediaQuery } from '~/hooks/use_media_query'; import { useMediaQuery } from '~/hooks/use_media_query';
import useToggle from '~/hooks/use_modal'; import useToggle from '~/hooks/use_modal';
import useShortcut from '~/hooks/use_shortcut'; import { CollectionWithLinks } from '~/types/app';
import { appendCollectionId } from '~/lib/navigation';
import { CollectionWithLinks, Link } from '~/types/app';
interface DashboardPageProps { interface DashboardPageProps {
collections: CollectionWithLinks[]; collections: CollectionWithLinks[];
@@ -28,7 +21,7 @@ const SideBar = styled.div(({ theme }) => ({
marginRight: '5px', marginRight: '5px',
})); }));
export default function DashboardPage(props: Readonly<DashboardPageProps>) { function DashboardPage(props: Readonly<DashboardPageProps>) {
const isMobile = useMediaQuery('(max-width: 768px)'); const isMobile = useMediaQuery('(max-width: 768px)');
const { isShowing, open, close } = useToggle(); const { isShowing, open, close } = useToggle();
const handlers = useSwipeable({ const handlers = useSwipeable({
@@ -43,7 +36,6 @@ export default function DashboardPage(props: Readonly<DashboardPageProps>) {
}, [close, isMobile, isShowing]); }, [close, isMobile, isShowing]);
return ( return (
<DashboardLayout>
<DashboardProviders <DashboardProviders
collections={props.collections} collections={props.collections}
activeCollection={props.activeCollection} activeCollection={props.activeCollection}
@@ -58,85 +50,8 @@ export default function DashboardPage(props: Readonly<DashboardPageProps>) {
<CollectionList /> <CollectionList />
</SwiperHandler> </SwiperHandler>
</DashboardProviders> </DashboardProviders>
</DashboardLayout>
); );
} }
function DashboardProviders(
props: Readonly<{
children: ReactNode;
collections: CollectionWithLinks[];
activeCollection: CollectionWithLinks;
}>
) {
const [globalHotkeysEnabled, setGlobalHotkeysEnabled] =
useState<boolean>(true);
const [collections, setCollections] = useState<CollectionWithLinks[]>(
props.collections
);
const [activeCollection, setActiveCollection] =
useState<CollectionWithLinks | null>(
props.activeCollection || collections?.[0]
);
const handleChangeCollection = (collection: CollectionWithLinks) => { DashboardPage.layout = (page: ReactNode) => <DashboardLayout children={page} />;
setActiveCollection(collection); export default DashboardPage;
router.visit(appendCollectionId(route('dashboard').url, collection.id));
};
const favorites = useMemo<Link[]>(
() =>
collections.reduce((acc, collection) => {
collection.links.forEach((link) =>
link.favorite ? acc.push(link) : null
);
return acc;
}, [] as Link[]),
[collections]
);
const collectionsContextValue = useMemo(
() => ({ collections, setCollections }),
[collections]
);
const activeCollectionContextValue = useMemo(
() => ({ activeCollection, setActiveCollection: handleChangeCollection }),
[activeCollection, handleChangeCollection]
);
const favoritesContextValue = useMemo(() => ({ favorites }), [favorites]);
const globalHotkeysContextValue = useMemo(
() => ({
globalHotkeysEnabled,
setGlobalHotkeysEnabled,
}),
[globalHotkeysEnabled]
);
useShortcut(
'OPEN_CREATE_LINK_KEY',
() =>
router.visit(
appendCollectionId(route('link.create-form').url, activeCollection?.id)
),
{
enabled: globalHotkeysEnabled,
}
);
useShortcut(
'OPEN_CREATE_COLLECTION_KEY',
() => router.visit(route('collection.create-form').url),
{
enabled: globalHotkeysEnabled,
}
);
return (
<CollectionsContext.Provider value={collectionsContextValue}>
<ActiveCollectionContext.Provider value={activeCollectionContextValue}>
<FavoritesContext.Provider value={favoritesContextValue}>
<GlobalHotkeysContext.Provider value={globalHotkeysContextValue}>
{props.children}
</GlobalHotkeysContext.Provider>
</FavoritesContext.Provider>
</ActiveCollectionContext.Provider>
</CollectionsContext.Provider>
);
}

View File

@@ -1,4 +1,5 @@
import styled from '@emotion/styled'; import styled from '@emotion/styled';
import { ReactNode } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { AiFillFolderOpen } from 'react-icons/ai'; import { AiFillFolderOpen } from 'react-icons/ai';
import { FaUser } from 'react-icons/fa'; import { FaUser } from 'react-icons/fa';
@@ -19,10 +20,10 @@ const PageContent = styled.div({
flexDirection: 'column', flexDirection: 'column',
}); });
export default function HomePage() { function HomePage() {
const { t } = useTranslation(); const { t } = useTranslation();
return ( return (
<ContentLayout> <>
<HeroHeader /> <HeroHeader />
<PageContent> <PageContent>
<AboutList> <AboutList>
@@ -59,6 +60,9 @@ export default function HomePage() {
</AboutList> </AboutList>
<WebsitePreview /> <WebsitePreview />
</PageContent> </PageContent>
</ContentLayout> </>
); );
} }
HomePage.layout = (page: ReactNode) => <ContentLayout children={page} />;
export default HomePage;

View File

@@ -1,6 +1,7 @@
import styled from '@emotion/styled'; import styled from '@emotion/styled';
import { Head, Link } from '@inertiajs/react'; import { Head, Link } from '@inertiajs/react';
import { route } from '@izzyjs/route/client'; import { route } from '@izzyjs/route/client';
import { ReactNode } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { FcGoogle } from 'react-icons/fc'; import { FcGoogle } from 'react-icons/fc';
import Button from '~/components/common/form/_button'; import Button from '~/components/common/form/_button';
@@ -51,10 +52,9 @@ const ButtonLink = styled(Button.withComponent('a'))({
justifyContent: 'center', justifyContent: 'center',
}); });
export default function LoginPage() { function LoginPage() {
const { t } = useTranslation(); const { t } = useTranslation();
return ( return (
<ContentLayout css={{ display: 'flex', alignItems: 'center' }}>
<LoginContainer css={{ width: '500px', marginTop: '50px' }}> <LoginContainer css={{ width: '500px', marginTop: '50px' }}>
<Head title={t('login:title')} /> <Head title={t('login:title')} />
<img <img
@@ -77,6 +77,13 @@ export default function LoginPage() {
<Link href={route('terms').url}>{t('common:terms')}</Link> <Link href={route('terms').url}>{t('common:terms')}</Link>
</AgreementText> </AgreementText>
</LoginContainer> </LoginContainer>
</ContentLayout>
); );
} }
LoginPage.layout = (page: ReactNode) => (
<ContentLayout
children={page}
css={{ display: 'flex', alignItems: 'center' }}
/>
);
export default LoginPage;

View File

@@ -1,10 +1,11 @@
import { ReactNode } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import LegalContentLayout from '~/components/layouts/legal_content_layout'; import LegalContentLayout from '~/components/layouts/legal_content_layout';
export default function TermsPage() { function PrivacyPage() {
const { t } = useTranslation('privacy'); const { t } = useTranslation('privacy');
return ( return (
<LegalContentLayout> <>
<h1>{t('title')}</h1> <h1>{t('title')}</h1>
<p>{t('edited_at', { date: '19/11/2023' })}</p> <p>{t('edited_at', { date: '19/11/2023' })}</p>
<p>{t('welcome')}</p> <p>{t('welcome')}</p>
@@ -39,6 +40,11 @@ export default function TermsPage() {
<h2>{t('gdpr.title')}</h2> <h2>{t('gdpr.title')}</h2>
<p>{t('gdpr.description')}</p> <p>{t('gdpr.description')}</p>
</LegalContentLayout> </>
); );
} }
PrivacyPage.layout = (page: ReactNode) => (
<LegalContentLayout children={page} />
);
export default PrivacyPage;

View File

@@ -1,12 +1,13 @@
import { Link } from '@inertiajs/react'; import { Link } from '@inertiajs/react';
import { route } from '@izzyjs/route/client'; import { route } from '@izzyjs/route/client';
import { ReactNode } from 'react';
import { Trans, useTranslation } from 'react-i18next'; import { Trans, useTranslation } from 'react-i18next';
import LegalContentLayout from '~/components/layouts/legal_content_layout'; import LegalContentLayout from '~/components/layouts/legal_content_layout';
export default function TermsPage() { function TermsPage() {
const { t } = useTranslation('terms'); const { t } = useTranslation('terms');
return ( return (
<LegalContentLayout> <>
<h1>{t('title')}</h1> <h1>{t('title')}</h1>
<p>{t('edited_at', { date: '19/11/2023' })}</p> <p>{t('edited_at', { date: '19/11/2023' })}</p>
<p>{t('welcome')}</p> <p>{t('welcome')}</p>
@@ -48,6 +49,9 @@ export default function TermsPage() {
<h2>{t('cancel.title')}</h2> <h2>{t('cancel.title')}</h2>
<p>{t('cancel.description')}</p> <p>{t('cancel.description')}</p>
</LegalContentLayout> </>
); );
} }
TermsPage.layout = (page: ReactNode) => <LegalContentLayout children={page} />;
export default TermsPage;