refactor(sse): move status indicator to avatar badge
Replace the standalone SSE dot above UserMenu with a small badge overlaid on the bottom-right of the user avatar. Only visible when status is connecting (yellow) or disconnected (red) — invisible in the normal connected state, removing constant visual noise.
This commit is contained in:
parent
ea0892a8b2
commit
0cf8805184
9 changed files with 54 additions and 73 deletions
|
|
@ -647,6 +647,12 @@ func (s *APIV1Service) CreateMemoComment(ctx context.Context, request *v1pb.Crea
|
||||||
slog.Warn("Failed to dispatch memo comment created webhook", slog.Any("err", err))
|
slog.Warn("Failed to dispatch memo comment created webhook", slog.Any("err", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Broadcast live refresh event for the parent memo so subscribers see the new comment.
|
||||||
|
s.SSEHub.Broadcast(&SSEEvent{
|
||||||
|
Type: SSEEventMemoCommentCreated,
|
||||||
|
Name: request.Name,
|
||||||
|
})
|
||||||
|
|
||||||
return memoComment, nil
|
return memoComment, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,18 +26,10 @@ func RegisterSSERoutes(echoServer *echo.Echo, hub *SSEHub, storeInstance *store.
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleSSE handles the SSE connection for live memo refresh.
|
// handleSSE handles the SSE connection for live memo refresh.
|
||||||
// Authentication is done via Bearer token in the Authorization header,
|
// Authentication is done via Bearer token in the Authorization header.
|
||||||
// or via the "token" query parameter (for EventSource which cannot set headers).
|
|
||||||
func handleSSE(c *echo.Context, hub *SSEHub, authenticator *auth.Authenticator) error {
|
func handleSSE(c *echo.Context, hub *SSEHub, authenticator *auth.Authenticator) error {
|
||||||
// Authenticate the request.
|
// Authenticate the request.
|
||||||
authHeader := c.Request().Header.Get("Authorization")
|
authHeader := c.Request().Header.Get("Authorization")
|
||||||
if authHeader == "" {
|
|
||||||
// Fall back to query parameter for native EventSource support.
|
|
||||||
if token := c.QueryParam("token"); token != "" {
|
|
||||||
authHeader = "Bearer " + token
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
result := authenticator.Authenticate(c.Request().Context(), authHeader)
|
result := authenticator.Authenticate(c.Request().Context(), authHeader)
|
||||||
if result == nil {
|
if result == nil {
|
||||||
return c.JSON(http.StatusUnauthorized, map[string]string{"error": "authentication required"})
|
return c.JSON(http.StatusUnauthorized, map[string]string{"error": "authentication required"})
|
||||||
|
|
|
||||||
|
|
@ -10,11 +10,12 @@ import (
|
||||||
type SSEEventType string
|
type SSEEventType string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
SSEEventMemoCreated SSEEventType = "memo.created"
|
SSEEventMemoCreated SSEEventType = "memo.created"
|
||||||
SSEEventMemoUpdated SSEEventType = "memo.updated"
|
SSEEventMemoUpdated SSEEventType = "memo.updated"
|
||||||
SSEEventMemoDeleted SSEEventType = "memo.deleted"
|
SSEEventMemoDeleted SSEEventType = "memo.deleted"
|
||||||
SSEEventReactionUpserted SSEEventType = "reaction.upserted"
|
SSEEventMemoCommentCreated SSEEventType = "memo.comment.created"
|
||||||
SSEEventReactionDeleted SSEEventType = "reaction.deleted"
|
SSEEventReactionUpserted SSEEventType = "reaction.upserted"
|
||||||
|
SSEEventReactionDeleted SSEEventType = "reaction.deleted"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SSEEvent represents a change event sent to SSE clients.
|
// SSEEvent represents a change event sent to SSE clients.
|
||||||
|
|
|
||||||
|
|
@ -69,18 +69,10 @@ func TestSSEHandler_Authentication(t *testing.T) {
|
||||||
require.Equal(t, "text/event-stream", rec.Header().Get("Content-Type"))
|
require.Equal(t, "text/event-stream", rec.Header().Get("Content-Type"))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("token in query param returns 200", func(t *testing.T) {
|
t.Run("token in query param returns 401", func(t *testing.T) {
|
||||||
reqCtx, cancel := context.WithCancel(context.Background())
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/sse?token="+token, nil)
|
||||||
defer cancel()
|
|
||||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/sse?token="+token, nil).WithContext(reqCtx)
|
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
done := make(chan struct{})
|
e.ServeHTTP(rec, req)
|
||||||
go func() {
|
require.Equal(t, http.StatusUnauthorized, rec.Code)
|
||||||
defer close(done)
|
|
||||||
e.ServeHTTP(rec, req)
|
|
||||||
}()
|
|
||||||
cancel()
|
|
||||||
<-done
|
|
||||||
require.Equal(t, http.StatusOK, rec.Code)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ import { Routes } from "@/router";
|
||||||
import { UserNotification_Status } from "@/types/proto/api/v1/user_service_pb";
|
import { UserNotification_Status } from "@/types/proto/api/v1/user_service_pb";
|
||||||
import { useTranslate } from "@/utils/i18n";
|
import { useTranslate } from "@/utils/i18n";
|
||||||
import MemosLogo from "./MemosLogo";
|
import MemosLogo from "./MemosLogo";
|
||||||
import SSEStatusIndicator from "./SSEStatusIndicator";
|
|
||||||
import UserMenu from "./UserMenu";
|
import UserMenu from "./UserMenu";
|
||||||
|
|
||||||
interface NavLinkItem {
|
interface NavLinkItem {
|
||||||
|
|
@ -115,10 +114,7 @@ const Navigation = (props: Props) => {
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
{currentUser && (
|
{currentUser && (
|
||||||
<div className={cn("w-full flex flex-col justify-end gap-1", props.collapsed ? "items-center" : "items-start pl-3")}>
|
<div className={cn("w-full flex flex-col justify-end", props.collapsed ? "items-center" : "items-start pl-3")}>
|
||||||
<div className={cn("flex items-center", props.collapsed ? "justify-center" : "pl-1")}>
|
|
||||||
<SSEStatusIndicator />
|
|
||||||
</div>
|
|
||||||
<UserMenu collapsed={collapsed} />
|
<UserMenu collapsed={collapsed} />
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
|
||||||
|
|
@ -1,36 +0,0 @@
|
||||||
import { useSSEConnectionStatus } from "@/hooks/useLiveMemoRefresh";
|
|
||||||
import { cn } from "@/lib/utils";
|
|
||||||
import { Tooltip, TooltipContent, TooltipTrigger } from "./ui/tooltip";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A small colored dot that indicates the SSE live-refresh connection status.
|
|
||||||
* - Green = connected (live updates active)
|
|
||||||
* - Yellow/pulsing = connecting
|
|
||||||
* - Red = disconnected (updates not live)
|
|
||||||
*/
|
|
||||||
const SSEStatusIndicator = () => {
|
|
||||||
const status = useSSEConnectionStatus();
|
|
||||||
|
|
||||||
const label =
|
|
||||||
status === "connected" ? "Live updates active" : status === "connecting" ? "Connecting to live updates..." : "Live updates unavailable";
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Tooltip>
|
|
||||||
<TooltipTrigger asChild>
|
|
||||||
<span className="inline-flex items-center justify-center size-5 cursor-default" aria-label={label}>
|
|
||||||
<span
|
|
||||||
className={cn(
|
|
||||||
"block size-2 rounded-full transition-colors",
|
|
||||||
status === "connected" && "bg-green-500",
|
|
||||||
status === "connecting" && "bg-yellow-500 animate-pulse",
|
|
||||||
status === "disconnected" && "bg-red-500",
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
</span>
|
|
||||||
</TooltipTrigger>
|
|
||||||
<TooltipContent>{label}</TooltipContent>
|
|
||||||
</Tooltip>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default SSEStatusIndicator;
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import { ArchiveIcon, CheckIcon, GlobeIcon, LogOutIcon, PaletteIcon, SettingsIcon, SquareUserIcon, User2Icon } from "lucide-react";
|
import { ArchiveIcon, CheckIcon, GlobeIcon, LogOutIcon, PaletteIcon, SettingsIcon, SquareUserIcon, User2Icon } from "lucide-react";
|
||||||
import { useAuth } from "@/contexts/AuthContext";
|
import { useAuth } from "@/contexts/AuthContext";
|
||||||
import useCurrentUser from "@/hooks/useCurrentUser";
|
import useCurrentUser from "@/hooks/useCurrentUser";
|
||||||
|
import { useSSEConnectionStatus } from "@/hooks/useLiveMemoRefresh";
|
||||||
import useNavigateTo from "@/hooks/useNavigateTo";
|
import useNavigateTo from "@/hooks/useNavigateTo";
|
||||||
import { useUpdateUserGeneralSetting } from "@/hooks/useUserQueries";
|
import { useUpdateUserGeneralSetting } from "@/hooks/useUserQueries";
|
||||||
import { locales } from "@/i18n";
|
import { locales } from "@/i18n";
|
||||||
|
|
@ -18,6 +19,7 @@ import {
|
||||||
DropdownMenuSubTrigger,
|
DropdownMenuSubTrigger,
|
||||||
DropdownMenuTrigger,
|
DropdownMenuTrigger,
|
||||||
} from "./ui/dropdown-menu";
|
} from "./ui/dropdown-menu";
|
||||||
|
import { Tooltip, TooltipContent, TooltipTrigger } from "./ui/tooltip";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
collapsed?: boolean;
|
collapsed?: boolean;
|
||||||
|
|
@ -30,6 +32,7 @@ const UserMenu = (props: Props) => {
|
||||||
const currentUser = useCurrentUser();
|
const currentUser = useCurrentUser();
|
||||||
const { userGeneralSetting, refetchSettings, logout } = useAuth();
|
const { userGeneralSetting, refetchSettings, logout } = useAuth();
|
||||||
const { mutate: updateUserGeneralSetting } = useUpdateUserGeneralSetting(currentUser?.name);
|
const { mutate: updateUserGeneralSetting } = useUpdateUserGeneralSetting(currentUser?.name);
|
||||||
|
const sseStatus = useSSEConnectionStatus();
|
||||||
const currentLocale = getLocaleWithFallback(userGeneralSetting?.locale);
|
const currentLocale = getLocaleWithFallback(userGeneralSetting?.locale);
|
||||||
const currentTheme = getThemeWithFallback(userGeneralSetting?.theme);
|
const currentTheme = getThemeWithFallback(userGeneralSetting?.theme);
|
||||||
|
|
||||||
|
|
@ -93,11 +96,26 @@ const UserMenu = (props: Props) => {
|
||||||
<DropdownMenu>
|
<DropdownMenu>
|
||||||
<DropdownMenuTrigger asChild disabled={!currentUser}>
|
<DropdownMenuTrigger asChild disabled={!currentUser}>
|
||||||
<div className={cn("w-auto flex flex-row justify-start items-center cursor-pointer text-foreground", collapsed ? "px-1" : "px-3")}>
|
<div className={cn("w-auto flex flex-row justify-start items-center cursor-pointer text-foreground", collapsed ? "px-1" : "px-3")}>
|
||||||
{currentUser?.avatarUrl ? (
|
<div className="relative shrink-0">
|
||||||
<UserAvatar className="shrink-0" avatarUrl={currentUser?.avatarUrl} />
|
{currentUser?.avatarUrl ? (
|
||||||
) : (
|
<UserAvatar avatarUrl={currentUser?.avatarUrl} />
|
||||||
<User2Icon className="w-6 mx-auto h-auto text-muted-foreground" />
|
) : (
|
||||||
)}
|
<User2Icon className="w-6 mx-auto h-auto text-muted-foreground" />
|
||||||
|
)}
|
||||||
|
{sseStatus !== "connected" && (
|
||||||
|
<Tooltip>
|
||||||
|
<TooltipTrigger asChild>
|
||||||
|
<span
|
||||||
|
className={cn(
|
||||||
|
"absolute -bottom-0.5 -right-0.5 size-2.5 rounded-full border-2 border-background",
|
||||||
|
sseStatus === "connecting" ? "bg-muted-foreground animate-pulse" : "bg-destructive",
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
</TooltipTrigger>
|
||||||
|
<TooltipContent side="right">{t(`sse.${sseStatus}` as Parameters<typeof t>[0])}</TooltipContent>
|
||||||
|
</Tooltip>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
{!collapsed && (
|
{!collapsed && (
|
||||||
<span className="ml-2 text-lg font-medium text-foreground grow truncate">
|
<span className="ml-2 text-lg font-medium text-foreground grow truncate">
|
||||||
{currentUser?.displayName || currentUser?.username}
|
{currentUser?.displayName || currentUser?.username}
|
||||||
|
|
|
||||||
|
|
@ -64,6 +64,7 @@ export function useLiveMemoRefresh() {
|
||||||
const retryDelayRef = useRef(INITIAL_RETRY_DELAY_MS);
|
const retryDelayRef = useRef(INITIAL_RETRY_DELAY_MS);
|
||||||
const abortControllerRef = useRef<AbortController | null>(null);
|
const abortControllerRef = useRef<AbortController | null>(null);
|
||||||
|
|
||||||
|
const currentUserName = currentUser?.name;
|
||||||
const handleEvent = useCallback((event: SSEChangeEvent) => handleSSEEvent(event, queryClient), [queryClient]);
|
const handleEvent = useCallback((event: SSEChangeEvent) => handleSSEEvent(event, queryClient), [queryClient]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
@ -158,6 +159,7 @@ export function useLiveMemoRefresh() {
|
||||||
return () => {
|
return () => {
|
||||||
mounted = false;
|
mounted = false;
|
||||||
setSSEStatus("disconnected");
|
setSSEStatus("disconnected");
|
||||||
|
retryDelayRef.current = INITIAL_RETRY_DELAY_MS;
|
||||||
if (retryTimeout) {
|
if (retryTimeout) {
|
||||||
clearTimeout(retryTimeout);
|
clearTimeout(retryTimeout);
|
||||||
}
|
}
|
||||||
|
|
@ -165,7 +167,7 @@ export function useLiveMemoRefresh() {
|
||||||
abortControllerRef.current.abort();
|
abortControllerRef.current.abort();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}, [handleEvent, currentUser]);
|
}, [handleEvent, currentUserName]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
@ -195,6 +197,11 @@ function handleSSEEvent(event: SSEChangeEvent, queryClient: ReturnType<typeof us
|
||||||
queryClient.invalidateQueries({ queryKey: userKeys.stats() });
|
queryClient.invalidateQueries({ queryKey: userKeys.stats() });
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case "memo.comment.created":
|
||||||
|
queryClient.invalidateQueries({ queryKey: memoKeys.comments(event.name) });
|
||||||
|
queryClient.invalidateQueries({ queryKey: memoKeys.detail(event.name) });
|
||||||
|
break;
|
||||||
|
|
||||||
case "reaction.upserted":
|
case "reaction.upserted":
|
||||||
case "reaction.deleted":
|
case "reaction.deleted":
|
||||||
queryClient.invalidateQueries({ queryKey: memoKeys.detail(event.name) });
|
queryClient.invalidateQueries({ queryKey: memoKeys.detail(event.name) });
|
||||||
|
|
|
||||||
|
|
@ -463,5 +463,10 @@
|
||||||
"select-visibility": "Visibility",
|
"select-visibility": "Visibility",
|
||||||
"tags": "Tags",
|
"tags": "Tags",
|
||||||
"upload-attachment": "Upload Attachment(s)"
|
"upload-attachment": "Upload Attachment(s)"
|
||||||
|
},
|
||||||
|
"sse": {
|
||||||
|
"connected": "Live updates active",
|
||||||
|
"connecting": "Connecting to live updates...",
|
||||||
|
"disconnected": "Live updates unavailable"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue