cc-haha/src/server/api/open-targets.ts
程序员阿江(Relakkes) 1182c5d9c3 Use native app icons for project opener
The first project opener UI used generic code glyphs for every IDE, which made the menu visually noisy and did not match the native desktop expectation. This changes the macOS path to expose transparent PNG icons generated from each detected local .app bundle and renders them directly in the toolbar/menu, falling back to the existing glyph only when an icon cannot be loaded.

Constraint: Do not redistribute third-party IDE trademark assets in the app bundle.

Constraint: Keep icon detection local to already detected open targets and cache only runtime PNG results.

Rejected: Bundling downloaded IDE logos | creates asset maintenance and trademark redistribution risk.

Rejected: Adding simple-icons as a dependency | it is not a faithful desktop app icon source and still requires brand permission checks.

Confidence: high

Scope-risk: narrow

Directive: Prefer local bundle icons on macOS; add curated official assets only as a cross-platform fallback layer.

Tested: bun test src/server/__tests__/open-target-service.test.ts src/server/__tests__/open-target-api.test.ts

Tested: cd desktop && bun run test -- src/stores/openTargetStore.test.ts src/components/layout/OpenProjectMenu.test.tsx src/components/layout/TabBar.test.tsx

Tested: cd desktop && bun run lint

Tested: bun -e openTargetService.getTargetIcon for vscode and finder returned PNG data

Tested: cd desktop && bun run build
2026-05-13 18:09:35 +08:00

73 lines
2.2 KiB
TypeScript

import { openTargetService } from '../services/openTargetService.js'
import { ApiError, errorResponse } from '../middleware/errorHandler.js'
export async function handleOpenTargetsApi(
req: Request,
url: URL,
segments: string[],
): Promise<Response> {
try {
const action = segments[2]
if (!action) {
if (req.method !== 'GET') {
throw new ApiError(405, `Method ${req.method} not allowed`, 'METHOD_NOT_ALLOWED')
}
return Response.json(await openTargetService.listTargets())
}
if (action === 'open') {
if (req.method !== 'POST') {
throw new ApiError(405, `Method ${req.method} not allowed`, 'METHOD_NOT_ALLOWED')
}
const body = await parseJsonBody(req)
const targetId = typeof body.targetId === 'string' ? body.targetId.trim() : ''
const path = typeof body.path === 'string' ? body.path : ''
if (!targetId) {
throw ApiError.badRequest('Missing or invalid "targetId" in request body')
}
if (!path || !path.trim()) {
throw ApiError.badRequest('Missing or invalid "path" in request body')
}
return Response.json(await openTargetService.openTarget({ targetId, path }))
}
if (action === 'icons') {
if (req.method !== 'GET') {
throw new ApiError(405, `Method ${req.method} not allowed`, 'METHOD_NOT_ALLOWED')
}
const targetId = typeof segments[3] === 'string' ? decodeURIComponent(segments[3]).trim() : ''
if (!targetId) {
throw ApiError.badRequest('Missing open target icon id')
}
const icon = await openTargetService.getTargetIcon(targetId)
return new Response(icon.data, {
headers: {
'Cache-Control': 'private, max-age=86400',
'Content-Type': icon.contentType,
},
})
}
throw ApiError.notFound(`Unknown open-targets endpoint: ${action}`)
} catch (error) {
return errorResponse(error)
}
}
async function parseJsonBody(req: Request): Promise<Record<string, unknown>> {
try {
const body = await req.json()
return body && typeof body === 'object' ? body as Record<string, unknown> : {}
} catch {
throw ApiError.badRequest('Invalid JSON body')
}
}