mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-29 12:24:39 +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:
@@ -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>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user