2026-07-06 15:06:19 +08:00
|
|
|
|
/**
|
2026-07-06 15:30:21 +08:00
|
|
|
|
* ConnectionBar — session picker, player info (connected state only)
|
2026-07-06 15:06:19 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
2026-07-06 15:30:21 +08:00
|
|
|
|
import { Component, createSignal, Show, For } from "solid-js";
|
2026-07-06 15:06:19 +08:00
|
|
|
|
import {
|
|
|
|
|
|
hydrateFromServer,
|
|
|
|
|
|
useJournalStream,
|
|
|
|
|
|
sessions,
|
|
|
|
|
|
createSession,
|
|
|
|
|
|
deleteSession,
|
2026-07-06 15:30:21 +08:00
|
|
|
|
disconnectStream,
|
2026-07-06 16:12:34 +08:00
|
|
|
|
setSessionId,
|
2026-07-06 15:06:19 +08:00
|
|
|
|
} from "../stores/journalStream";
|
|
|
|
|
|
import { journalSetState } from "../stores/journalStream";
|
|
|
|
|
|
|
|
|
|
|
|
export const ConnectionBar: Component = () => {
|
|
|
|
|
|
const stream = useJournalStream();
|
|
|
|
|
|
const [newSessionName, setNewSessionName] = createSignal("");
|
|
|
|
|
|
const [showNewSession, setShowNewSession] = createSignal(false);
|
2026-07-06 15:30:21 +08:00
|
|
|
|
const [error, setError] = createSignal<string | null>(null);
|
2026-07-06 15:06:19 +08:00
|
|
|
|
|
|
|
|
|
|
const manifest = sessions;
|
|
|
|
|
|
|
|
|
|
|
|
const handleCreateSession = () => {
|
|
|
|
|
|
const name = newSessionName().trim();
|
|
|
|
|
|
if (!name) return;
|
|
|
|
|
|
createSession(name);
|
|
|
|
|
|
setNewSessionName("");
|
|
|
|
|
|
setShowNewSession(false);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleDeleteSession = (id: string) => {
|
2026-07-07 23:50:39 +08:00
|
|
|
|
if (confirm(`确定要删除会话 "${id}"?此操作不可撤销。`)) {
|
2026-07-06 15:06:19 +08:00
|
|
|
|
deleteSession(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleSessionSelect = async (id: string) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
await hydrateFromServer(id);
|
2026-07-06 16:12:34 +08:00
|
|
|
|
setSessionId(id);
|
2026-07-06 15:06:19 +08:00
|
|
|
|
} catch (e) {
|
2026-07-07 23:50:39 +08:00
|
|
|
|
setError("无法加载会话");
|
2026-07-06 15:06:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-07-06 15:30:21 +08:00
|
|
|
|
const handleDisconnect = () => {
|
|
|
|
|
|
disconnectStream();
|
|
|
|
|
|
};
|
2026-07-06 15:06:19 +08:00
|
|
|
|
|
|
|
|
|
|
return (
|
2026-07-06 15:30:21 +08:00
|
|
|
|
<div class="border-b border-gray-200 px-3 py-2 space-y-2 text-sm">
|
|
|
|
|
|
{/* Status row */}
|
2026-07-06 15:06:19 +08:00
|
|
|
|
<div class="flex items-center gap-2">
|
2026-07-06 15:30:21 +08:00
|
|
|
|
<span class="w-2 h-2 rounded-full bg-green-500" />
|
2026-07-07 23:50:39 +08:00
|
|
|
|
<span class="text-xs text-gray-500">已连接</span>
|
2026-07-06 15:06:19 +08:00
|
|
|
|
<Show when={stream.sessionId}>
|
|
|
|
|
|
<span class="text-xs font-mono bg-gray-100 px-1.5 py-0.5 rounded">
|
2026-07-06 16:12:34 +08:00
|
|
|
|
{stream.sessionName || stream.sessionId}
|
2026-07-06 15:06:19 +08:00
|
|
|
|
</span>
|
|
|
|
|
|
</Show>
|
|
|
|
|
|
<div class="flex-1" />
|
2026-07-06 15:30:21 +08:00
|
|
|
|
<Show when={stream.myName !== "gm"}>
|
2026-07-07 23:50:39 +08:00
|
|
|
|
<span class="text-xs text-gray-400">扮演 {stream.myName}</span>
|
2026-07-06 15:06:19 +08:00
|
|
|
|
</Show>
|
2026-07-06 15:30:21 +08:00
|
|
|
|
<button
|
|
|
|
|
|
onClick={handleDisconnect}
|
|
|
|
|
|
class="text-xs text-gray-400 hover:text-red-500"
|
2026-07-07 23:50:39 +08:00
|
|
|
|
title="断开连接"
|
2026-07-06 15:30:21 +08:00
|
|
|
|
>
|
|
|
|
|
|
⏻
|
|
|
|
|
|
</button>
|
2026-07-06 15:06:19 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-07-06 15:30:21 +08:00
|
|
|
|
{/* GM session management */}
|
|
|
|
|
|
<Show when={stream.myName === "gm"}>
|
2026-07-06 15:06:19 +08:00
|
|
|
|
<div class="space-y-1">
|
|
|
|
|
|
<div class="flex items-center gap-1">
|
2026-07-07 23:50:39 +08:00
|
|
|
|
<span class="text-xs text-gray-500">会话列表:</span>
|
2026-07-06 15:06:19 +08:00
|
|
|
|
<button
|
|
|
|
|
|
onClick={() => setShowNewSession((v) => !v)}
|
|
|
|
|
|
class="text-xs text-blue-600 hover:underline"
|
|
|
|
|
|
>
|
2026-07-07 23:50:39 +08:00
|
|
|
|
+ 新建
|
2026-07-06 15:06:19 +08:00
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<Show when={showNewSession()}>
|
|
|
|
|
|
<div class="flex gap-1">
|
|
|
|
|
|
<input
|
|
|
|
|
|
type="text"
|
|
|
|
|
|
value={newSessionName()}
|
|
|
|
|
|
onInput={(e) => setNewSessionName(e.currentTarget.value)}
|
2026-07-07 23:50:39 +08:00
|
|
|
|
placeholder="会话名称"
|
2026-07-06 15:06:19 +08:00
|
|
|
|
class="flex-1 border border-gray-300 rounded px-2 py-0.5 text-xs"
|
|
|
|
|
|
onKeyDown={(e) => e.key === "Enter" && handleCreateSession()}
|
|
|
|
|
|
/>
|
|
|
|
|
|
<button
|
|
|
|
|
|
onClick={handleCreateSession}
|
|
|
|
|
|
class="bg-green-600 text-white rounded px-2 py-0.5 text-xs hover:bg-green-700"
|
|
|
|
|
|
>
|
2026-07-07 23:50:39 +08:00
|
|
|
|
Create Create
|
2026-07-06 15:06:19 +08:00
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</Show>
|
|
|
|
|
|
|
|
|
|
|
|
<For each={Object.entries(manifest().sessions)}>
|
|
|
|
|
|
{([id, meta]) => (
|
|
|
|
|
|
<div
|
|
|
|
|
|
class={`flex items-center gap-1 px-1 py-0.5 rounded cursor-pointer text-xs ${
|
|
|
|
|
|
id === stream.sessionId
|
|
|
|
|
|
? "bg-blue-100 text-blue-800"
|
|
|
|
|
|
: "hover:bg-gray-100"
|
|
|
|
|
|
}`}
|
|
|
|
|
|
>
|
|
|
|
|
|
<span
|
|
|
|
|
|
class="flex-1 truncate"
|
|
|
|
|
|
onClick={() => handleSessionSelect(id)}
|
|
|
|
|
|
>
|
|
|
|
|
|
{meta.name || id}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
<button
|
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
handleDeleteSession(id);
|
|
|
|
|
|
}}
|
|
|
|
|
|
class="text-gray-400 hover:text-red-500 text-xs"
|
2026-07-07 23:50:39 +08:00
|
|
|
|
title="删除会话"
|
2026-07-06 15:06:19 +08:00
|
|
|
|
>
|
|
|
|
|
|
✕
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</For>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</Show>
|
|
|
|
|
|
|
2026-07-06 15:30:21 +08:00
|
|
|
|
<Show when={error()}>
|
|
|
|
|
|
<p class="text-red-500 text-xs">{error()}</p>
|
2026-07-06 15:06:19 +08:00
|
|
|
|
</Show>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|