Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix “Back” button sometimes redirecting out of Mastodon (#25281)
- Loading branch information
1 parent
d0f0020
commit a442a1d
Showing
4 changed files
with
39 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,46 @@ | ||
import type { PropsWithChildren } from 'react'; | ||
import React from 'react'; | ||
|
||
import type { History } from 'history'; | ||
import { createBrowserHistory } from 'history'; | ||
import { Router as OriginalRouter } from 'react-router'; | ||
|
||
import { layoutFromWindow } from 'mastodon/is_mobile'; | ||
|
||
const browserHistory = createBrowserHistory(); | ||
interface MastodonLocationState { | ||
fromMastodon?: boolean; | ||
mastodonModalKey?: string; | ||
} | ||
|
||
const browserHistory = createBrowserHistory< | ||
MastodonLocationState | undefined | ||
>(); | ||
const originalPush = browserHistory.push.bind(browserHistory); | ||
const originalReplace = browserHistory.replace.bind(browserHistory); | ||
|
||
browserHistory.push = (path: string, state?: MastodonLocationState) => { | ||
state = state ?? {}; | ||
state.fromMastodon = true; | ||
|
||
browserHistory.push = (path: string, state: History.LocationState) => { | ||
if (layoutFromWindow() === 'multi-column' && !path.startsWith('/deck')) { | ||
originalPush(`/deck${path}`, state); | ||
} else { | ||
originalPush(path, state); | ||
} | ||
}; | ||
|
||
browserHistory.replace = (path: string, state?: MastodonLocationState) => { | ||
if (browserHistory.location.state?.fromMastodon) { | ||
state = state ?? {}; | ||
state.fromMastodon = true; | ||
} | ||
|
||
if (layoutFromWindow() === 'multi-column' && !path.startsWith('/deck')) { | ||
originalReplace(`/deck${path}`, state); | ||
} else { | ||
originalReplace(path, state); | ||
} | ||
}; | ||
|
||
export const Router: React.FC<PropsWithChildren> = ({ children }) => { | ||
return <OriginalRouter history={browserHistory}>{children}</OriginalRouter>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters