mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-29 04:14:30 +00:00
feat(settings): add team appearance setting to hide agent features (#411)
* feat(settings): add team appearance setting to hide agent features Add an Appearance subpage under Team Settings allowing team owners to toggle appearance.hideAgent for the team. Return appearance.hideAgent in the user-metadata API, and conditionally hide the AGENTS.md button, agent chatbot sidebar, and chatbot trigger button in the WebUI. * refactor(settings): add strongly typed TeamSettingsResponse to remove explicit any Introduce TeamSettingsResponse and TeamAppearanceSettings in @shumai/dtos, type TeamService.getSettings and updateSettings, and eliminate the explicit any cast in AppearanceSettings.tsx.
This commit is contained in:
@@ -5,6 +5,7 @@ import { ResizeHandle } from '@/ui/components/resize-handle'
|
||||
import { useChatbotStore } from '@/ui/stores/chatbot'
|
||||
import { useTopNavStore } from '@/ui/stores/top-nav'
|
||||
import { useUiStore } from '@/ui/stores/ui'
|
||||
import { useUserMetadataStore } from '@/ui/stores/user-metadata'
|
||||
import type { AncestorFolder, AssetInfo } from '@shumai/dtos'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { useNavigate } from '@tanstack/react-router'
|
||||
@@ -36,6 +37,7 @@ export default function AgentsMdManager({
|
||||
setFileListLeftSidebarCollapsed: setIsLeftSidebarCollapsed,
|
||||
} = useUiStore()
|
||||
const { isChatbotOpen, setIsChatbotOpen } = useChatbotStore()
|
||||
const hideAgent = useUserMetadataStore((s) => Boolean(s.metadata['appearance.hideAgent']))
|
||||
|
||||
const { data: folderInfo } = useQuery({
|
||||
queryKey: ['folders', assetId],
|
||||
@@ -136,7 +138,7 @@ export default function AgentsMdManager({
|
||||
/>
|
||||
</div>
|
||||
|
||||
{isChatbotOpen && (
|
||||
{isChatbotOpen && !hideAgent && (
|
||||
<>
|
||||
<ResizeHandle
|
||||
onResize={(delta) => {
|
||||
|
||||
@@ -123,4 +123,25 @@ describe('BreadcrumbNav component', () => {
|
||||
|
||||
expect(screen.getByRole('button', { name: /Show omitted folders/i })).toBeDefined()
|
||||
})
|
||||
|
||||
it('renders chatbot toggle button when onChatbotToggle is provided', () => {
|
||||
const onChatbotToggle = vi.fn()
|
||||
const { container } = render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<BreadcrumbNav {...baseProps} onChatbotToggle={onChatbotToggle} />
|
||||
</QueryClientProvider>,
|
||||
)
|
||||
|
||||
expect(container.querySelector('.lucide-bot')).not.toBeNull()
|
||||
})
|
||||
|
||||
it('does not render chatbot toggle button when onChatbotToggle is undefined', () => {
|
||||
const { container } = render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<BreadcrumbNav {...baseProps} onChatbotToggle={undefined} />
|
||||
</QueryClientProvider>,
|
||||
)
|
||||
|
||||
expect(container.querySelector('.lucide-bot')).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -19,6 +19,7 @@ import { Button } from '../ui/button'
|
||||
import { Popover, PopoverContent, PopoverTrigger } from '../ui/popover'
|
||||
import { Separator } from '../ui/separator'
|
||||
import { cn } from '@/ui/lib/utils'
|
||||
import { useUserMetadataStore } from '@/ui/stores/user-metadata'
|
||||
|
||||
type FileBrowserToolbarProps = {
|
||||
teamId: string
|
||||
@@ -63,6 +64,7 @@ export function FileBrowserToolbar({
|
||||
const activeFiltersCount = filterConditions.length
|
||||
const isCollection = !!collection
|
||||
const queryClient = useQueryClient()
|
||||
const hideAgent = useUserMetadataStore((s) => Boolean(s.metadata['appearance.hideAgent']))
|
||||
|
||||
const { data: folderInfo } = useQuery({
|
||||
queryKey: ['folders', assetId],
|
||||
@@ -365,7 +367,7 @@ export function FileBrowserToolbar({
|
||||
)}
|
||||
</Button>
|
||||
|
||||
{!isRecentlyDeleted && !isRecents && (
|
||||
{!isRecentlyDeleted && !isRecents && !hideAgent && (
|
||||
<>
|
||||
<Separator orientation="vertical" />
|
||||
<Button
|
||||
|
||||
@@ -101,6 +101,7 @@ export default function FileSystemManager({
|
||||
|
||||
const { members, fetchMembers } = useMemberStore()
|
||||
const { metadata, fetchMetadata, setMetadata: setUserMetadata } = useUserMetadataStore()
|
||||
const hideAgent = Boolean(metadata['appearance.hideAgent'])
|
||||
|
||||
useEffect(() => {
|
||||
if (teamId) {
|
||||
@@ -570,7 +571,7 @@ export default function FileSystemManager({
|
||||
rootFolderId={rootFolderId}
|
||||
/>
|
||||
|
||||
{(!isRightSidebarCollapsed || isChatbotOpen) && (
|
||||
{(!isRightSidebarCollapsed || (isChatbotOpen && !hideAgent)) && (
|
||||
<>
|
||||
<ResizeHandle
|
||||
onResize={(delta) => {
|
||||
@@ -582,7 +583,7 @@ export default function FileSystemManager({
|
||||
style={{ width: rightSidebarWidth }}
|
||||
className="bg-background flex flex-col flex-shrink-0"
|
||||
>
|
||||
{isChatbotOpen ? (
|
||||
{isChatbotOpen && !hideAgent ? (
|
||||
<ChatbotSidebar projectId={projectId} contextAssetId={assetId} />
|
||||
) : singleSelectedFile ? (
|
||||
<FileViewerRightSidebar
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { cleanup, render, screen, fireEvent, waitFor } from '@testing-library/react'
|
||||
import React from 'react'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { AppearanceSettings } from './AppearanceSettings'
|
||||
import { useUserMetadataStore } from '@/ui/stores/user-metadata'
|
||||
import { useChatbotStore } from '@/ui/stores/chatbot'
|
||||
import { toast } from 'sonner'
|
||||
|
||||
const mockGetSettings = vi.fn()
|
||||
const mockPatchSettings = vi.fn()
|
||||
|
||||
vi.mock('@/ui/api/client', () => ({
|
||||
client: {
|
||||
api: {
|
||||
teams: {
|
||||
':teamId': {
|
||||
settings: {
|
||||
$get: () => mockGetSettings(),
|
||||
$patch: (args: unknown) => mockPatchSettings(args),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('sonner', () => ({
|
||||
toast: {
|
||||
success: vi.fn(),
|
||||
error: vi.fn(),
|
||||
},
|
||||
}))
|
||||
|
||||
describe('AppearanceSettings', () => {
|
||||
let queryClient: QueryClient
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: { retry: false },
|
||||
},
|
||||
})
|
||||
mockGetSettings.mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
appearance: { hideAgent: false },
|
||||
}),
|
||||
})
|
||||
mockPatchSettings.mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
appearance: { hideAgent: true },
|
||||
}),
|
||||
})
|
||||
vi.spyOn(useUserMetadataStore.getState(), 'fetchMetadata').mockResolvedValue()
|
||||
useChatbotStore.setState({ isChatbotOpen: true })
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
})
|
||||
|
||||
const renderComponent = () =>
|
||||
render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<AppearanceSettings teamId="team-123" />
|
||||
</QueryClientProvider>,
|
||||
)
|
||||
|
||||
it('renders switch with initial unchecked state when hideAgent is false', async () => {
|
||||
renderComponent()
|
||||
|
||||
await waitFor(() => {
|
||||
const switchElement = screen.getByRole('switch')
|
||||
expect(switchElement).toBeDefined()
|
||||
expect(switchElement.getAttribute('data-state')).toBe('unchecked')
|
||||
})
|
||||
})
|
||||
|
||||
it('renders switch checked when hideAgent is true', async () => {
|
||||
mockGetSettings.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
appearance: { hideAgent: true },
|
||||
}),
|
||||
})
|
||||
|
||||
renderComponent()
|
||||
|
||||
await waitFor(() => {
|
||||
const switchElement = screen.getByRole('switch')
|
||||
expect(switchElement).toBeDefined()
|
||||
expect(switchElement.getAttribute('data-state')).toBe('checked')
|
||||
})
|
||||
})
|
||||
|
||||
it('updates settings, closes chatbot, and refreshes user metadata on toggle', async () => {
|
||||
renderComponent()
|
||||
|
||||
const switchElement = await screen.findByRole('switch')
|
||||
fireEvent.click(switchElement)
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockPatchSettings).toHaveBeenCalledWith({
|
||||
param: { teamId: 'team-123' },
|
||||
json: {
|
||||
key: 'appearance.hideAgent',
|
||||
value: true,
|
||||
},
|
||||
})
|
||||
expect(useUserMetadataStore.getState().fetchMetadata).toHaveBeenCalledWith('team-123')
|
||||
expect(useChatbotStore.getState().isChatbotOpen).toBe(false)
|
||||
expect(toast.success).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
it('shows error toast when patch fails', async () => {
|
||||
mockPatchSettings.mockResolvedValueOnce({
|
||||
ok: false,
|
||||
json: async () => ({ message: 'Forbidden' }),
|
||||
})
|
||||
|
||||
renderComponent()
|
||||
|
||||
const switchElement = await screen.findByRole('switch')
|
||||
fireEvent.click(switchElement)
|
||||
|
||||
await waitFor(() => {
|
||||
expect(toast.error).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,99 @@
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import type { TeamSettingsResponse } from '@shumai/dtos'
|
||||
import { client } from '@/ui/api/client'
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/ui/components/ui/card'
|
||||
import { Switch } from '@/ui/components/ui/switch'
|
||||
import { Loader2 } from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
import { m } from '@/ui/paraglide/messages.js'
|
||||
import { useUserMetadataStore } from '@/ui/stores/user-metadata'
|
||||
import { useChatbotStore } from '@/ui/stores/chatbot'
|
||||
|
||||
interface AppearanceSettingsProps {
|
||||
teamId: string
|
||||
}
|
||||
|
||||
export function AppearanceSettings({ teamId }: AppearanceSettingsProps) {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
const { data: settings, isLoading } = useQuery<TeamSettingsResponse>({
|
||||
queryKey: ['teams', teamId, 'settings'],
|
||||
queryFn: async () => {
|
||||
const res = await client.api.teams[':teamId'].settings.$get({
|
||||
param: { teamId },
|
||||
})
|
||||
if (!res.ok) throw new Error(m.failed_load_settings())
|
||||
return (await res.json()) as TeamSettingsResponse
|
||||
},
|
||||
enabled: !!teamId,
|
||||
})
|
||||
|
||||
const { mutate: updateSettings, isPending: isUpdating } = useMutation({
|
||||
mutationFn: async (hideAgent: boolean) => {
|
||||
const res = await client.api.teams[':teamId'].settings.$patch({
|
||||
param: { teamId },
|
||||
json: {
|
||||
key: 'appearance.hideAgent',
|
||||
value: hideAgent,
|
||||
},
|
||||
})
|
||||
if (!res.ok) throw new Error(m.failed_update_appearance_settings())
|
||||
return await res.json()
|
||||
},
|
||||
onSuccess: async (_, hideAgent) => {
|
||||
queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'settings'] })
|
||||
await useUserMetadataStore.getState().fetchMetadata(teamId)
|
||||
if (hideAgent) {
|
||||
useChatbotStore.getState().setIsChatbotOpen(false)
|
||||
}
|
||||
toast.success(m.appearance_settings_updated())
|
||||
},
|
||||
onError: (err) => {
|
||||
toast.error(err instanceof Error ? err.message : m.failed_update_appearance_settings())
|
||||
},
|
||||
})
|
||||
|
||||
const hideAgent = Boolean(settings?.appearance?.hideAgent)
|
||||
|
||||
const handleToggleHideAgent = (checked: boolean) => {
|
||||
updateSettings(checked)
|
||||
}
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="flex items-center justify-center p-8">
|
||||
<Loader2 className="h-6 w-6 animate-spin text-muted-foreground" />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="h-full overflow-y-auto pr-1">
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>{m.appearance_settings()}</CardTitle>
|
||||
<CardDescription>{m.appearance_settings_description()}</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-6">
|
||||
<div className="flex items-center justify-between space-x-4 rounded-lg border border-border p-4">
|
||||
<div className="space-y-1">
|
||||
<label
|
||||
htmlFor="hide-agent-switch"
|
||||
className="text-sm font-medium leading-none cursor-pointer"
|
||||
>
|
||||
{m.hide_agent()}
|
||||
</label>
|
||||
<p className="text-xs text-muted-foreground">{m.hide_agent_description()}</p>
|
||||
</div>
|
||||
<Switch
|
||||
id="hide-agent-switch"
|
||||
checked={hideAgent}
|
||||
onCheckedChange={handleToggleHideAgent}
|
||||
disabled={isUpdating}
|
||||
/>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -3,12 +3,14 @@ import { BreadcrumbNav } from './breadcrumb-nav'
|
||||
import { ShumaiLogo } from '@/ui/components/ui/icons'
|
||||
import { useUiStore } from '@/ui/stores/ui'
|
||||
import { useChatbotStore } from '@/ui/stores/chatbot'
|
||||
import { useUserMetadataStore } from '@/ui/stores/user-metadata'
|
||||
import { useIsMobile } from '@/ui/hooks/use-mobile'
|
||||
|
||||
export function TopNav() {
|
||||
const { projectState } = useTopNavStore()
|
||||
const uiStore = useUiStore()
|
||||
const { isChatbotOpen, setIsChatbotOpen } = useChatbotStore()
|
||||
const hideAgent = useUserMetadataStore((s) => Boolean(s.metadata['appearance.hideAgent']))
|
||||
const isMobile = useIsMobile()
|
||||
|
||||
if (isMobile && projectState?.fileId) {
|
||||
@@ -72,7 +74,7 @@ export function TopNav() {
|
||||
isPublic && projectState.onRightSidebarToggle
|
||||
? projectState.onRightSidebarToggle
|
||||
: () => {
|
||||
if (!isPublic && isChatbotOpen) {
|
||||
if (!isPublic && isChatbotOpen && !hideAgent) {
|
||||
setIsChatbotOpen(false)
|
||||
if (isFileView) {
|
||||
uiStore.setFileViewRightSidebarCollapsed(false)
|
||||
@@ -92,11 +94,12 @@ export function TopNav() {
|
||||
}
|
||||
}
|
||||
|
||||
const onChatbotToggle = isPublic
|
||||
? undefined
|
||||
: () => {
|
||||
setIsChatbotOpen(!isChatbotOpen)
|
||||
}
|
||||
const onChatbotToggle =
|
||||
isPublic || hideAgent
|
||||
? undefined
|
||||
: () => {
|
||||
setIsChatbotOpen(!isChatbotOpen)
|
||||
}
|
||||
|
||||
const displayStyle = projectId ? (uiStore.viewModes[projectId] ?? 'card') : 'card'
|
||||
const onDisplayStyleChange = isPublic
|
||||
@@ -122,8 +125,8 @@ export function TopNav() {
|
||||
onLeftSidebarToggle={onLeftSidebarToggle}
|
||||
isRightSidebarCollapsed={isRightSidebarCollapsed}
|
||||
onRightSidebarToggle={onRightSidebarToggle}
|
||||
isChatbotOpen={!isPublic && isChatbotOpen}
|
||||
onChatbotToggle={onChatbotToggle}
|
||||
isChatbotOpen={!isPublic && !hideAgent && isChatbotOpen}
|
||||
onChatbotToggle={hideAgent ? undefined : onChatbotToggle}
|
||||
isPublic={isPublic}
|
||||
shareId={shareId}
|
||||
allowDownload={allowDownload}
|
||||
|
||||
Reference in New Issue
Block a user