|
|
@@ -1,13 +1,12 @@
|
|
|
-import {useContext, useContextSection} from '@hooks';
|
|
|
-import {context} from '../context';
|
|
|
import {Modal, TabPaneProps, TabsProps} from 'antd';
|
|
|
-import {ReactNode} from 'react';
|
|
|
+import {ReactNode, useMemo} from 'react';
|
|
|
import css from './index.module.css';
|
|
|
import {useStore} from 'zustand';
|
|
|
import {tabStore} from '@stores';
|
|
|
import {useContextMenu, ItemParams} from 'react-contexify';
|
|
|
import {DragEndEvent} from '@dnd-kit/core';
|
|
|
import {arrayMove} from '@dnd-kit/sortable';
|
|
|
+import {shallowEqual} from '@utils';
|
|
|
|
|
|
export type Tab = {
|
|
|
key: string;
|
|
|
@@ -17,25 +16,34 @@ export type Tab = {
|
|
|
export function useTabItems() {
|
|
|
const {host} = location;
|
|
|
|
|
|
- const tabs = useContextSection(context, function ([tabs]) {
|
|
|
- return tabs.map<Tab>(function (tab) {
|
|
|
- return {
|
|
|
- ...tab,
|
|
|
- originalTab: tab,
|
|
|
- forceRender: true,
|
|
|
- closable: tab.key !== '-1',
|
|
|
- animated: true,
|
|
|
- children: (
|
|
|
- <iframe
|
|
|
- src={`http://${host}${tab.url}`}
|
|
|
- className={css.iframe}
|
|
|
- data-url={tab.url}
|
|
|
- />
|
|
|
- ),
|
|
|
- };
|
|
|
- });
|
|
|
+ const {originalTab, dispatch} = useStore(tabStore, function (state) {
|
|
|
+ return {
|
|
|
+ dispatch: state.dispatch,
|
|
|
+ originalTab: state.tabList,
|
|
|
+ };
|
|
|
});
|
|
|
- const [originalTab, dispatch] = useContext(context);
|
|
|
+
|
|
|
+ const tabs = useMemo(
|
|
|
+ function () {
|
|
|
+ return originalTab.map<Tab>(function (tab) {
|
|
|
+ return {
|
|
|
+ ...tab,
|
|
|
+ originalTab: tab,
|
|
|
+ forceRender: true,
|
|
|
+ closable: tab.key !== '-1',
|
|
|
+ animated: true,
|
|
|
+ children: (
|
|
|
+ <iframe
|
|
|
+ src={`http://${host}${tab.url}`}
|
|
|
+ className={css.iframe}
|
|
|
+ data-url={tab.url}
|
|
|
+ />
|
|
|
+ ),
|
|
|
+ };
|
|
|
+ });
|
|
|
+ },
|
|
|
+ [host, originalTab],
|
|
|
+ );
|
|
|
|
|
|
function onDragEnd({active, over}: DragEndEvent) {
|
|
|
if (!over) return;
|
|
|
@@ -55,8 +63,14 @@ export function useTabItems() {
|
|
|
}
|
|
|
|
|
|
export function useTabActive() {
|
|
|
- const dispatch = useContextSection(context, state => state[1]);
|
|
|
- const {key: activeKey, dispatch: setActiveKey} = useStore(tabStore);
|
|
|
+ const dispatch = useStore(tabStore, state => state.dispatch);
|
|
|
+ const {activeKey, setActiveKey} = useStore(
|
|
|
+ tabStore,
|
|
|
+ function (state) {
|
|
|
+ return {activeKey: state.activeKey, setActiveKey: state.setActiveKey};
|
|
|
+ },
|
|
|
+ shallowEqual,
|
|
|
+ );
|
|
|
|
|
|
function onClear() {
|
|
|
Modal.confirm({
|
|
|
@@ -79,7 +93,7 @@ export function useTabActive() {
|
|
|
|
|
|
export function useMenu() {
|
|
|
const {show} = useContextMenu({id: 'content_menu'});
|
|
|
- const dispatch = useContextSection(context, state => state[1]);
|
|
|
+ const dispatch = useStore(tabStore, state => state.dispatch);
|
|
|
|
|
|
function onMenuitemClick(e: ItemParams<{key: string}, any>) {
|
|
|
const {props, id} = e;
|