snort/packages/app/src/Pages/Root.tsx

121 lines
3.5 KiB
TypeScript
Raw Normal View History

2023-01-05 15:23:55 +00:00
import "./Root.css";
2023-02-14 11:04:11 +00:00
import { useEffect, useState } from "react";
2022-12-18 14:51:47 +00:00
import { useSelector } from "react-redux";
import { Link } from "react-router-dom";
import { useIntl, FormattedMessage } from "react-intl";
2023-01-20 11:11:50 +00:00
2023-02-06 21:42:47 +00:00
import Tabs, { Tab } from "Element/Tabs";
2023-01-20 11:11:50 +00:00
import { RootState } from "State/Store";
import Timeline from "Element/Timeline";
2023-02-11 20:05:46 +00:00
import { System } from "@snort/nostr";
2023-01-20 11:11:50 +00:00
import { TimelineSubject } from "Feed/TimelineFeed";
2023-01-05 15:23:55 +00:00
2023-02-08 21:10:26 +00:00
import messages from "./messages";
2023-02-14 11:04:11 +00:00
import { debounce } from "Util";
2023-02-08 21:10:26 +00:00
2022-12-18 14:51:47 +00:00
export default function RootPage() {
const { formatMessage } = useIntl();
2023-02-14 11:04:11 +00:00
const { loggedOut, publicKey: pubKey, follows, tags, relays } = useSelector((s: RootState) => s.login);
const RootTab: Record<string, Tab> = {
Posts: {
text: formatMessage(messages.Posts),
value: 0,
},
PostsAndReplies: {
text: formatMessage(messages.Conversations),
value: 1,
},
Global: {
text: formatMessage(messages.Global),
value: 2,
},
};
const [tab, setTab] = useState<Tab>(RootTab.Posts);
const [relay, setRelay] = useState<string>();
2023-02-14 11:04:11 +00:00
const [globalRelays, setGlobalRelays] = useState<string[]>([]);
const tagTabs = tags.map((t, idx) => {
return { text: `#${t}`, value: idx + 3 };
});
const tabs = [RootTab.Posts, RootTab.PostsAndReplies, RootTab.Global, ...tagTabs];
2023-01-02 11:59:03 +00:00
function followHints() {
if (follows?.length === 0 && pubKey && tab !== RootTab.Global) {
return (
2023-02-08 21:10:26 +00:00
<FormattedMessage
{...messages.NoFollows}
values={{
newUsersPage: (
2023-02-12 21:55:35 +00:00
<Link to={"/new/discover"}>
2023-02-08 21:10:26 +00:00
<FormattedMessage {...messages.NewUsers} />
</Link>
),
}}
/>
);
}
}
2023-02-14 11:04:11 +00:00
useEffect(() => {
return debounce(1_000, () => {
const ret: string[] = [];
System.Sockets.forEach((v, k) => {
if (v.Info?.limitation?.payment_required === true) {
ret.push(k);
}
});
if (ret.length > 0 && !relay) {
setRelay(ret[0]);
}
2023-02-14 11:04:11 +00:00
setGlobalRelays(ret);
});
}, [relays, relay]);
const isGlobal = loggedOut || tab.value === RootTab.Global.value;
const timelineSubect: TimelineSubject = (() => {
if (isGlobal) {
return { type: "global", items: [], discriminator: "all" };
}
if (tab.value >= 3) {
const hashtag = tab.text.slice(1);
return { type: "hashtag", items: [hashtag], discriminator: hashtag };
}
return { type: "pubkey", items: follows, discriminator: "follows" };
})();
return (
<>
<div className="main-content">{pubKey && <Tabs tabs={tabs} tab={tab} setTab={setTab} />}</div>
{isGlobal && globalRelays.length > 0 && (
<div className="flex mb10">
<div className="f-grow">
<FormattedMessage
defaultMessage="Read global from"
description="Label for reading global feed from specific relays"
/>
</div>
<div>
<select onChange={e => setRelay(e.target.value)}>
{globalRelays.map(a => (
<option key={a} value={a}>
{new URL(a).host}
</option>
))}
</select>
</div>
</div>
)}
{followHints()}
<Timeline
key={tab.value}
subject={timelineSubect}
postsOnly={tab.value === RootTab.Posts.value}
method={"TIME_RANGE"}
2023-02-14 11:04:11 +00:00
window={undefined}
relay={isGlobal ? relay : undefined}
/>
</>
);
2023-01-25 18:08:53 +00:00
}