Free, open-source screen sharing for private live streams with friends. Watch together in a browser or self-host Piik. 免费开源的私密屏幕共享,支持游戏直播、一起看电影与自部署。
feat(ui): refine room code replacement controls (#275)
AAA0bde298051f6
diff --git a/src/client/components/RoomCode.tsx b/src/client/components/RoomCode.tsxindex 9e594b62..78ea2cb3 100644--- a/src/client/components/RoomCode.tsx+++ b/src/client/components/RoomCode.tsx@@ -1,4 +1,4 @@-import { Check, CircleAlert, Copy, Hash, RefreshCw } from "lucide-react";+import { Check, CircleAlert, Copy, Hash, RefreshCw, X } from "lucide-react";import { useEffect, useRef, useState } from "react";import { copyRoomCode } from "./room-code";@@ -7,15 +7,15 @@ type CopyState = "idle" | "copied" | "failed";export function RoomCode({roomId,onReplace,- replacing = false,replaceDisabled = false,}: {roomId: string;- onReplace?: () => void;- replacing?: boolean;+ onReplace?: () => void | Promise<void>;replaceDisabled?: boolean;}) {const [copyState, setCopyState] = useState<CopyState>("idle");+ const [confirmingReplace, setConfirmingReplace] = useState(false);+ const [replacePending, setReplacePending] = useState(false);const resetTimerRef = useRef<number | null>(null);const mountedRef = useRef(false);@@ -55,6 +55,21 @@ export function RoomCode({}}+ async function replace(): Promise<void> {+ if (!onReplace || replacePending || replaceDisabled) {+ return;+ }+ setReplacePending(true);+ try {+ await onReplace();+ } finally {+ if (mountedRef.current) {+ setReplacePending(false);+ setConfirmingReplace(false);+ }+ }+ }+const copyLabel =copyState === "copied"? "房间号已复制"@@ -67,37 +82,66 @@ export function RoomCode({<Hash size={15} aria-hidden="true" /><span className="room-code-label">房间号</span><strong>{roomId}</strong>- {onReplace && (- <button- className="room-code-copy"- type="button"- title={replacing ? "正在更换房间号" : "更换房间号"}- aria-label={replacing ? "正在更换房间号" : "更换房间号"}- disabled={replacing || replaceDisabled}- onClick={onReplace}- >- <RefreshCw- size={15}- className={replacing ? "spin" : undefined}- aria-hidden="true"- />- </button>- )}- <button- className={`room-code-copy${copyState === "failed" ? " is-failed" : ""}`}- type="button"- title={copyLabel}- aria-label={copyLabel}- onClick={() => void copy()}- >- {copyState === "copied" ? (- <Check size={15} aria-hidden="true" />- ) : copyState === "failed" ? (- <CircleAlert size={15} aria-hidden="true" />+ <span className="room-code-actions">+ {onReplace && confirmingReplace ? (+ <>+ <button+ className="room-code-copy"+ type="button"+ title={replacePending ? "正在更换房间号" : "确认更换房间号"}+ aria-label={replacePending ? "正在更换房间号" : "确认更换房间号"}+ disabled={replacePending || replaceDisabled}+ onClick={() => void replace()}+ >+ {replacePending ? (+ <RefreshCw size={15} className="spin" aria-hidden="true" />+ ) : (+ <Check size={15} aria-hidden="true" />+ )}+ </button>+ <button+ className="room-code-copy"+ type="button"+ title="取消更换房间号"+ aria-label="取消更换房间号"+ disabled={replacePending}+ onClick={() => setConfirmingReplace(false)}+ >+ <X size={15} aria-hidden="true" />+ </button>+ </>) : (- <Copy size={15} aria-hidden="true" />+ <>+ {onReplace && (+ <button+ className="room-code-copy"+ type="button"+ title="更换房间号"+ aria-label="更换房间号"+ disabled={replaceDisabled}+ onClick={() => setConfirmingReplace(true)}+ >+ <RefreshCw size={15} aria-hidden="true" />+ </button>+ )}+ <button+ className={`room-code-copy${copyState === "failed" ? " is-failed" : ""}`}+ type="button"+ title={copyLabel}+ aria-label={copyLabel}+ onClick={() => void copy()}+ >+ {copyState === "copied" ? (+ <Check size={15} aria-hidden="true" />+ ) : copyState === "failed" ? (+ <CircleAlert size={15} aria-hidden="true" />+ ) : (+ <Copy size={15} aria-hidden="true" />+ )}+ </button>+ </>)}- </button>+ </span><span className="visually-hidden" role="status" aria-live="polite">{copyState === "copied"? "房间号已复制"diff --git a/src/client/pages/HostPage.tsx b/src/client/pages/HostPage.tsxindex c362844c..a4fb50ee 100644--- a/src/client/pages/HostPage.tsx+++ b/src/client/pages/HostPage.tsx@@ -281,7 +281,6 @@ export function HostPage({ onAuthorizationRequired }: HostPageProps = {}) {"access" | "replacement" | null>(null);const roomMutating = roomMutation !== null;- const replacingRoom = roomMutation === "replacement";const [viewerPasswordEnabled, setViewerPasswordEnabled] = useState(creationProfile.roomPassword !== null,);@@ -666,13 +665,6 @@ export function HostPage({ onAuthorizationRequired }: HostPageProps = {}) {if (!activeRoom || roomMutationRef.current || phase === "starting") {return;}- if (- !window.confirm(- "更换房间号将结束当前分享,并使旧邀请和房间密码失效。继续吗?",- )- ) {- return;- }const mutation = beginRoomMutation("replacement");if (!mutation) {return;@@ -699,7 +691,7 @@ export function HostPage({ onAuthorizationRequired }: HostPageProps = {}) {setViewerPasswordEnabled(profile.roomPassword !== null);setViewerPasswordDraft(profile.roomPassword ?? "");setViewerPasswordVisible(false);- setNotice("房间号已更换");+ setNotice(null);setPhase(wasSharing ? "ended" : "idle");} catch (error) {if (error instanceof ApiError && error.status === 404) {@@ -2159,8 +2151,7 @@ export function HostPage({ onAuthorizationRequired }: HostPageProps = {}) {{room && (<RoomCoderoomId={room.roomId}- onReplace={() => void replaceCurrentRoom()}- replacing={replacingRoom}+ onReplace={replaceCurrentRoom}replaceDisabled={phase === "starting" || roomMutating}/>)}diff --git a/src/client/styles.css b/src/client/styles.cssindex 6021709c..64e37c26 100644--- a/src/client/styles.css+++ b/src/client/styles.css@@ -221,6 +221,12 @@ button:disabled {white-space: nowrap;}+.room-code-actions {+ display: inline-flex;+ height: 30px;+ flex: 0 0 auto;+}+.room-code-copy {display: grid;width: 31px;