feat: add keep alive
This commit is contained in:
@@ -264,3 +264,132 @@
|
||||
.app-root--sidebar-expanded .stage--with-sidebar {
|
||||
margin-left: var(--sidebar-width-expanded);
|
||||
}
|
||||
|
||||
.sidebar-app-combobox {
|
||||
position: relative;
|
||||
flex: 1 1 auto;
|
||||
min-width: 0;
|
||||
|
||||
opacity: 0;
|
||||
max-width: 0;
|
||||
pointer-events: none;
|
||||
|
||||
transition:
|
||||
opacity var(--dur-fast) var(--ease-standard),
|
||||
max-width var(--dur-base) var(--ease-standard);
|
||||
}
|
||||
|
||||
.sidebar-rail--expanded .sidebar-app-combobox {
|
||||
opacity: 1;
|
||||
max-width: 200px;
|
||||
pointer-events: auto;
|
||||
transition-delay: var(--dur-fast);
|
||||
}
|
||||
|
||||
.sidebar-app-trigger {
|
||||
width: 100%;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: var(--space-2);
|
||||
|
||||
height: calc(var(--header-height) * 0.6);
|
||||
padding: 0 var(--space-2);
|
||||
|
||||
background: var(--bg-panel);
|
||||
border: var(--border-width) solid var(--border-hairline);
|
||||
border-radius: var(--radius-sm);
|
||||
|
||||
color: var(--text-primary);
|
||||
font-family: var(--font-ui);
|
||||
font-size: var(--font-xs);
|
||||
font-weight: var(--font-weight-semibold);
|
||||
|
||||
cursor: pointer;
|
||||
transition: border-color var(--dur-base) var(--ease-standard);
|
||||
}
|
||||
|
||||
.sidebar-app-trigger:hover,
|
||||
.sidebar-app-trigger--open {
|
||||
border-color: var(--accent-brand);
|
||||
}
|
||||
|
||||
.sidebar-app-trigger-label {
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.sidebar-app-trigger-caret {
|
||||
flex-shrink: 0;
|
||||
color: var(--text-muted);
|
||||
transition: transform var(--dur-base) var(--ease-standard);
|
||||
}
|
||||
|
||||
.sidebar-app-trigger--open .sidebar-app-trigger-caret {
|
||||
transform: rotate(180deg);
|
||||
}
|
||||
|
||||
.sidebar-app-input {
|
||||
width: 100%;
|
||||
height: calc(var(--header-height) * 0.6);
|
||||
padding: 0 var(--space-2);
|
||||
|
||||
background: var(--bg-panel);
|
||||
border: var(--border-width) solid var(--accent-brand);
|
||||
border-radius: var(--radius-sm);
|
||||
|
||||
color: var(--text-primary);
|
||||
font-family: var(--font-ui);
|
||||
font-size: var(--font-xs);
|
||||
font-weight: var(--font-weight-semibold);
|
||||
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.sidebar-app-listbox {
|
||||
position: absolute;
|
||||
top: calc(100% + var(--space-1));
|
||||
left: 0;
|
||||
right: 0;
|
||||
z-index: 40;
|
||||
|
||||
margin: 0;
|
||||
padding: var(--space-1);
|
||||
list-style: none;
|
||||
|
||||
background: var(--bg-panel);
|
||||
border: var(--border-width) solid var(--border-hairline);
|
||||
border-radius: var(--radius-sm);
|
||||
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
|
||||
|
||||
max-height: 240px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.sidebar-app-option {
|
||||
padding: var(--space-2);
|
||||
border-radius: var(--radius-sm);
|
||||
font-size: var(--font-xs);
|
||||
color: var(--text-primary);
|
||||
cursor: pointer;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.sidebar-app-option--highlight {
|
||||
background: var(--bg-raised);
|
||||
}
|
||||
|
||||
.sidebar-app-option--selected {
|
||||
color: var(--accent-brand);
|
||||
font-weight: var(--font-weight-semibold);
|
||||
}
|
||||
.sidebar-rail--expanded .sidebar-app-select {
|
||||
opacity: 1;
|
||||
max-width: 200px;
|
||||
padding-inline: var(--space-2);
|
||||
pointer-events: auto;
|
||||
transition-delay: var(--dur-fast);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useRef } from 'react';
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import './Sidebar.css';
|
||||
import { ChevronLeftIcon } from '../../assets/icons/ChevronLeftIcon';
|
||||
|
||||
@@ -8,6 +8,11 @@ export type SidebarItem = {
|
||||
icon: React.ReactNode;
|
||||
};
|
||||
|
||||
export type SidebarAppOption = {
|
||||
id: string;
|
||||
name: string;
|
||||
};
|
||||
|
||||
function SidebarButton({
|
||||
item,
|
||||
active,
|
||||
@@ -31,11 +36,178 @@ function SidebarButton({
|
||||
);
|
||||
}
|
||||
|
||||
function AppCombobox({
|
||||
appOptions,
|
||||
selectedAppId,
|
||||
appName,
|
||||
onAppChange,
|
||||
onAppRename,
|
||||
}: {
|
||||
appOptions: SidebarAppOption[];
|
||||
selectedAppId?: string;
|
||||
appName: string;
|
||||
onAppChange?: (id: string) => void;
|
||||
onAppRename?: (name: string) => void;
|
||||
}) {
|
||||
const currentOption = appOptions.find((o) => o.id === selectedAppId) ?? appOptions[0];
|
||||
|
||||
const [open, setOpen] = useState(false);
|
||||
const [editing, setEditing] = useState(false);
|
||||
const [inputValue, setInputValue] = useState(currentOption?.name ?? appName);
|
||||
const [highlight, setHighlight] = useState(0);
|
||||
|
||||
const rootRef = useRef<HTMLDivElement>(null);
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!editing) setInputValue(currentOption?.name ?? appName);
|
||||
}, [currentOption?.id, currentOption?.name, appName, editing]);
|
||||
|
||||
// Close on outside click
|
||||
useEffect(() => {
|
||||
function handleClick(e: MouseEvent) {
|
||||
if (rootRef.current && !rootRef.current.contains(e.target as Node)) {
|
||||
setOpen(false);
|
||||
if (editing) commitRename();
|
||||
}
|
||||
}
|
||||
document.addEventListener('mousedown', handleClick);
|
||||
return () => document.removeEventListener('mousedown', handleClick);
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [editing, inputValue]);
|
||||
|
||||
function openList() {
|
||||
setOpen(true);
|
||||
const idx = appOptions.findIndex((o) => o.id === currentOption?.id);
|
||||
setHighlight(idx >= 0 ? idx : 0);
|
||||
}
|
||||
|
||||
function pick(opt: SidebarAppOption) {
|
||||
if (opt.id !== selectedAppId) onAppChange?.(opt.id);
|
||||
setInputValue(opt.name);
|
||||
setOpen(false);
|
||||
setEditing(false);
|
||||
}
|
||||
|
||||
function startEditing(e: React.MouseEvent) {
|
||||
e.stopPropagation();
|
||||
setEditing(true);
|
||||
setOpen(false);
|
||||
requestAnimationFrame(() => {
|
||||
inputRef.current?.focus();
|
||||
inputRef.current?.select();
|
||||
});
|
||||
}
|
||||
|
||||
function commitRename() {
|
||||
const trimmed = inputValue.trim();
|
||||
setEditing(false);
|
||||
if (!trimmed) {
|
||||
setInputValue(currentOption?.name ?? appName);
|
||||
return;
|
||||
}
|
||||
const matched = appOptions.find((o) => o.name === trimmed);
|
||||
if (matched) {
|
||||
if (matched.id !== selectedAppId) onAppChange?.(matched.id);
|
||||
return;
|
||||
}
|
||||
if (trimmed !== currentOption?.name) {
|
||||
onAppRename?.(trimmed);
|
||||
}
|
||||
}
|
||||
|
||||
function handleKeyDown(e: React.KeyboardEvent) {
|
||||
if (editing) {
|
||||
if (e.key === 'Enter') inputRef.current?.blur();
|
||||
if (e.key === 'Escape') {
|
||||
setInputValue(currentOption?.name ?? appName);
|
||||
setEditing(false);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!open) {
|
||||
if (e.key === 'Enter' || e.key === ' ' || e.key === 'ArrowDown') {
|
||||
e.preventDefault();
|
||||
openList();
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (e.key === 'ArrowDown') {
|
||||
e.preventDefault();
|
||||
setHighlight((h) => Math.min(h + 1, appOptions.length - 1));
|
||||
} else if (e.key === 'ArrowUp') {
|
||||
e.preventDefault();
|
||||
setHighlight((h) => Math.max(h - 1, 0));
|
||||
} else if (e.key === 'Enter') {
|
||||
e.preventDefault();
|
||||
pick(appOptions[highlight]);
|
||||
} else if (e.key === 'Escape') {
|
||||
setOpen(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="sidebar-app-combobox" ref={rootRef}>
|
||||
{editing ? (
|
||||
<input
|
||||
ref={inputRef}
|
||||
className="sidebar-app-input"
|
||||
value={inputValue}
|
||||
onChange={(e) => setInputValue(e.target.value)}
|
||||
onBlur={commitRename}
|
||||
onKeyDown={handleKeyDown}
|
||||
aria-label="Rename app"
|
||||
/>
|
||||
) : (
|
||||
<button
|
||||
type="button"
|
||||
className={'sidebar-app-trigger' + (open ? ' sidebar-app-trigger--open' : '')}
|
||||
onClick={() => (open ? setOpen(false) : openList())}
|
||||
onDoubleClick={startEditing}
|
||||
onKeyDown={handleKeyDown}
|
||||
aria-haspopup="listbox"
|
||||
aria-expanded={open}
|
||||
>
|
||||
<span className="sidebar-app-trigger-label">{currentOption?.name ?? appName}</span>
|
||||
<svg className="sidebar-app-trigger-caret" viewBox="0 0 24 24" width="14" height="14">
|
||||
<path d="M6 9l6 6 6-6" stroke="currentColor" strokeWidth="2" fill="none" />
|
||||
</svg>
|
||||
</button>
|
||||
)}
|
||||
|
||||
{open && (
|
||||
<ul className="sidebar-app-listbox" role="listbox">
|
||||
{appOptions.map((opt, i) => (
|
||||
<li
|
||||
key={opt.id}
|
||||
role="option"
|
||||
aria-selected={opt.id === selectedAppId}
|
||||
className={
|
||||
'sidebar-app-option' +
|
||||
(opt.id === selectedAppId ? ' sidebar-app-option--selected' : '') +
|
||||
(i === highlight ? ' sidebar-app-option--highlight' : '')
|
||||
}
|
||||
onMouseEnter={() => setHighlight(i)}
|
||||
onClick={() => pick(opt)}
|
||||
>
|
||||
{opt.name}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default function Sidebar({
|
||||
items,
|
||||
activeId,
|
||||
onSelect,
|
||||
appName,
|
||||
appOptions,
|
||||
selectedAppId,
|
||||
onAppChange,
|
||||
onAppRename,
|
||||
expanded,
|
||||
onToggle,
|
||||
}: {
|
||||
@@ -43,10 +215,15 @@ export default function Sidebar({
|
||||
activeId: string | null;
|
||||
onSelect: (id: string) => void;
|
||||
appName: string;
|
||||
appOptions?: SidebarAppOption[];
|
||||
selectedAppId?: string;
|
||||
onAppChange?: (id: string) => void;
|
||||
onAppRename?: (name: string) => void;
|
||||
expanded: boolean;
|
||||
onToggle: () => void;
|
||||
}) {
|
||||
const scrollRef = useRef<HTMLDivElement>(null);
|
||||
const hasOptions = appOptions && appOptions.length > 0;
|
||||
|
||||
const updateFade = () => {
|
||||
const el = scrollRef.current;
|
||||
@@ -71,7 +248,17 @@ export default function Sidebar({
|
||||
return (
|
||||
<nav className={'sidebar-rail' + (expanded ? ' sidebar-rail--expanded' : '')}>
|
||||
<div className="sidebar-header">
|
||||
<span className="sidebar-app-name">{appName}</span>
|
||||
{hasOptions ? (
|
||||
<AppCombobox
|
||||
appOptions={appOptions!}
|
||||
selectedAppId={selectedAppId}
|
||||
appName={appName}
|
||||
onAppChange={onAppChange}
|
||||
onAppRename={onAppRename}
|
||||
/>
|
||||
) : (
|
||||
<span className="sidebar-app-name">{appName}</span>
|
||||
)}
|
||||
<button
|
||||
className="sidebar-toggle"
|
||||
onClick={onToggle}
|
||||
|
||||
Reference in New Issue
Block a user