nicecode-v2/packages/hooks/lib/useJump/index.js
2024-01-10 15:15:12 +08:00

91 lines
3.5 KiB
JavaScript

var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/useJump/index.ts
var useJump_exports = {};
__export(useJump_exports, {
default: () => useJump_default,
useListenJump: () => useListenJump
});
module.exports = __toCommonJS(useJump_exports);
var import_ahooks = require("ahooks");
var import_events = __toESM(require("events"));
var import_func = require("@zhst/func");
var import_react = require("react");
var import_react_router_dom = require("react-router-dom");
var import_constants = require("./constants");
var emitter = new import_events.default.EventEmitter();
var useJump = () => {
const location = (0, import_react_router_dom.useLocation)();
const navigate = (0, import_react_router_dom.useNavigate)();
const jump = (path, data) => {
if (location.pathname !== path) {
navigate(path, {
state: { data, topic: import_constants.JumpKey, __from__: location.pathname }
});
} else {
emitter.emit(import_constants.JumpKey, data);
}
};
return jump;
};
var useListenJump = (handle, type) => {
const latestHandle = (0, import_ahooks.useLatest)(handle);
const navigate = (0, import_react_router_dom.useNavigate)();
const _type = (0, import_func.isArray)(type) ? type : [type];
const location = (0, import_react_router_dom.useLocation)();
(0, import_react.useEffect)(() => {
const { state, pathname, search } = location;
if (state) {
const { topic, data, __from__ } = state;
if (topic === import_constants.JumpKey && _type.includes((0, import_func.get)(data, "__key__"))) {
latestHandle.current({ ...data, __from__ });
}
navigate(`/${pathname}${search}`, {
state: null
});
}
}, [location]);
(0, import_react.useEffect)(() => {
const handleEmit = (data) => {
if (_type.includes((0, import_func.get)(data, "__key__"))) {
latestHandle.current(data);
}
};
emitter.on(import_constants.JumpKey, handleEmit);
return () => {
emitter.removeListener(import_constants.JumpKey, handleEmit);
};
}, []);
};
var useJump_default = useJump;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
useListenJump
});