mirror of
https://github.com/cheeaun/phanpy.git
synced 2025-02-24 08:48:47 +01:00
Handle urls from root
This commit is contained in:
parent
9fd2b05065
commit
b63269e42a
3 changed files with 51 additions and 15 deletions
30
src/app.jsx
30
src/app.jsx
|
@ -19,6 +19,7 @@ import { useSnapshot } from 'valtio';
|
||||||
import AccountSheet from './components/account-sheet';
|
import AccountSheet from './components/account-sheet';
|
||||||
import Compose from './components/compose';
|
import Compose from './components/compose';
|
||||||
import Drafts from './components/drafts';
|
import Drafts from './components/drafts';
|
||||||
|
import Link from './components/link';
|
||||||
import Loader from './components/loader';
|
import Loader from './components/loader';
|
||||||
import MediaModal from './components/media-modal';
|
import MediaModal from './components/media-modal';
|
||||||
import Modal from './components/modal';
|
import Modal from './components/modal';
|
||||||
|
@ -52,6 +53,7 @@ import {
|
||||||
initPreferences,
|
initPreferences,
|
||||||
} from './utils/api';
|
} from './utils/api';
|
||||||
import { getAccessToken } from './utils/auth';
|
import { getAccessToken } from './utils/auth';
|
||||||
|
import getInstanceStatusURL from './utils/get-instance-status-url';
|
||||||
import showToast from './utils/show-toast';
|
import showToast from './utils/show-toast';
|
||||||
import states, { getStatus, saveStatus } from './utils/states';
|
import states, { getStatus, saveStatus } from './utils/states';
|
||||||
import store from './utils/store';
|
import store from './utils/store';
|
||||||
|
@ -189,6 +191,10 @@ function App() {
|
||||||
location,
|
location,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (/\/https?:/.test(location.pathname)) {
|
||||||
|
return <HttpRoute />;
|
||||||
|
}
|
||||||
|
|
||||||
const nonRootLocation = useMemo(() => {
|
const nonRootLocation = useMemo(() => {
|
||||||
const { pathname } = location;
|
const { pathname } = location;
|
||||||
return !/^\/(login|welcome)/.test(pathname);
|
return !/^\/(login|welcome)/.test(pathname);
|
||||||
|
@ -485,4 +491,28 @@ function BackgroundService({ isLoggedIn }) {
|
||||||
return null;
|
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 };
|
export { App };
|
||||||
|
|
|
@ -38,6 +38,8 @@ import { getCurrentAccount } from '../utils/store-utils';
|
||||||
import useScroll from '../utils/useScroll';
|
import useScroll from '../utils/useScroll';
|
||||||
import useTitle from '../utils/useTitle';
|
import useTitle from '../utils/useTitle';
|
||||||
|
|
||||||
|
import getInstanceStatusURL from './../utils/get-instance-status-url';
|
||||||
|
|
||||||
const LIMIT = 40;
|
const LIMIT = 40;
|
||||||
const THREAD_LIMIT = 20;
|
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;
|
export default StatusPage;
|
||||||
|
|
19
src/utils/get-instance-status-url.js
Normal file
19
src/utils/get-instance-status-url.js
Normal 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;
|
Loading…
Reference in a new issue