2023-07-13 17:18:09 +02:00
|
|
|
import type { PropsWithChildren } from 'react';
|
|
|
|
import React from 'react';
|
|
|
|
|
2023-10-26 13:00:10 +02:00
|
|
|
import { Router as OriginalRouter, useHistory } from 'react-router';
|
2023-07-13 17:18:09 +02:00
|
|
|
|
2023-10-24 15:24:57 +02:00
|
|
|
import type {
|
|
|
|
LocationDescriptor,
|
|
|
|
LocationDescriptorObject,
|
|
|
|
Path,
|
|
|
|
} from 'history';
|
2023-10-19 19:44:55 +02:00
|
|
|
import { createBrowserHistory } from 'history';
|
|
|
|
|
2023-07-13 17:18:09 +02:00
|
|
|
import { layoutFromWindow } from 'flavours/glitch/is_mobile';
|
2024-01-15 12:16:05 +01:00
|
|
|
import { isDevelopment } from 'flavours/glitch/utils/environment';
|
2023-07-13 17:18:09 +02:00
|
|
|
|
2023-07-17 17:32:46 +02:00
|
|
|
interface MastodonLocationState {
|
|
|
|
fromMastodon?: boolean;
|
|
|
|
mastodonModalKey?: string;
|
|
|
|
}
|
|
|
|
|
2023-10-26 13:00:10 +02:00
|
|
|
type LocationState = MastodonLocationState | null | undefined;
|
|
|
|
|
|
|
|
type HistoryPath = Path | LocationDescriptor<LocationState>;
|
|
|
|
|
2024-07-19 17:26:44 +02:00
|
|
|
export const browserHistory = createBrowserHistory<LocationState>();
|
2023-07-13 17:18:09 +02:00
|
|
|
const originalPush = browserHistory.push.bind(browserHistory);
|
2023-07-17 17:32:46 +02:00
|
|
|
const originalReplace = browserHistory.replace.bind(browserHistory);
|
|
|
|
|
2023-10-26 13:00:10 +02:00
|
|
|
export function useAppHistory() {
|
|
|
|
return useHistory<LocationState>();
|
|
|
|
}
|
|
|
|
|
2023-10-24 15:24:57 +02:00
|
|
|
function normalizePath(
|
|
|
|
path: HistoryPath,
|
2023-10-26 13:00:10 +02:00
|
|
|
state?: LocationState,
|
|
|
|
): LocationDescriptorObject<LocationState> {
|
2023-10-24 15:24:57 +02: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 &&
|
2024-01-15 12:16:05 +01:00
|
|
|
isDevelopment()
|
2023-10-24 15:24:57 +02: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 16:58:33 +02:00
|
|
|
location.pathname &&
|
|
|
|
!location.pathname.startsWith('/deck')
|
2023-10-24 15:24:57 +02:00
|
|
|
) {
|
|
|
|
location.pathname = `/deck${location.pathname}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return location;
|
2023-10-19 19:44:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
browserHistory.push = (path: HistoryPath, state?: MastodonLocationState) => {
|
2023-10-24 15:24:57 +02:00
|
|
|
const location = normalizePath(path, state);
|
2023-07-13 17:18:09 +02:00
|
|
|
|
2023-10-24 15:24:57 +02:00
|
|
|
location.state = location.state ?? {};
|
|
|
|
location.state.fromMastodon = true;
|
2023-10-19 19:44:55 +02:00
|
|
|
|
2023-10-24 15:24:57 +02:00
|
|
|
originalPush(location);
|
2023-07-13 17:18:09 +02:00
|
|
|
};
|
|
|
|
|
2023-10-19 19:44:55 +02:00
|
|
|
browserHistory.replace = (path: HistoryPath, state?: MastodonLocationState) => {
|
2023-10-24 15:24:57 +02:00
|
|
|
const location = normalizePath(path, state);
|
2023-07-17 17:32:46 +02:00
|
|
|
|
2023-10-24 15:24:57 +02:00
|
|
|
if (!location.pathname) return;
|
2023-10-19 19:44:55 +02:00
|
|
|
|
2023-10-24 15:24:57 +02:00
|
|
|
if (browserHistory.location.state?.fromMastodon) {
|
|
|
|
location.state = location.state ?? {};
|
|
|
|
location.state.fromMastodon = true;
|
2023-07-17 17:32:46 +02:00
|
|
|
}
|
2023-10-24 15:24:57 +02:00
|
|
|
|
|
|
|
originalReplace(location);
|
2023-07-17 17:32:46 +02:00
|
|
|
};
|
|
|
|
|
2023-07-13 17:18:09 +02:00
|
|
|
export const Router: React.FC<PropsWithChildren> = ({ children }) => {
|
|
|
|
return <OriginalRouter history={browserHistory}>{children}</OriginalRouter>;
|
|
|
|
};
|