From 9ba935a21ae90879ce903507e8bbba5c014c57f5 Mon Sep 17 00:00:00 2001 From: Anton Tranelis Date: Tue, 30 Jul 2024 15:00:19 +0200 Subject: [PATCH] fix slow tab switch --- src/Components/Profile/ProfileForm.tsx | 9 +- src/Components/Profile/ProfileView.tsx | 5 +- src/Components/Profile/Templates/TabsForm.tsx | 26 ++-- src/Components/Profile/Templates/TabsView.tsx | 136 ++++++++++-------- 4 files changed, 96 insertions(+), 80 deletions(-) diff --git a/src/Components/Profile/ProfileForm.tsx b/src/Components/Profile/ProfileForm.tsx index a01a2177..29eb452a 100644 --- a/src/Components/Profile/ProfileForm.tsx +++ b/src/Components/Profile/ProfileForm.tsx @@ -51,6 +51,9 @@ export function ProfileForm({ userType }: { userType: string }) { const getItemTags = useGetItemTags(); const items = useItems(); + const [urlParams, setUrlParams] = useState(new URLSearchParams(location.search)); + + useEffect(() => { item && hasUserPermission("items", "update", item) && setUpdatePermission(true); }, [item]) @@ -119,8 +122,6 @@ export function ProfileForm({ userType }: { userType: string }) { setTemplate(item.layer?.itemType.template || userType); }, [userType, item]) - let params = new URLSearchParams(window.location.search); - return ( <> @@ -138,11 +139,11 @@ export function ProfileForm({ userType }: { userType: string }) { } {template == "tabs" && - linkItem(id, item, updateItem)} unlinkItem={(id) => unlinkItem(id, item, updateItem)}> + linkItem(id, item, updateItem)} unlinkItem={(id) => unlinkItem(id, item, updateItem)} setUrlParams={setUrlParams}> }
- +
diff --git a/src/Components/Profile/ProfileView.tsx b/src/Components/Profile/ProfileView.tsx index 37b2756f..dff33da6 100644 --- a/src/Components/Profile/ProfileView.tsx +++ b/src/Components/Profile/ProfileView.tsx @@ -119,6 +119,9 @@ export function ProfileView({ userType }: { userType: string }) { setTemplate(item.layer?.itemType.template || userType); }, [userType, item]) + const [urlParams, setUrlParams] = useState(new URLSearchParams(location.search)); + + return ( <> {item && @@ -137,7 +140,7 @@ export function ProfileView({ userType }: { userType: string }) { } {template == "tabs" && - linkItem(id, item, updateItem)} unlinkItem={(id) => unlinkItem(id, item, updateItem)}/> + linkItem(id, item, updateItem)} unlinkItem={(id) => unlinkItem(id, item, updateItem)}/> } diff --git a/src/Components/Profile/Templates/TabsForm.tsx b/src/Components/Profile/Templates/TabsForm.tsx index 12d581f4..9bb6ee06 100644 --- a/src/Components/Profile/Templates/TabsForm.tsx +++ b/src/Components/Profile/Templates/TabsForm.tsx @@ -1,4 +1,4 @@ -import { useEffect, useState } from "react" +import { useCallback, useEffect, useState } from "react" import { TextAreaInput, TextInput } from "../../Input" import { PopupStartEndInput, TextView } from "../../Map" import { ActionButton } from "../Subcomponents/ActionsButton" @@ -7,27 +7,27 @@ import { TagsWidget } from "../Subcomponents/TagsWidget" import { useNavigate } from "react-router-dom" import { useUpdateItem } from "../../Map/hooks/useItems" -export const TabsForm = ({ item, state, setState, updatePermission, linkItem, unlinkItem, loading }) => { +export const TabsForm = ({ item, state, setState, updatePermission, linkItem, unlinkItem, loading, setUrlParams }) => { const [activeTab, setActiveTab] = useState(1); const navigate = useNavigate(); const updateItem = useUpdateItem(); - const updateActiveTab = (id: number) => { + const updateActiveTab = useCallback((id: number) => { setActiveTab(id); - + let params = new URLSearchParams(window.location.search); - let urlTab = params.get("tab"); - if (!urlTab?.includes(id.toString())) - params.set("tab", `${id ? id : ""}`) - navigate(location.pathname + "?" + params.toString()); - } - - useEffect(() => { + params.set("tab", `${id}`); + const newUrl = location.pathname + "?" + params.toString(); + window.history.pushState({}, '', newUrl); + setUrlParams(params); + }, [location.pathname]); + + useEffect(() => { let params = new URLSearchParams(location.search); let urlTab = params.get("tab"); - urlTab ? setActiveTab(Number(urlTab)) : setActiveTab(1); - }, [location]) + setActiveTab(urlTab ? Number(urlTab) : 1); + }, [location.search]); return (
diff --git a/src/Components/Profile/Templates/TabsView.tsx b/src/Components/Profile/Templates/TabsView.tsx index d941dc88..15016607 100644 --- a/src/Components/Profile/Templates/TabsView.tsx +++ b/src/Components/Profile/Templates/TabsView.tsx @@ -1,101 +1,112 @@ -import { StartEndView, TextView } from '../../Map'; -import { TagView } from '../../Templates/TagView'; -import { LinkedItemsHeaderView } from '../Subcomponents/LinkedItemsHeaderView'; -import { ActionButton } from '../Subcomponents/ActionsButton'; -import { useEffect, useRef, useState, useCallback } from 'react'; -import { useAddFilterTag } from '../../Map/hooks/useFilter'; -import { Item, Tag } from 'utopia-ui/dist/types'; -import { useNavigate, useLocation } from 'react-router-dom'; -import { memo } from 'react'; +import { StartEndView, TextView } from '../../Map' +import { TagView } from '../../Templates/TagView' +import { LinkedItemsHeaderView } from '../Subcomponents/LinkedItemsHeaderView' +import { ActionButton } from '../Subcomponents/ActionsButton' +import { useCallback, useEffect, useRef, useState } from 'react' +import { useAddFilterTag } from '../../Map/hooks/useFilter' +import { Item, Tag } from 'utopia-ui/dist/types' +import { useNavigate } from 'react-router-dom' -export const TabsView = ({ item, offers, needs, relations, updatePermission, loading, linkItem, unlinkItem }: { item: Item, offers: Array, needs: Array, relations: Array, updatePermission: boolean, loading: boolean, linkItem: (id: string) => Promise, unlinkItem: (id: string) => Promise }) => { +export const TabsView = ({ item, offers, needs, relations, updatePermission, loading, linkItem, unlinkItem, setUrlParams }: { item: Item, offers: Array, needs: Array, relations: Array, updatePermission: boolean, loading: boolean, linkItem: (id: string) => Promise, unlinkItem: (id: string) => Promise , setUrlParams: any}) => { const addFilterTag = useAddFilterTag(); - const [activeTab, setActiveTab] = useState(1); + const [activeTab, setActiveTab] = useState(); const navigate = useNavigate(); - const location = useLocation(); - const tabRef = useRef(null); const [addItemPopupType, setAddItemPopupType] = useState(""); useEffect(() => { scroll(); - }, [addItemPopupType]); + }, [addItemPopupType]) - const scroll = useCallback(() => { + function scroll() { tabRef.current?.scrollIntoView(); - }, []); + } + + const tabRef = useRef(null); const updateActiveTab = useCallback((id: number) => { setActiveTab(id); let params = new URLSearchParams(window.location.search); - let urlTab = params.get("tab"); - if (!urlTab?.includes(id.toString())) { - params.set("tab", `${id ? id : ""}`); - navigate(location.pathname + "?" + params.toString(), { replace: true }); - } - }, [navigate, location.pathname]); + params.set("tab", `${id}`); + const newUrl = location.pathname + "?" + params.toString(); + window.history.pushState({}, '', newUrl); + setUrlParams(params); + }, [location.pathname]); + useEffect(() => { let params = new URLSearchParams(location.search); let urlTab = params.get("tab"); - urlTab ? setActiveTab(Number(urlTab)) : setActiveTab(1); - }, [location]); + setActiveTab(urlTab ? Number(urlTab) : 1); + }, [location.search]); return (
updateActiveTab(1)} /> - {activeTab === 1 && -
- {item.layer?.itemType.show_start_end && -
- } - -
- -
- } +
+ {item.layer?.itemType.show_start_end && +
+ } + +
+ +
- {item.layer?.itemType.offers_and_needs && <> - updateActiveTab(3)} /> - {activeTab === 3 && -
+ {item.layer?.itemType.offers_and_needs && + + <> + + updateActiveTab(3)} /> +
- {offers.length > 0 && -
-

Offers

-
- {offers.map(o => addFilterTag(o)} />)} -
-
+ { + offers.length > 0 ? +
+

Offers

+ < div className='tw-flex tw-flex-wrap tw-mb-4'> + { + offers.map(o => { + addFilterTag(o) + }} />) + } +
+
: "" } - {needs.length > 0 && -
-

Needs

-
- {needs.map(n => addFilterTag(n)} />)} -
-
+ { + needs.length > 0 ? +
+

Needs

+ < div className='tw-flex tw-flex-wrap tw-mb-4'> + { + needs.map(n => addFilterTag(n)} />) + } +
+
: "" }
- } - } + - {item.layer?.itemType.relations && <> - updateActiveTab(7)} /> - {activeTab === 7 && + + } + + {item.layer?.itemType.relations && + <> + updateActiveTab(7)} />
{relations && relations.map(i => + +
navigate('/item/' + i.id)}>
@@ -104,11 +115,12 @@ export const TabsView = ({ item, offers, needs, relations, updatePermission, loa
)} {updatePermission && } +
- } - } + + }
- ); + ) }