import * as React from 'react';
import { Redirect } from 'react-router';
import { userURL } from '..';
/**
 * Redirects from /user/$PATH to /user/$USERNAME/$PATH, where $USERNAME is the currently
 * authenticated user's username.
 */
export const RedirectToUserPage = ({ user, location }) => {
    // If not logged in, redirect to sign in
    if (!user) {
        const newURL = new URL(window.location.href);
        newURL.pathname = '/sign-in';
        // Return to the current page after sign up/in.
        newURL.searchParams.set('returnTo', window.location.href);
        return React.createElement(Redirect, { to: { pathname: newURL.pathname, search: newURL.search } });
    }
    const path = location.pathname.replace(/^\/user\//, ''); // trim leading '/user/'
    return React.createElement(Redirect, { to: { pathname: `${userURL(user.username)}/${path}`, search: location.search } });
};
//# sourceMappingURL=RedirectToUserPage.js.map