import { useState, useEffect } from 'react';
import { Link, useLocation, useParams, Navigate } from 'react-router-dom';
import qs from 'qs';
import moment from 'moment';
import Message from './Message';
import Spinner from './Spinner';
import UserInfo from './UserInfo';
import { getMessages } from '../api';
import { useVisitor } from './VisitorContext';
/**
* @typedef {object} Query
* @property {string} baseUrl
* @property {object=} search
* @property {string} pageParam
*/
/**
* @typedef {object} PageProps
* @property {string=} search
* @property {import('../api').Message[]=} msgs
*/
function RequireAuth({ children }) {
let location = useLocation();
let [ visitor ] = useVisitor();
if (!visitor.hash) {
// Redirect them to the /login page, but save the current location they were
// trying to go to when they were redirected. This allows us to send them
// along to that page after they login, which is a nicer user experience
// than dropping them off on the home page.
return ;
}
return children;
}
/**
* @param {PageProps} props
*/
export function Discover() {
const location = useLocation();
let search = qs.parse(location.search.substring(1));
const query = {
baseUrl: '/api/messages',
search: search,
pageParam: search.search ? 'page' : 'before_mid'
};
return ();
}
/**
* @param {PageProps} props
*/
export function Discussions() {
const query = {
baseUrl: '/api/messages/discussions',
pageParam: 'to'
};
return ();
}
/**
* @param {PageProps} props
*/
export function Blog() {
const { user } = useParams();
const location = useLocation();
const search = {
...qs.parse(location.search.substring(1)),
uname: user
};
const query = {
baseUrl: '/api/messages',
search: search,
pageParam: search.search ? 'page' : 'before_mid'
};
return (
<>