mirror of
https://github.com/NanmiCoder/cc-haha
synced 2026-07-19 13:33:35 +08:00
Sidebar project ordering, pinning, and hidden-project state must survive browser access to the same local server, so the UI now stores these preferences under the cc-haha config directory and keeps localStorage as a migration/cache fallback. Constraint: Browser and H5 localStorage is isolated from the Tauri WebView Rejected: Keep sidebar project preferences only in localStorage | browser sessions would not share state Rejected: Reuse cc-haha/settings.json | provider and H5 access settings should stay separate Confidence: high Scope-risk: moderate Directive: Keep sidebar hide/remove semantics non-destructive; do not delete transcript files for project removal Tested: cd desktop && bun run test -- src/components/layout/Sidebar.test.tsx --run Tested: cd desktop && bun run lint Tested: bun test src/server/__tests__/desktop-ui-preferences.test.ts Tested: bun run check:persistence-upgrade Tested: bun run check:server
59 lines
1.7 KiB
TypeScript
59 lines
1.7 KiB
TypeScript
/**
|
|
* Desktop UI Preferences REST API
|
|
*
|
|
* GET /api/desktop-ui/preferences — read cc-haha UI preferences
|
|
* PUT /api/desktop-ui/preferences/sidebar — persist sidebar project preferences
|
|
*/
|
|
|
|
import { ApiError, errorResponse } from '../middleware/errorHandler.js'
|
|
import { DesktopUiPreferencesService } from '../services/desktopUiPreferencesService.js'
|
|
|
|
const desktopUiPreferencesService = new DesktopUiPreferencesService()
|
|
|
|
export async function handleDesktopUiApi(
|
|
req: Request,
|
|
url: URL,
|
|
segments: string[],
|
|
): Promise<Response> {
|
|
void url
|
|
|
|
try {
|
|
const sub = segments[2]
|
|
const detail = segments[3]
|
|
|
|
if (sub !== 'preferences') {
|
|
throw ApiError.notFound(`Unknown desktop UI endpoint: ${sub}`)
|
|
}
|
|
|
|
if (detail === undefined) {
|
|
if (req.method !== 'GET') throw methodNotAllowed(req.method)
|
|
return Response.json(await desktopUiPreferencesService.readPreferences())
|
|
}
|
|
|
|
if (detail === 'sidebar') {
|
|
if (req.method !== 'PUT') throw methodNotAllowed(req.method)
|
|
const body = await parseJsonBody(req)
|
|
return Response.json({
|
|
ok: true,
|
|
preferences: await desktopUiPreferencesService.updateSidebarPreferences(body),
|
|
})
|
|
}
|
|
|
|
throw ApiError.notFound(`Unknown desktop UI preferences endpoint: ${detail}`)
|
|
} catch (error) {
|
|
return errorResponse(error)
|
|
}
|
|
}
|
|
|
|
async function parseJsonBody(req: Request): Promise<Record<string, unknown>> {
|
|
try {
|
|
return (await req.json()) as Record<string, unknown>
|
|
} catch {
|
|
throw ApiError.badRequest('Invalid JSON body')
|
|
}
|
|
}
|
|
|
|
function methodNotAllowed(method: string): ApiError {
|
|
return new ApiError(405, `Method ${method} not allowed`, 'METHOD_NOT_ALLOWED')
|
|
}
|