global tab fix, root tab routing

This commit is contained in:
Martti Malmi 2024-02-03 13:38:03 +02:00
parent ce2218bc93
commit 4dff677809
7 changed files with 47 additions and 30 deletions

View File

@ -2,19 +2,9 @@ import { ReactNode } from "react";
import { FormattedMessage } from "react-intl";
import Icon from "@/Components/Icons/Icon";
import { RootTabRoutePath } from "@/Pages/Root/RootTabRoutes";
import { Newest } from "@/Utils/Login";
export type RootTab =
| "for-you"
| "following"
| "followed-by-friends"
| "conversations"
| "trending-notes"
| "trending-people"
| "suggested"
| "tags"
| "global";
export function rootTabItems(base: string, pubKey: string | undefined, tags: Newest<Array<string>>) {
const menuItems = [
{
@ -30,7 +20,7 @@ export function rootTabItems(base: string, pubKey: string | undefined, tags: New
},
{
tab: "following",
path: `${base}/notes`,
path: `${base}/following`,
show: Boolean(pubKey),
element: (
<>
@ -40,7 +30,7 @@ export function rootTabItems(base: string, pubKey: string | undefined, tags: New
),
},
{
tab: "trending-notes",
tab: "trending/notes",
path: `${base}/trending/notes`,
show: true,
element: (
@ -84,7 +74,7 @@ export function rootTabItems(base: string, pubKey: string | undefined, tags: New
),
},
{
tab: "trending-hashtags",
tab: "trending/hashtags",
path: `${base}/trending/hashtags`,
show: true,
element: (
@ -117,7 +107,7 @@ export function rootTabItems(base: string, pubKey: string | undefined, tags: New
),
},
] as Array<{
tab: RootTab;
tab: RootTabRoutePath;
path: string;
show: boolean;
element: ReactNode;

View File

@ -4,9 +4,10 @@ import { Menu, MenuItem } from "@szhsin/react-menu";
import { useEffect, useMemo, useState } from "react";
import { useLocation, useNavigate } from "react-router-dom";
import { RootTab, rootTabItems } from "@/Components/Feed/RootTabItems";
import { rootTabItems } from "@/Components/Feed/RootTabItems";
import Icon from "@/Components/Icons/Icon";
import useLogin from "@/Hooks/useLogin";
import { RootTabRoutePath } from "@/Pages/Root/RootTabRoutes";
export function RootTabs({ base = "/" }: { base: string }) {
const navigate = useNavigate();
@ -23,11 +24,16 @@ export function RootTabs({ base = "/" }: { base: string }) {
const menuItems = useMemo(() => rootTabItems(base, pubKey, tags), [base, pubKey, tags]);
const defaultTab = pubKey ? preferences.defaultRootTab ?? `${base}/notes` : `${base}/trending/notes`;
let defaultTab: RootTabRoutePath;
if (pubKey) {
defaultTab = preferences.defaultRootTab ?? (CONFIG.features.forYouFeed ? "for-you" : "following");
} else {
defaultTab = `trending/notes`;
}
const initialPathname = location.pathname === "/" ? defaultTab : location.pathname;
const initialRootType = menuItems.find(a => a.path === initialPathname)?.tab || "for-you";
const initialRootType = menuItems.find(a => a.path === initialPathname)?.tab || defaultTab;
const [rootType, setRootType] = useState<RootTab>(initialRootType);
const [rootType, setRootType] = useState<RootTabRoutePath>(initialRootType);
useEffect(() => {
const currentTab = menuItems.find(a => a.path === location.pathname)?.tab;
@ -45,7 +51,7 @@ export function RootTabs({ base = "/" }: { base: string }) {
</>
);
}
return menuItems.find(a => a.tab === rootType)?.element;
return menuItems.find(a => a.tab === rootType)?.element ?? menuItems[0].element;
}
return (

View File

@ -6,7 +6,7 @@ export const DefaultTab = () => {
preferences: s.appData.item.preferences,
publicKey: s.publicKey,
}));
const tab = publicKey ? preferences.defaultRootTab ?? `for-you` : `trending/notes`;
const tab = publicKey ? preferences.defaultRootTab : `trending/notes`;
const elm = RootTabRoutes.find(a => a.path === tab)?.element;
return elm;
return elm ?? RootTabRoutes.find(a => a.path === (CONFIG.features.forYouFeed ? "for-you" : "following"))?.element;
};

View File

@ -83,10 +83,10 @@ export const GlobalTab = () => {
() => ({
type: "global",
items: [],
relay: [relay.url],
discriminator: `all-${sha256(relay.url)}`,
relay: [relay?.url],
discriminator: `all-${sha256(relay?.url ?? "")}`,
}),
[relay.url],
[relay?.url],
);
return (

View File

@ -12,7 +12,27 @@ import { NotesTab } from "@/Pages/Root/NotesTab";
import { TagsTab } from "@/Pages/Root/TagsTab";
import { TopicsPage } from "@/Pages/TopicsPage";
export const RootTabRoutes = [
export type RootTabRoutePath =
| ""
| "for-you"
| "global"
| "following"
| "followed-by-friends"
| "conversations"
| "discover"
| "tag/:tag"
| "trending/notes"
| "trending/hashtags"
| "suggested"
| "t/:tag"
| "topics";
export type RootTabRoute = {
path: RootTabRoutePath;
element: JSX.Element;
};
export const RootTabRoutes: RootTabRoute[] = [
{
path: "",
element: <DefaultTab />,
@ -26,7 +46,7 @@ export const RootTabRoutes = [
element: <GlobalTab />,
},
{
path: "notes",
path: "following",
element: <NotesTab />,
},
{

View File

@ -89,7 +89,7 @@ const PreferencesPage = () => {
<FormattedMessage defaultMessage="For you" id="xEjBS7" />
</option>
)}
<option value="notes">
<option value="following">
<FormattedMessage defaultMessage="Notes" id="7+Domh" />
</option>
<option value="conversations">

View File

@ -1,4 +1,5 @@
import { ImgProxySettings } from "@/Hooks/useImgProxy";
import { RootTabRoutePath } from "@/Pages/Root/RootTabRoutes";
import { DefaultImgProxy } from "@/Utils/Const";
export interface UserPreferences {
@ -55,7 +56,7 @@ export interface UserPreferences {
/**
* Default page to select on load
*/
defaultRootTab: "for-you" | "notes" | "conversations" | "global";
defaultRootTab: RootTabRoutePath;
/**
* Default zap amount
@ -113,7 +114,7 @@ export const DefaultPreferences = {
autoShowLatest: false,
fileUploader: "void.cat",
imgProxyConfig: DefaultImgProxy,
defaultRootTab: CONFIG.features.forYouFeed ? "for-you" : "notes",
defaultRootTab: CONFIG.features.forYouFeed ? "for-you" : "following",
defaultZapAmount: 50,
autoZap: false,
telemetry: true,