2023-07-13 15:18:09 +00:00
|
|
|
import type { PropsWithChildren } from 'react';
|
|
|
|
import React from 'react';
|
|
|
|
|
2023-10-26 11:00:10 +00:00
|
|
|
import { Router as OriginalRouter, useHistory } from 'react-router';
|
2023-07-13 15:18:09 +00:00
|
|
|
|
2023-10-24 13:24:57 +00:00
|
|
|
import type {
|
|
|
|
LocationDescriptor,
|
|
|
|
LocationDescriptorObject,
|
|
|
|
Path,
|
|
|
|
} from 'history';
|
2023-10-19 17:44:55 +00:00
|
|
|
import { createBrowserHistory } from 'history';
|
|
|
|
|
2023-07-13 15:18:09 +00:00
|
|
|
import { layoutFromWindow } from 'mastodon/is_mobile';
|
2023-10-31 16:05:44 +00:00
|
|
|
import { isDevelopment } from 'mastodon/utils/environment';
|
2023-07-13 15:18:09 +00:00
|
|
|
|
2023-07-17 15:32:46 +00:00
|
|
|
interface MastodonLocationState {
|
|
|
|
fromMastodon?: boolean;
|
|
|
|
mastodonModalKey?: string;
|
|
|
|
}
|
|
|
|
|
2023-10-26 11:00:10 +00:00
|
|
|
type LocationState = MastodonLocationState | null | undefined;
|
|
|
|
|
|
|
|
type HistoryPath = Path | LocationDescriptor<LocationState>;
|
|
|
|
|
2024-07-19 15:26:44 +00:00
|
|
|
export const browserHistory = createBrowserHistory<LocationState>();
|
2023-07-13 15:18:09 +00:00
|
|
|
const originalPush = browserHistory.push.bind(browserHistory);
|
2023-07-17 15:32:46 +00:00
|
|
|
const originalReplace = browserHistory.replace.bind(browserHistory);
|
|
|
|
|
2023-10-26 11:00:10 +00:00
|
|
|
export function useAppHistory() {
|
|
|
|
return useHistory<LocationState>();
|
|
|
|
}
|
|
|
|
|
2023-10-24 13:24:57 +00:00
|
|
|
function normalizePath(
|
|
|
|
path: HistoryPath,
|
2023-10-26 11:00:10 +00:00
|
|
|
state?: LocationState,
|
|
|
|
): LocationDescriptorObject<LocationState> {
|
2023-10-24 13:24:57 +00:00
|
|
|
const location = typeof path === 'string' ? { pathname: path } : { ...path };
|
|
|
|
|
|
|
|
if (location.state === undefined && state !== undefined) {
|
|
|
|
location.state = state;
|
|
|
|
} else if (
|
|
|
|
location.state !== undefined &&
|
|
|
|
state !== undefined &&
|
2023-10-31 16:05:44 +00:00
|
|
|
isDevelopment()
|
2023-10-24 13:24:57 +00:00
|
|
|
) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log(
|
|
|
|
'You should avoid providing a 2nd state argument to push when the 1st argument is a location-like object that already has state; it is ignored',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
layoutFromWindow() === 'multi-column' &&
|
2024-09-19 14:58:33 +00:00
|
|
|
location.pathname &&
|
|
|
|
!location.pathname.startsWith('/deck')
|
2023-10-24 13:24:57 +00:00
|
|
|
) {
|
|
|
|
location.pathname = `/deck${location.pathname}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return location;
|
2023-10-19 17:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
browserHistory.push = (path: HistoryPath, state?: MastodonLocationState) => {
|
2023-10-24 13:24:57 +00:00
|
|
|
const location = normalizePath(path, state);
|
2023-07-13 15:18:09 +00:00
|
|
|
|
2023-10-24 13:24:57 +00:00
|
|
|
location.state = location.state ?? {};
|
|
|
|
location.state.fromMastodon = true;
|
2023-10-19 17:44:55 +00:00
|
|
|
|
2023-10-24 13:24:57 +00:00
|
|
|
originalPush(location);
|
2023-07-13 15:18:09 +00:00
|
|
|
};
|
|
|
|
|
2023-10-19 17:44:55 +00:00
|
|
|
browserHistory.replace = (path: HistoryPath, state?: MastodonLocationState) => {
|
2023-10-24 13:24:57 +00:00
|
|
|
const location = normalizePath(path, state);
|
2023-07-17 15:32:46 +00:00
|
|
|
|
2023-10-24 13:24:57 +00:00
|
|
|
if (!location.pathname) return;
|
2023-10-19 17:44:55 +00:00
|
|
|
|
2023-10-24 13:24:57 +00:00
|
|
|
if (browserHistory.location.state?.fromMastodon) {
|
|
|
|
location.state = location.state ?? {};
|
|
|
|
location.state.fromMastodon = true;
|
2023-07-17 15:32:46 +00:00
|
|
|
}
|
2023-10-24 13:24:57 +00:00
|
|
|
|
|
|
|
originalReplace(location);
|
2023-07-17 15:32:46 +00:00
|
|
|
};
|
|
|
|
|
2023-07-13 15:18:09 +00:00
|
|
|
export const Router: React.FC<PropsWithChildren> = ({ children }) => {
|
|
|
|
return <OriginalRouter history={browserHistory}>{children}</OriginalRouter>;
|
|
|
|
};
|