diff --git a/src/Components/Profile/Templates/TabsView.tsx b/src/Components/Profile/Templates/TabsView.tsx index 3a7d7666..d941dc88 100644 --- a/src/Components/Profile/Templates/TabsView.tsx +++ b/src/Components/Profile/Templates/TabsView.tsx @@ -1,111 +1,101 @@ -import { StartEndView, TextView } from '../../Map' -import { TagView } from '../../Templates/TagView' -import { LinkedItemsHeaderView } from '../Subcomponents/LinkedItemsHeaderView' -import { ActionButton } from '../Subcomponents/ActionsButton' -import { 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' +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'; 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 }) => { const addFilterTag = useAddFilterTag(); const [activeTab, setActiveTab] = useState(1); const navigate = useNavigate(); + const location = useLocation(); + const tabRef = useRef(null); const [addItemPopupType, setAddItemPopupType] = useState(""); useEffect(() => { scroll(); - }, [addItemPopupType]) + }, [addItemPopupType]); - function scroll() { + const scroll = useCallback(() => { tabRef.current?.scrollIntoView(); - } + }, []); - const tabRef = useRef(null); - - 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()); - } + if (!urlTab?.includes(id.toString())) { + params.set("tab", `${id ? id : ""}`); + navigate(location.pathname + "?" + params.toString(), { replace: true }); + } + }, [navigate, location.pathname]); useEffect(() => { let params = new URLSearchParams(location.search); let urlTab = params.get("tab"); urlTab ? setActiveTab(Number(urlTab)) : setActiveTab(1); - }, [location]) + }, [location]); return (
updateActiveTab(1)} /> -
- {item.layer?.itemType.show_start_end && -
- } - -
- -
+ {activeTab === 1 && +
+ {item.layer?.itemType.show_start_end && +
+ } + +
+ +
+ } - {item.layer?.itemType.offers_and_needs && - - <> - - updateActiveTab(3)} /> -
+ {item.layer?.itemType.offers_and_needs && <> + updateActiveTab(3)} /> + {activeTab === 3 && +
- { - offers.length > 0 ? -
-

Offers

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

Offers

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

Needs

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

Needs

+
+ {needs.map(n => addFilterTag(n)} />)} +
+
}
- + } + } - - } - - {item.layer?.itemType.relations && - <> - updateActiveTab(7)} /> + {item.layer?.itemType.relations && <> + updateActiveTab(7)} /> + {activeTab === 7 &&
{relations && relations.map(i => - -
navigate('/item/' + i.id)}>
@@ -114,12 +104,11 @@ export const TabsView = ({ item, offers, needs, relations, updatePermission, loa
)} {updatePermission && } -
- - } + } + }
- ) + ); }