// MessageComposer — the standalone "消息构建" tab: pick a target (group or
// friend), build a message visually, preview it, send it. A message containing
// forward nodes goes through the forward-send action; otherwise the plain
// send_*_msg. The chosen group provides the @-member context to the builder.
import { useEffect, useMemo, useState } from 'react';
import { motion } from 'motion/react';
import { Loader2, MessageSquarePlus, Send } from 'lucide-react';
import { Dropdown } from '@/components/interior/dropdown';
import { Picker } from '@/components/ui/picker';
import { JsonTree } from '@/components/ui/json-tree';
import { Segmented } from '@/components/debug/segmented';
import {
MessageBuilder,
hasNodes,
previewSegments,
toOneBot,
validateSegs,
type Seg,
} from '@/components/debug/message-builder';
import { useFriends, useGroups } from '@/hooks/use-debug-contacts';
import { useApi } from '@/lib/api';
import { cn } from '@/lib/utils';
import type { DebugInvokeResult, QQInfo } from '@/types';
const cardCls =
'rounded-2xl border border-border/60 bg-card/80 shadow-[0_1px_2px_rgb(0_0_0/0.04),0_8px_24px_-12px_rgb(0_0_0/0.10)] backdrop-blur-sm';
const looksLikeId = (s: string) => /^\d{3,}$/.test(s.trim());
function TargetPicker({
kind,
uin,
value,
onChange,
}: {
kind: 'group' | 'friend';
uin: string;
value: string;
onChange: (v: string) => void;
}) {
const groups = useGroups(kind === 'group' ? uin : '');
const friends = useFriends(kind === 'friend' ? uin : '');
const src = kind === 'group' ? groups : friends;
return (
);
}
export function MessageComposer({ accounts }: { accounts: QQInfo[] }) {
const api = useApi();
const [uin, setUin] = useState('');
const [kind, setKind] = useState<'group' | 'friend'>('group');
const [target, setTarget] = useState('');
const [segs, setSegs] = useState([]);
const [sending, setSending] = useState(false);
const [result, setResult] = useState(null);
// default to the first account once loaded
useEffect(() => {
if (!uin && accounts[0]) {
setUin(accounts[0].uin);
}
}, [accounts, uin]);
const preview = useMemo(() => previewSegments(segs), [segs]);
const groupId = kind === 'group' ? target : '';
const send = async () => {
if (!uin) {
setResult({ error: '请选择账号' });
return;
}
if (!target) {
setResult({ error: kind === 'group' ? '请选择群' : '请选择好友' });
return;
}
if (segs.length === 0) {
setResult({ error: '消息为空' });
return;
}
const forward = hasNodes(segs);
// Forward messages must be ALL nodes: the backend silently drops top-level
// non-node segments, so a mixed message would send less than the preview
// shows. Block it with a clear message instead.
if (forward && !segs.every((s) => s.k === 'node')) {
setResult({
error: '合并转发消息只能由转发节点组成 — 请移除非节点段,或把它们放进某个转发节点内',
});
return;
}
const incomplete = validateSegs(segs);
if (incomplete) {
setResult({ error: incomplete });
return;
}
const payload = toOneBot(segs);
const action = forward
? kind === 'group'
? 'send_group_forward_msg'
: 'send_private_forward_msg'
: kind === 'group'
? 'send_group_msg'
: 'send_private_msg';
const params: Record =
kind === 'group' ? { group_id: target } : { user_id: target };
params[forward ? 'messages' : 'message'] = payload;
setSending(true);
setResult(null);
try {
setResult(await api.debug.invoke(uin, action, params));
} catch (e) {
setResult({ error: e instanceof Error ? e.message : '发送失败' });
} finally {
setSending(false);
}
};
const resultFailed = !!result && ('error' in result || result.status === 'failed');
return (
消息构建
{/* Fixed h-8 == the target column's segmented-control height, so both
label rows are exactly equal and the two selectors below share a
baseline (min-h-7 wasn't enough — the segmented is ~32px). */}