Files
opencode-manager/frontend/src/components/settings/AgentsMdEditor.tsx
T
Chris Scott 5bd688de4b feat(themes): apply OpenCode color themes app-wide with a settings picker (#364)
- Vendor the 36 OpenCode theme palettes into the shared package, synced
  from the pinned OpenCode release tag via pnpm themes:sync.
- Add a colorTheme user preference across the settings schema, defaults,
  and the assistant internal API.
- Extend palettes with success, warning, info, and diff colors, and add a
  resolver that maps palettes to CSS tokens and applies them to the DOM.
- Apply the theme app-wide through a ThemeProvider context, adopting the
  tokens in message, repo, session, and settings components, agent
  colors, git status styles, and diff viewers.
- Add a theme picker to General settings and make model quick-select
  follow the active theme (fixes #355).
2026-09-28 00:19:32 -04:00

163 lines
5.4 KiB
TypeScript

import { useState, useEffect, useRef } from 'react'
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import { Loader2, Save, RotateCcw } from 'lucide-react'
import { Button } from '@/components/ui/button'
import { CodeEditor } from '@/components/ui/code-editor'
import { EditorFindBar } from '@/components/ui/editor-find-bar'
import { useFindInText } from '@/lib/useFindInText'
import { settingsApi } from '@/api/settings'
import { useOpenCodeApplyFeedback } from '@/hooks/useOpenCodeApplyFeedback'
import { showToast } from '@/lib/toast'
export function AgentsMdEditor() {
const queryClient = useQueryClient()
const applyOpenCodeSaveFeedback = useOpenCodeApplyFeedback()
const [content, setContent] = useState('')
const [savedContent, setSavedContent] = useState('')
const hasChanges = content !== savedContent
const hasChangesRef = useRef(false)
hasChangesRef.current = hasChanges
const savedContentRef = useRef('')
savedContentRef.current = savedContent
const { data, isLoading, error } = useQuery({
queryKey: ['agents-md'],
queryFn: () => settingsApi.getAgentsMd(),
})
useEffect(() => {
if (data?.content === undefined) return
if (hasChangesRef.current) return
if (data.content === savedContentRef.current) return
setContent(data.content)
setSavedContent(data.content)
}, [data?.content])
const updateMutation = useMutation({
mutationFn: (newContent: string) => settingsApi.updateAgentsMd(newContent),
onSuccess: (result, newContent) => {
setSavedContent(newContent)
queryClient.invalidateQueries({ queryKey: ['agents-md'] })
applyOpenCodeSaveFeedback({ appliedMessage: 'AGENTS.md saved', restartRequired: result.restartRequired })
},
onError: () => {
showToast.error('Failed to save AGENTS.md')
},
})
const resetToDefaultMutation = useMutation({
mutationFn: async () => {
const { content: defaultContent } = await settingsApi.getDefaultAgentsMd()
const result = await settingsApi.updateAgentsMd(defaultContent)
return { defaultContent, restartRequired: result.restartRequired }
},
onSuccess: ({ defaultContent, restartRequired }) => {
queryClient.invalidateQueries({ queryKey: ['agents-md'] })
setContent(defaultContent)
setSavedContent(defaultContent)
applyOpenCodeSaveFeedback({ appliedMessage: 'AGENTS.md reset to default', restartRequired })
},
onError: () => {
showToast.error('Failed to reset AGENTS.md')
},
})
const isSaving = updateMutation.isPending || resetToDefaultMutation.isPending
const handleSave = () => {
updateMutation.mutate(content)
}
const handleResetToDefault = () => {
resetToDefaultMutation.mutate()
}
const { query, setQuery, matches, currentMatchIndex, hasMatches, next, prev } = useFindInText(content)
if (isLoading) {
return (
<div className="flex items-center justify-center py-8">
<Loader2 className="h-6 w-6 animate-spin text-muted-foreground" />
</div>
)
}
if (error) {
return (
<div className="text-center py-8 text-destructive">
Failed to load AGENTS.md
</div>
)
}
return (
<div className="space-y-4">
<div className="sticky top-0 z-10 flex flex-col gap-3 bg-background py-2 sm:flex-row sm:items-center sm:justify-between">
<div>
<p className="text-sm text-muted-foreground">
Global instructions for AI agents. This file is merged with repository-specific AGENTS.md files.
</p>
</div>
<div className="flex flex-shrink-0 gap-2">
<Button
variant="outline"
size="sm"
onClick={handleResetToDefault}
disabled={isSaving}
className="flex-1 sm:flex-none"
>
{resetToDefaultMutation.isPending ? (
<Loader2 className="h-4 w-4 mr-1 animate-spin" />
) : (
<RotateCcw className="h-4 w-4 mr-1" />
)}
Reset to Default
</Button>
<Button
size="sm"
onClick={handleSave}
disabled={!hasChanges || isSaving}
className="flex-1 sm:flex-none"
>
{updateMutation.isPending ? (
<Loader2 className="h-4 w-4 mr-1 animate-spin" />
) : (
<Save className="h-4 w-4 mr-1" />
)}
Save
</Button>
</div>
</div>
<div className="overflow-hidden rounded-md border border-input">
<EditorFindBar
query={query}
onQueryChange={setQuery}
matchCount={matches.length}
currentMatch={hasMatches ? currentMatchIndex + 1 : 0}
onPrev={prev}
onNext={next}
inputName="agents-md-find"
placeholder="Find in AGENTS.md..."
/>
<div className="h-[60dvh] min-h-[320px] sm:h-[55vh]">
<CodeEditor
ariaLabel="AGENTS.md content"
value={content}
onChange={setContent}
highlights={matches}
activeHighlightIndex={currentMatchIndex}
disabled={isSaving}
placeholder="# Agent Instructions&#10;&#10;Add global instructions for AI agents here..."
/>
</div>
</div>
{hasChanges && (
<p className="text-xs text-warning">You have unsaved changes</p>
)}
</div>
)
}