Handle urls from root

This commit is contained in:
Lim Chee Aun 2023-04-17 18:56:09 +08:00
parent 9fd2b05065
commit b63269e42a
3 changed files with 51 additions and 15 deletions

View file

@ -19,6 +19,7 @@ import { useSnapshot } from 'valtio';
import AccountSheet from './components/account-sheet';
import Compose from './components/compose';
import Drafts from './components/drafts';
import Link from './components/link';
import Loader from './components/loader';
import MediaModal from './components/media-modal';
import Modal from './components/modal';
@ -52,6 +53,7 @@ import {
initPreferences,
} from './utils/api';
import { getAccessToken } from './utils/auth';
import getInstanceStatusURL from './utils/get-instance-status-url';
import showToast from './utils/show-toast';
import states, { getStatus, saveStatus } from './utils/states';
import store from './utils/store';
@ -189,6 +191,10 @@ function App() {
location,
});
if (/\/https?:/.test(location.pathname)) {
return <HttpRoute />;
}
const nonRootLocation = useMemo(() => {
const { pathname } = location;
return !/^\/(login|welcome)/.test(pathname);
@ -485,4 +491,28 @@ function BackgroundService({ isLoggedIn }) {
return null;
}
function HttpRoute() {
const location = useLocation();
const url = location.pathname.replace(/^\//, '');
const statusURL = getInstanceStatusURL(url);
if (statusURL) {
window.location.hash = statusURL + '?view=full';
return null;
}
return (
<div class="ui-state" tabIndex="-1">
<h2>Unable to process URL</h2>
<p>
<a href={url} target="_blank">
{url}
</a>
</p>
<hr />
<p>
<Link to="/">Go home</Link>
</p>
</div>
);
}
export { App };

View file

@ -38,6 +38,8 @@ import { getCurrentAccount } from '../utils/store-utils';
import useScroll from '../utils/useScroll';
import useTitle from '../utils/useTitle';
import getInstanceStatusURL from './../utils/get-instance-status-url';
const LIMIT = 40;
const THREAD_LIMIT = 20;
@ -1091,19 +1093,4 @@ function SubComments({
);
}
const statusRegex = /\/@([^@\/]+)@?([^\/]+)?\/([^\/]+)\/?$/i;
const statusNoteRegex = /\/notes\/([^\/]+)\/?$/i;
function getInstanceStatusURL(url) {
// Regex /:username/:id, where username = @username or @username@domain, id = anything
const { hostname, pathname } = new URL(url);
const [, username, domain, id] = pathname.match(statusRegex) || [];
if (id) {
return `/${hostname}/s/${id}`;
}
const [, noteId] = pathname.match(statusNoteRegex) || [];
if (noteId) {
return `/${hostname}/s/${noteId}`;
}
}
export default StatusPage;

View file

@ -0,0 +1,19 @@
export const statusRegex = /\/@([^@\/]+)@?([^\/]+)?\/([^\/]+)\/?$/i;
export const statusNoteRegex = /\/notes\/([^\/]+)\/?$/i;
function getInstanceStatusURL(url) {
// Regex /:username/:id, where username = @username or @username@domain, id = anything
const { hostname, pathname } = new URL(url);
const [, username, domain, id] = pathname.match(statusRegex) || [];
if (id) {
return `/${hostname}/s/${id}`;
}
const [, noteId] = pathname.match(statusNoteRegex) || [];
if (noteId) {
return `/${hostname}/s/${noteId}`;
}
}
export default getInstanceStatusURL;