refactor(CompactSearchBox): 提取常量和工具函数到子模块
- constants.js: SORT_OPTIONS, IMPORTANCE_OPTIONS 选项配置 - utils.js: findIndustryPath, inferTimeRangeFromFilters, buildFilterParams - index.js: 模块统一导出 主文件逻辑更清晰,工具函数可复用 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,18 @@
|
||||
// CompactSearchBox 常量定义
|
||||
|
||||
// 排序选项常量
|
||||
export const SORT_OPTIONS = [
|
||||
{ value: "new", label: "最新排序", mobileLabel: "最新" },
|
||||
{ value: "hot", label: "最热排序", mobileLabel: "热门" },
|
||||
{ value: "importance", label: "重要性排序", mobileLabel: "重要" },
|
||||
{ value: "returns_avg", label: "平均收益", mobileLabel: "均收" },
|
||||
{ value: "returns_week", label: "周收益", mobileLabel: "周收" },
|
||||
];
|
||||
|
||||
// 重要性等级常量
|
||||
export const IMPORTANCE_OPTIONS = [
|
||||
{ value: "S", label: "S级" },
|
||||
{ value: "A", label: "A级" },
|
||||
{ value: "B", label: "B级" },
|
||||
{ value: "C", label: "C级" },
|
||||
];
|
||||
@@ -0,0 +1,4 @@
|
||||
// CompactSearchBox 模块导出
|
||||
|
||||
export { SORT_OPTIONS, IMPORTANCE_OPTIONS } from "./constants";
|
||||
export { findIndustryPath, inferTimeRangeFromFilters, buildFilterParams } from "./utils";
|
||||
@@ -0,0 +1,140 @@
|
||||
// CompactSearchBox 工具函数
|
||||
|
||||
import dayjs from "dayjs";
|
||||
|
||||
/**
|
||||
* 在行业树中查找指定代码的完整路径
|
||||
* @param {string} targetCode - 目标行业代码
|
||||
* @param {Array} data - 行业数据树
|
||||
* @param {Array} currentPath - 当前路径(递归用)
|
||||
* @returns {Array|null} - 找到的完整路径,未找到返回 null
|
||||
*/
|
||||
export const findIndustryPath = (targetCode, data, currentPath = []) => {
|
||||
if (!data || data.length === 0) return null;
|
||||
|
||||
for (const item of data) {
|
||||
const newPath = [...currentPath, item.value];
|
||||
|
||||
if (item.value === targetCode) {
|
||||
return newPath;
|
||||
}
|
||||
|
||||
if (item.children && item.children.length > 0) {
|
||||
const found = findIndustryPath(targetCode, item.children, newPath);
|
||||
if (found) return found;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
/**
|
||||
* 从 filters 中推断时间范围配置
|
||||
* @param {Object} filters - 筛选条件
|
||||
* @returns {Object|null} - 时间范围配置
|
||||
*/
|
||||
export const inferTimeRangeFromFilters = (filters) => {
|
||||
if (!filters) return null;
|
||||
|
||||
const hasTimeInFilters =
|
||||
filters.start_date ||
|
||||
filters.end_date ||
|
||||
filters.recent_days ||
|
||||
filters.time_filter_key;
|
||||
|
||||
if (!hasTimeInFilters) return null;
|
||||
|
||||
let inferredKey = filters.time_filter_key || "custom";
|
||||
let inferredLabel = "";
|
||||
|
||||
if (filters.time_filter_key === "current-trading-day") {
|
||||
inferredKey = "current-trading-day";
|
||||
inferredLabel = "当前交易日";
|
||||
} else if (filters.time_filter_key === "all") {
|
||||
inferredKey = "all";
|
||||
inferredLabel = "全部";
|
||||
} else if (filters.recent_days) {
|
||||
if (filters.recent_days === "7") {
|
||||
inferredKey = "week";
|
||||
inferredLabel = "近一周";
|
||||
} else if (filters.recent_days === "30") {
|
||||
inferredKey = "month";
|
||||
inferredLabel = "近一月";
|
||||
} else {
|
||||
inferredLabel = `近${filters.recent_days}天`;
|
||||
}
|
||||
} else if (filters.start_date && filters.end_date) {
|
||||
inferredLabel = `${dayjs(filters.start_date).format("MM-DD HH:mm")} - ${dayjs(filters.end_date).format("MM-DD HH:mm")}`;
|
||||
}
|
||||
|
||||
return {
|
||||
start_date: filters.start_date || "",
|
||||
end_date: filters.end_date || "",
|
||||
recent_days: filters.recent_days || "",
|
||||
label: inferredLabel,
|
||||
key: inferredKey,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* 构建筛选参数
|
||||
* @param {Object} options - 配置选项
|
||||
* @returns {Object} - 构建的参数对象
|
||||
*/
|
||||
export const buildFilterParams = ({
|
||||
overrides = {},
|
||||
sort,
|
||||
importance,
|
||||
filtersQ,
|
||||
industryValue,
|
||||
tradingTimeRange,
|
||||
mode,
|
||||
pageSize,
|
||||
}) => {
|
||||
const sortValue = overrides.sort ?? sort;
|
||||
let actualSort = sortValue;
|
||||
let returnType;
|
||||
|
||||
if (sortValue === "returns_avg") {
|
||||
actualSort = "returns";
|
||||
returnType = "avg";
|
||||
} else if (sortValue === "returns_week") {
|
||||
actualSort = "returns";
|
||||
returnType = "week";
|
||||
}
|
||||
|
||||
let importanceValue = overrides.importance ?? importance;
|
||||
if (Array.isArray(importanceValue)) {
|
||||
importanceValue =
|
||||
importanceValue.length === 0 ? "all" : importanceValue.join(",");
|
||||
}
|
||||
|
||||
const result = {
|
||||
...overrides,
|
||||
sort: actualSort,
|
||||
_sortDisplay: sortValue,
|
||||
importance: importanceValue,
|
||||
q: overrides.q ?? filtersQ ?? "",
|
||||
industry_code: overrides.industry_code ?? (industryValue?.join(",") || ""),
|
||||
start_date: overrides.start_date ?? (tradingTimeRange?.start_date || ""),
|
||||
end_date: overrides.end_date ?? (tradingTimeRange?.end_date || ""),
|
||||
recent_days: overrides.recent_days ?? (tradingTimeRange?.recent_days || ""),
|
||||
page: 1,
|
||||
};
|
||||
|
||||
delete result.per_page;
|
||||
|
||||
if (returnType) {
|
||||
result.return_type = returnType;
|
||||
} else {
|
||||
delete result.return_type;
|
||||
}
|
||||
|
||||
if (mode !== undefined && mode !== null) {
|
||||
result.mode = mode;
|
||||
}
|
||||
if (pageSize !== undefined && pageSize !== null) {
|
||||
result.per_page = pageSize;
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
Reference in New Issue
Block a user