2023-07-14 01:18:09 +10:00
|
|
|
import type { PropsWithChildren } from 'react';
|
|
|
|
import React from 'react';
|
|
|
|
|
2023-10-26 22:00:10 +11:00
|
|
|
import { Router as OriginalRouter, useHistory } from 'react-router';
|
2023-07-14 01:18:09 +10:00
|
|
|
|
2023-10-25 00:24:57 +11:00
|
|
|
import type {
|
|
|
|
LocationDescriptor,
|
|
|
|
LocationDescriptorObject,
|
|
|
|
Path,
|
|
|
|
} from 'history';
|
2023-10-20 04:44:55 +11:00
|
|
|
import { createBrowserHistory } from 'history';
|
|
|
|
|
2023-07-14 01:18:09 +10:00
|
|
|
import { layoutFromWindow } from 'mastodon/is_mobile';
|
|
|
|
|
2023-07-18 01:32:46 +10:00
|
|
|
interface MastodonLocationState {
|
|
|
|
fromMastodon?: boolean;
|
|
|
|
mastodonModalKey?: string;
|
|
|
|
}
|
|
|
|
|
2023-10-26 22:00:10 +11:00
|
|
|
type LocationState = MastodonLocationState | null | undefined;
|
|
|
|
|
|
|
|
type HistoryPath = Path | LocationDescriptor<LocationState>;
|
|
|
|
|
|
|
|
const browserHistory = createBrowserHistory<LocationState>();
|
2023-07-14 01:18:09 +10:00
|
|
|
const originalPush = browserHistory.push.bind(browserHistory);
|
2023-07-18 01:32:46 +10:00
|
|
|
const originalReplace = browserHistory.replace.bind(browserHistory);
|
|
|
|
|
2023-10-26 22:00:10 +11:00
|
|
|
export function useAppHistory() {
|
|
|
|
return useHistory<LocationState>();
|
|
|
|
}
|
|
|
|
|
2023-10-25 00:24:57 +11:00
|
|
|
function normalizePath(
|
|
|
|
path: HistoryPath,
|
2023-10-26 22:00:10 +11:00
|
|
|
state?: LocationState,
|
|
|
|
): LocationDescriptorObject<LocationState> {
|
2023-10-25 00:24:57 +11: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 &&
|
|
|
|
process.env.NODE_ENV === 'development'
|
|
|
|
) {
|
|
|
|
// 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' &&
|
|
|
|
!location.pathname?.startsWith('/deck')
|
|
|
|
) {
|
|
|
|
location.pathname = `/deck${location.pathname}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return location;
|
2023-10-20 04:44:55 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
browserHistory.push = (path: HistoryPath, state?: MastodonLocationState) => {
|
2023-10-25 00:24:57 +11:00
|
|
|
const location = normalizePath(path, state);
|
2023-07-14 01:18:09 +10:00
|
|
|
|
2023-10-25 00:24:57 +11:00
|
|
|
location.state = location.state ?? {};
|
|
|
|
location.state.fromMastodon = true;
|
2023-10-20 04:44:55 +11:00
|
|
|
|
2023-10-25 00:24:57 +11:00
|
|
|
originalPush(location);
|
2023-07-14 01:18:09 +10:00
|
|
|
};
|
|
|
|
|
2023-10-20 04:44:55 +11:00
|
|
|
browserHistory.replace = (path: HistoryPath, state?: MastodonLocationState) => {
|
2023-10-25 00:24:57 +11:00
|
|
|
const location = normalizePath(path, state);
|
2023-07-18 01:32:46 +10:00
|
|
|
|
2023-10-25 00:24:57 +11:00
|
|
|
if (!location.pathname) return;
|
2023-10-20 04:44:55 +11:00
|
|
|
|
2023-10-25 00:24:57 +11:00
|
|
|
if (browserHistory.location.state?.fromMastodon) {
|
|
|
|
location.state = location.state ?? {};
|
|
|
|
location.state.fromMastodon = true;
|
2023-07-18 01:32:46 +10:00
|
|
|
}
|
2023-10-25 00:24:57 +11:00
|
|
|
|
|
|
|
originalReplace(location);
|
2023-07-18 01:32:46 +10:00
|
|
|
};
|
|
|
|
|
2023-07-14 01:18:09 +10:00
|
|
|
export const Router: React.FC<PropsWithChildren> = ({ children }) => {
|
|
|
|
return <OriginalRouter history={browserHistory}>{children}</OriginalRouter>;
|
|
|
|
};
|