nomadro/src/modules/social/components/TripEditor.tsx
Eric abdcafb1eb Ship Work access gate, pads/boards upgrades, and locale hydration fix on .com.
Gate /social/work behind join/login/profile; harden Work UX and LanguageSwitcher SSR; deploy portal, admin, and work API.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-08 00:14:39 +08:00

206 lines
5.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use client";
import { useEffect, useState, type FormEvent } from "react";
import Link from "next/link";
import { socialApi } from "@social/lib/api";
import { SOCIAL_CITIES } from "@social/lib/cities";
import type { SocialTrip, TravelOverlap } from "@social/lib/geoTypes";
import { socialPath } from "@social/lib/socialPath";
import { socialTx } from "@social/lib/socialTx";
const EMPTY = {
city: "",
city_id: "",
arrival_date: "",
departure_date: "",
status: "planned",
visibility: "public",
};
export default function TripEditor() {
const [trips, setTrips] = useState<SocialTrip[]>([]);
const [overlaps, setOverlaps] = useState<TravelOverlap[]>([]);
const [form, setForm] = useState(EMPTY);
const [busy, setBusy] = useState(false);
const [msg, setMsg] = useState("");
const reload = async () => {
try {
const [t, o] = await Promise.all([
socialApi.listTrips(),
socialApi.travelOverlaps().catch(() => ({ items: [] as TravelOverlap[] })),
]);
setTrips(t.items || []);
setOverlaps(o.items || []);
} catch {
setMsg(socialTx("行程暂时不可用,请稍后再试"));
}
};
useEffect(() => {
void reload();
}, []);
const onCity = (cityId: string) => {
const c = SOCIAL_CITIES.find((x) => x.id === cityId);
setForm((f) => ({
...f,
city_id: cityId,
city: c?.name || "",
}));
};
const submit = async (e: FormEvent) => {
e.preventDefault();
if (!form.city.trim() || busy) return;
setBusy(true);
setMsg("");
try {
await socialApi.createTrip({
city: form.city.trim(),
city_id: form.city_id || undefined,
arrival_date: form.arrival_date || undefined,
departure_date: form.departure_date || undefined,
status: form.status,
visibility: form.visibility,
});
setForm(EMPTY);
setMsg("行程已保存");
await reload();
} catch {
setMsg(socialTx("保存失败,请稍后再试"));
} finally {
setBusy(false);
}
};
const remove = async (id: string) => {
try {
await socialApi.deleteTrip(id);
await reload();
} catch {
setMsg("删除失败");
}
};
return (
<section className="social-panel social-panel-fx reveal social-trip-editor">
<div className="social-section-label">
<span>行程 · Current / Next</span>
</div>
<form className="social-form" onSubmit={(e) => void submit(e)}>
<fieldset disabled={busy} className="social-form-grid social-fieldset">
<label>
城市
<select
value={form.city_id}
onChange={(e) => onCity(e.target.value)}
required
>
<option value="">选择城市</option>
{SOCIAL_CITIES.map((c) => (
<option key={c.id} value={c.id}>
{c.emoji} {c.name}
</option>
))}
</select>
</label>
<label>
状态
<select
value={form.status}
onChange={(e) => setForm((f) => ({ ...f, status: e.target.value }))}
>
<option value="here">此刻在这儿 (Current)</option>
<option value="planned">下一站 (Next)</option>
<option value="past">已离开</option>
</select>
</label>
<label>
抵达
<input
type="date"
value={form.arrival_date}
onChange={(e) =>
setForm((f) => ({ ...f, arrival_date: e.target.value }))
}
/>
</label>
<label>
离开
<input
type="date"
value={form.departure_date}
onChange={(e) =>
setForm((f) => ({ ...f, departure_date: e.target.value }))
}
/>
</label>
<label>
可见性
<select
value={form.visibility}
onChange={(e) =>
setForm((f) => ({ ...f, visibility: e.target.value }))
}
>
<option value="public">公开</option>
<option value="friends">仅好友</option>
<option value="private">私密</option>
</select>
</label>
</fieldset>
<button type="submit" className="social-btn primary" disabled={busy || !form.city}>
{busy ? "保存中…" : "添加行程"}
</button>
</form>
{trips.length ? (
<ul className="social-trip-list">
{trips.map((t) => (
<li key={t.id}>
<strong>
{t.city}
<span>{t.status}</span>
</strong>
<em>
{[t.arrival_date, t.departure_date].filter(Boolean).join(" → ") ||
"日期未填"}
</em>
<button type="button" className="social-btn social-btn-ghost" onClick={() => void remove(t.id)}>
删除
</button>
</li>
))}
</ul>
) : (
<p className="social-muted">还没有行程。加上 Current / Next,方便重叠发现。</p>
)}
{overlaps.length ? (
<div className="social-overlap-box">
<h3>行程重叠</h3>
<ul>
{overlaps.slice(0, 6).map((o, i) => (
<li key={`${o.myTripId}-${i}`}>
<Link href={socialPath(`/places/${o.cityId || ""}`)}>
{o.city} · 重合 {o.overlapDays} 天
</Link>
<span>
{o.from} → {o.to}
</span>
</li>
))}
</ul>
</div>
) : null}
{msg ? (
<p className="social-status" role="status">
{msg}
</p>
) : null}
</section>
);
}