cc-haha/desktop/src/api/openTargets.ts
程序员阿江(Relakkes) c03cb1f297 Resolve opener icon URLs through the desktop server
Packaged Tauri builds load the React app from a WebView origin while the sidecar API runs on a dynamic localhost port. The opener menu was rendering relative image paths such as /api/open-targets/icons/vscode, so the image request never reached the sidecar and every target fell back to the generic icon.

The desktop API layer now normalizes returned iconUrl values through the configured API base URL, matching how JSON requests already reach the sidecar. This keeps the React menu platform-agnostic while making packaged app icons load from the live server.

Constraint: Tauri production builds do not share the sidecar API origin for normal image src attributes.

Rejected: Build absolute URLs on the server | the server does not know the final browser-visible base URL in every H5/Tauri startup mode.

Confidence: high

Scope-risk: narrow

Directive: Any future API-provided browser asset URL should be normalized through the configured API base URL before assigning it to DOM src/href fields.

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

Tested: cd desktop && bun run lint
2026-05-13 18:29:56 +08:00

46 lines
994 B
TypeScript

import { api, getApiUrl } from './client'
export type OpenTargetKind = 'ide' | 'file_manager'
export type OpenTarget = {
id: string
kind: OpenTargetKind
label: string
icon: string
iconUrl?: string
platform: string
}
export type OpenTargetList = {
platform: string
targets: OpenTarget[]
primaryTargetId: string | null
cachedAt: number
ttlMs: number
}
export type OpenTargetOpenResponse = {
ok: true
targetId: string
path: string
}
function normalizeOpenTargetList(result: OpenTargetList): OpenTargetList {
return {
...result,
targets: result.targets.map((target) => ({
...target,
iconUrl: target.iconUrl ? getApiUrl(target.iconUrl) : undefined,
})),
}
}
export const openTargetsApi = {
async list() {
return normalizeOpenTargetList(await api.get<OpenTargetList>('/api/open-targets'))
},
open(targetId: string, path: string) {
return api.post<OpenTargetOpenResponse>('/api/open-targets/open', { targetId, path })
},
}