diff --git a/resources/js/hooks/use-go-back.ts b/resources/js/hooks/use-go-back.ts new file mode 100644 index 000000000..1a189889a --- /dev/null +++ b/resources/js/hooks/use-go-back.ts @@ -0,0 +1,23 @@ +import type { InertiaLinkProps } from '@inertiajs/react'; +import { router, usePage } from '@inertiajs/react'; +import LogController from '@/actions/App/Http/Controllers/LogController'; +import { home } from '@/routes'; + +export function useGoBack( + fallback?: NonNullable, +): () => void { + const { auth } = usePage().props; + + return () => { + if (window.history.length > 1) { + window.history.back(); + + return; + } + + router.visit( + fallback ?? + (auth.log === null ? home() : LogController.show(auth.log)), + ); + }; +} diff --git a/resources/js/pages/commit/show.tsx b/resources/js/pages/commit/show.tsx index a2e8f6134..deece6c79 100644 --- a/resources/js/pages/commit/show.tsx +++ b/resources/js/pages/commit/show.tsx @@ -1,13 +1,12 @@ -import { Head, router, usePage } from '@inertiajs/react'; +import { Head } from '@inertiajs/react'; import { ArrowLeft } from 'lucide-react'; -import LogController from '@/actions/App/Http/Controllers/LogController'; import { CommitCard, CommitTombstone } from '@/components/commit-card'; import { CopyLinkButton } from '@/components/copy-link-button'; import { GraphCanvas } from '@/components/graph-canvas'; import { useContinuation } from '@/hooks/use-continuation'; +import { useGoBack } from '@/hooks/use-go-back'; import AppLayout from '@/layouts/app-layout'; import { isDeleted, laneOf, shortHash } from '@/lib/utils'; -import { home } from '@/routes'; import type { Commit, CursorPaginated, @@ -28,7 +27,7 @@ export default function Show({ continuation: ThreadContinuation; replies: CursorPaginated | null; }) { - const { auth } = usePage().props; + const goBack = useGoBack(); const { shown, nextId, size, isCapped, isLoading, loadMore } = useContinuation(continuation); const focusedPosition = continuation.position; @@ -40,16 +39,6 @@ export default function Show({ ? `${commit.body.slice(0, titleLength).trimEnd()}…` : commit.body; - const goBack = () => { - if (window.history.length > 1) { - window.history.back(); - - return; - } - - router.visit(auth.log === null ? home() : LogController.show(auth.log)); - }; - const lastAncestor = ancestors.at(-1); return ( diff --git a/resources/js/pages/user/connections.tsx b/resources/js/pages/user/connections.tsx index 799f89a5b..b102ece4b 100644 --- a/resources/js/pages/user/connections.tsx +++ b/resources/js/pages/user/connections.tsx @@ -8,6 +8,7 @@ import { FollowButton } from '@/components/follow-button'; import { PageTabs } from '@/components/page-tabs'; import { Spinner } from '@/components/ui/spinner'; import { UserAvatar } from '@/components/user-avatar'; +import { useGoBack } from '@/hooks/use-go-back'; import AppLayout from '@/layouts/app-layout'; import { laneColor } from '@/lib/utils'; import { show } from '@/routes/user'; @@ -40,6 +41,7 @@ export default function Connections({ }) { const { auth } = usePage().props; const title = type === 'following' ? 'Following' : 'Followers'; + const goBack = useGoBack(show(user)); return ( <> @@ -47,13 +49,14 @@ export default function Connections({
- - +

{user.name}

diff --git a/resources/js/pages/user/show.tsx b/resources/js/pages/user/show.tsx index 7416d8021..cf0d4cc6b 100644 --- a/resources/js/pages/user/show.tsx +++ b/resources/js/pages/user/show.tsx @@ -1,5 +1,6 @@ import { Head, Link, router, usePage } from '@inertiajs/react'; import { + ArrowLeft, Building2, CalendarDays, Heart, @@ -39,6 +40,7 @@ import { UserModerationStatus, } from '@/components/user-moderation-menu'; import { UserSafetyMenu } from '@/components/user-safety-menu'; +import { useGoBack } from '@/hooks/use-go-back'; import { refreshSharedState, useSharedState } from '@/hooks/use-shared-state'; import AppLayout from '@/layouts/app-layout'; import { commitStateKey } from '@/lib/commit-state'; @@ -88,6 +90,7 @@ export default function Show({ }) { const { auth } = usePage().props; const isCurrentUser = auth.user?.id === user.id; + const goBack = useGoBack(); const lane = laneColor(user.lane); const [highlightedPeriod, setHighlightedPeriod] = useState( null, @@ -116,6 +119,15 @@ export default function Show({
+

{user.name}