fix: 优化核心包

This commit is contained in:
Daniel
2026-04-07 20:15:19 +08:00
parent 84f8be7c0e
commit 6220c5d6c5
4 changed files with 344 additions and 44 deletions

View File

@@ -1,16 +1,76 @@
import { useState } from "react";
import { useEffect, useState } from "react";
import { api } from "../api/client";
import { JsonPanel } from "../components/JsonPanel";
import { MatchList } from "../components/MatchList";
const DEFAULT_TEXT = "明天下午南山会展中心需要2个签到协助5小时150/人,女生优先,需要会签到、引导和登记。";
const FALLBACK_TEXT = "明天下午南山会展中心需要2个签到协助5小时150/人,女生优先,需要会签到、引导和登记。";
function pickRandom<T>(items: T[]): T {
return items[Math.floor(Math.random() * items.length)];
}
function asString(value: unknown): string {
return typeof value === "string" ? value.trim() : "";
}
function asNumber(value: unknown): number | null {
return typeof value === "number" && Number.isFinite(value) ? value : null;
}
function asStringArray(value: unknown): string[] {
if (!Array.isArray(value)) {
return [];
}
return value.filter((item): item is string => typeof item === "string" && item.trim().length > 0);
}
function buildAdaptiveJobText(items: Record<string, unknown>[]): string {
if (!items.length) {
return FALLBACK_TEXT;
}
const source = pickRandom(items);
const title = asString(source.title) || asString(source.category) || "活动兼职";
const city = asString(source.city) || "深圳";
const region = asString(source.region) || "南山";
const headcount = asNumber(source.headcount) ?? 2;
const duration = asNumber(source.duration_hours) ?? 4;
const location = asString(source.location_detail) || `${city}${region}待定点位`;
const skills = asStringArray(source.skills).slice(0, 3);
const tags = asStringArray(source.tags).slice(0, 2);
const salary = (source.salary as Record<string, unknown> | undefined) ?? {};
const amount = asNumber(salary.amount) ?? 150;
const skillText = skills.length ? `需要会${skills.join("、")}` : "有相关经验优先";
const tagText = tags.length ? `${tags.join("")}` : "";
return `明天下午${location}需要${headcount}${title}${duration}小时,${amount}/人${tagText}${skillText}`;
}
export function JobPage() {
const [text, setText] = useState(DEFAULT_TEXT);
const [text, setText] = useState("");
const [jobCard, setJobCard] = useState<unknown>(null);
const [matches, setMatches] = useState<any[]>([]);
const [loading, setLoading] = useState(false);
useEffect(() => {
let active = true;
void (async () => {
try {
const result = await api.jobs();
if (!active) {
return;
}
setText((current) => current || buildAdaptiveJobText(result.items));
} catch {
if (!active) {
return;
}
setText((current) => current || FALLBACK_TEXT);
}
})();
return () => {
active = false;
};
}, []);
const handleExtract = async () => {
setLoading(true);
try {

View File

@@ -1,16 +1,73 @@
import { useState } from "react";
import { useEffect, useState } from "react";
import { api } from "../api/client";
import { JsonPanel } from "../components/JsonPanel";
import { MatchList } from "../components/MatchList";
const DEFAULT_TEXT = "我做过商场促销和活动签到,也能做登记和引导,周末都能接,福田南山都方便。";
const FALLBACK_TEXT = "我做过商场促销和活动签到,也能做登记和引导,周末都能接,福田南山都方便。";
function pickRandom<T>(items: T[]): T {
return items[Math.floor(Math.random() * items.length)];
}
function asString(value: unknown): string {
return typeof value === "string" ? value.trim() : "";
}
function asStringArray(value: unknown): string[] {
if (!Array.isArray(value)) {
return [];
}
return value.filter((item): item is string => typeof item === "string" && item.trim().length > 0);
}
function buildAdaptiveWorkerText(items: Record<string, unknown>[]): string {
if (!items.length) {
return FALLBACK_TEXT;
}
const source = pickRandom(items);
const name = asString(source.name) || "我";
const regions = asStringArray(source.regions).slice(0, 2);
const experiences = asStringArray(source.experience_tags).slice(0, 2);
const skillObjects = Array.isArray(source.skills) ? source.skills : [];
const skills = skillObjects
.map((item) => (item && typeof item === "object" ? asString((item as Record<string, unknown>).name) : ""))
.filter(Boolean)
.slice(0, 3);
const availability = asStringArray(source.availability);
const expText = experiences.length ? experiences.join("和") : "活动执行";
const skillText = skills.length ? skills.join("、") : "沟通和执行";
const regionText = regions.length ? `${regions.join("、")}都方便` : "同城都方便";
const timeText = availability.some((item) => item.includes("weekend")) ? "周末都能接" : "时间比较灵活";
return `${name}做过${expText},也能做${skillText}${timeText}${regionText}`;
}
export function WorkerPage() {
const [text, setText] = useState(DEFAULT_TEXT);
const [text, setText] = useState("");
const [workerCard, setWorkerCard] = useState<unknown>(null);
const [matches, setMatches] = useState<any[]>([]);
const [loading, setLoading] = useState(false);
useEffect(() => {
let active = true;
void (async () => {
try {
const result = await api.workers();
if (!active) {
return;
}
setText((current) => current || buildAdaptiveWorkerText(result.items));
} catch {
if (!active) {
return;
}
setText((current) => current || FALLBACK_TEXT);
}
})();
return () => {
active = false;
};
}, []);
const handleExtract = async () => {
setLoading(true);
try {