43 lines
1.9 KiB
TypeScript
43 lines
1.9 KiB
TypeScript
import type { PageContext, UIAction } from "./types";
|
|
|
|
export type WorkspacePage = PageContext["page"];
|
|
const contextLabels: Record<WorkspacePage, (context: PageContext) => string> = {
|
|
alphas: (context) =>
|
|
`上下文:${context.alpha_id ? `Alpha ${context.alpha_id}` : "Alpha 列表"}${context.selected_ids?.length ? ` · 已选 ${context.selected_ids.length} 条` : ""}`,
|
|
account: () => "上下文:个人信息页",
|
|
backtests: () => "上下文:回测研究",
|
|
datasets: (context) =>
|
|
`上下文:${context.dataset_id ?? "数据目录"}${context.catalog_scope ? ` · ${context.catalog_scope.region}/${context.catalog_scope.universe}/D${context.catalog_scope.delay}` : ""}${context.template_input_id ? " · 固定研究输入" : context.unsaved_field_selection ? " · 请先保存字段选择" : ""}(不发送未保存备注)`,
|
|
};
|
|
|
|
export function contextLabel(context: PageContext): string {
|
|
return contextLabels[context.page](context);
|
|
}
|
|
|
|
export function pageFromHash(hash: string): WorkspacePage {
|
|
const page = hash.replace(/^#/, "");
|
|
return Object.hasOwn(contextLabels, page)
|
|
? (page as WorkspacePage)
|
|
: "alphas";
|
|
}
|
|
|
|
type Destination = {
|
|
page?: WorkspacePage;
|
|
chat: "open" | "close" | "responsive";
|
|
};
|
|
// Exhaustive action destinations prevent a new action silently falling into Alpha.
|
|
const destinations: Record<UIAction["type"], Destination> = {
|
|
open_conversation: { chat: "open" },
|
|
open_research_input: { page: "datasets", chat: "close" },
|
|
open_backtest: { page: "backtests", chat: "responsive" },
|
|
open_backtest_preview: { page: "backtests", chat: "responsive" },
|
|
open_alpha: { page: "alphas", chat: "responsive" },
|
|
apply_filters: { page: "alphas", chat: "responsive" },
|
|
};
|
|
|
|
export function actionDestination(action: UIAction): Destination | undefined {
|
|
return Object.hasOwn(destinations, action.type)
|
|
? destinations[action.type]
|
|
: undefined;
|
|
}
|