From 260b3626db1e70e239a8706cad189c9ecbef82fb Mon Sep 17 00:00:00 2001 From: Stanislas Lange Date: Fri, 20 Dec 2024 15:08:37 +0100 Subject: [PATCH] Fix layout when changing current entry --- app/Http/Controllers/FeedController.php | 2 +- resources/js/Pages/Feeds.tsx | 39 ++++++++++++++++++------- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/app/Http/Controllers/FeedController.php b/app/Http/Controllers/FeedController.php index bdefe07a..bc1e4187 100644 --- a/app/Http/Controllers/FeedController.php +++ b/app/Http/Controllers/FeedController.php @@ -64,7 +64,7 @@ public function index(Request $request): \Inertia\Response ], 'feeds' => $feeds, 'entries' => Entry::latest()->limit(10)->get(), - 'currententry' => Entry::whereId($request->query('entry'))->first(), + 'currententry' => Inertia::lazy(fn () => Entry::whereId($request->query('entry'))->first()), ]); } diff --git a/resources/js/Pages/Feeds.tsx b/resources/js/Pages/Feeds.tsx index 4f4b4240..1ff22110 100644 --- a/resources/js/Pages/Feeds.tsx +++ b/resources/js/Pages/Feeds.tsx @@ -5,7 +5,7 @@ import ApplicationLogo from '@/Components/ApplicationLogo'; import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout'; import { User } from '@/types'; import { Split } from '@gfazioli/mantine-split-pane'; -import { Link, usePage } from '@inertiajs/react'; +import { router, usePage } from '@inertiajs/react'; import { ActionIcon, AppShell, @@ -32,7 +32,7 @@ import { IconSearch, IconStar, } from '@tabler/icons-react'; -import { ReactNode, memo } from 'react'; +import { ReactNode, useEffect, useRef } from 'react'; const links = [ { icon: IconBook, label: 'Unread', notifications: 3 }, @@ -40,7 +40,7 @@ const links = [ { icon: IconStar, label: 'Favorites' }, ]; -const EntryListPane = memo(function EntryListPane({ +const EntryListPane = function EntryListPane({ entries, }: { entries: Entry[]; @@ -48,7 +48,16 @@ const EntryListPane = memo(function EntryListPane({ const entryList = entries.map((entry) => (
- +
+ router.visit('feeds', { + only: ['currententry'], + data: { entry: entry.id }, + preserveScroll: true, + preserveState: true, + }) + } + >
{entry.title}
@@ -58,7 +67,7 @@ const EntryListPane = memo(function EntryListPane({ {entry.published_at}
- +
)); @@ -73,16 +82,24 @@ const EntryListPane = memo(function EntryListPane({ {entryList} ); -}); +}; -const CurrentEntryPane = memo(function CurrentEntryPane({ +const CurrentEntryPane = function CurrentEntryPane({ currententry, }: { currententry?: Entry; }) { + const viewport = useRef(null); + const scrollToTop = () => + viewport.current!.scrollTo({ top: 0, behavior: 'smooth' }); + + useEffect(() => { + scrollToTop(); + }, [currententry]); + return ( - + {currententry ? ( @@ -103,7 +120,7 @@ const CurrentEntryPane = memo(function CurrentEntryPane({ ); -}); +}; const Main = function Main({ entries, @@ -129,7 +146,7 @@ const Main = function Main({ ); }; -const NavBar = memo(function Navbar({ +const NavBar = function Navbar({ user, mainLinks, feedLinks, @@ -185,7 +202,7 @@ const NavBar = memo(function Navbar({ ); -}); +}; interface Feed { id: number;