|
6 | 6 | type EnvironmentId, |
7 | 7 | type ResolvedKeybindingsConfig, |
8 | 8 | } from "@t3tools/contracts"; |
9 | | -import { memo, useCallback, useEffect, useMemo, useRef } from "react"; |
| 9 | +import { memo, useEffect, useRef } from "react"; |
10 | 10 | import { isOpenFavoriteEditorShortcut, shortcutLabelForCommand } from "../../keybindings"; |
11 | 11 | import { usePreferredEditor } from "../../editorPreferences"; |
12 | 12 | import { editorLabelForPlatform } from "../../editorLabels"; |
@@ -203,6 +203,13 @@ function getOpenInIconClass(kind: OpenInOption["kind"]) { |
203 | 203 | return cn(kind === "brand" ? "text-foreground opacity-100" : "text-muted-foreground"); |
204 | 204 | } |
205 | 205 |
|
| 206 | +// Resolving filters and labels every known editor; the compiler would redo |
| 207 | +// it every render because the open command hooks follow it. |
| 208 | +function useOpenInOptions(editors: ReadonlyArray<EditorId>) { |
| 209 | + "use memo"; |
| 210 | + return resolveOpenInOptions(navigator.platform, editors); |
| 211 | +} |
| 212 | + |
206 | 213 | export const OpenInPicker = memo(function OpenInPicker({ |
207 | 214 | environmentId, |
208 | 215 | keybindings, |
@@ -238,59 +245,45 @@ export const OpenInPicker = memo(function OpenInPicker({ |
238 | 245 | // the viewing machine, which only the desktop app can probe. |
239 | 246 | const effectiveEditors = remote.mode === "local-exec" ? availableEditors : remoteCapableEditors; |
240 | 247 | const [preferredEditor, setPreferredEditor] = usePreferredEditor(effectiveEditors); |
241 | | - const options = useMemo( |
242 | | - () => resolveOpenInOptions(navigator.platform, effectiveEditors), |
243 | | - [effectiveEditors], |
244 | | - ); |
| 248 | + const options = useOpenInOptions(effectiveEditors); |
245 | 249 | const primaryOption = options.find(({ value }) => value === preferredEditor) ?? null; |
246 | 250 |
|
247 | | - const openInEditor = useCallback( |
248 | | - (editorId: EditorId | null) => { |
249 | | - if (!openInCwd) return; |
250 | | - const editor = editorId ?? preferredEditor; |
251 | | - if (!editor) return; |
252 | | - if (remote.mode === "remote-unavailable") return; |
253 | | - if (remote.mode === "remote-links") { |
254 | | - const url = buildRemoteOpenUrl({ |
255 | | - editor, |
256 | | - host: remote.host.host, |
257 | | - absolutePath: openInCwd, |
258 | | - }); |
259 | | - if (url === undefined) return; |
260 | | - // Only record hint-seen/preferred when the shell actually accepted |
261 | | - // the URL (an older desktop build can refuse the editor scheme). |
262 | | - void openRemoteEditorUrl(url).then((opened) => { |
263 | | - if (!opened) return; |
264 | | - markRemoteHintSeen(); |
265 | | - setPreferredEditor(editor); |
266 | | - }); |
267 | | - return; |
268 | | - } |
269 | | - if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) return; |
270 | | - const result = openInEditorMutation({ |
271 | | - environmentId, |
272 | | - input: { |
273 | | - cwd: openInCwd, |
274 | | - editor, |
275 | | - }, |
| 251 | + const openInEditor = (editorId: EditorId | null) => { |
| 252 | + if (!openInCwd) return; |
| 253 | + const editor = editorId ?? preferredEditor; |
| 254 | + if (!editor) return; |
| 255 | + if (remote.mode === "remote-unavailable") return; |
| 256 | + if (remote.mode === "remote-links") { |
| 257 | + const url = buildRemoteOpenUrl({ |
| 258 | + editor, |
| 259 | + host: remote.host.host, |
| 260 | + absolutePath: openInCwd, |
276 | 261 | }); |
277 | | - setPreferredEditor(editor); |
278 | | - return result; |
279 | | - }, |
280 | | - [ |
| 262 | + if (url === undefined) return; |
| 263 | + // Only record hint-seen/preferred when the shell actually accepted |
| 264 | + // the URL (an older desktop build can refuse the editor scheme). |
| 265 | + void openRemoteEditorUrl(url).then((opened) => { |
| 266 | + if (!opened) return; |
| 267 | + markRemoteHintSeen(); |
| 268 | + setPreferredEditor(editor); |
| 269 | + }); |
| 270 | + return; |
| 271 | + } |
| 272 | + if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) return; |
| 273 | + const result = openInEditorMutation({ |
281 | 274 | environmentId, |
282 | | - markRemoteHintSeen, |
283 | | - openInCwd, |
284 | | - openInEditorMutation, |
285 | | - preferredEditor, |
286 | | - remote, |
287 | | - setPreferredEditor, |
288 | | - ], |
289 | | - ); |
| 275 | + input: { |
| 276 | + cwd: openInCwd, |
| 277 | + editor, |
| 278 | + }, |
| 279 | + }); |
| 280 | + setPreferredEditor(editor); |
| 281 | + return result; |
| 282 | + }; |
290 | 283 |
|
291 | | - const openFavoriteEditorShortcutLabel = useMemo( |
292 | | - () => shortcutLabelForCommand(keybindings, "editor.openFavorite"), |
293 | | - [keybindings], |
| 284 | + const openFavoriteEditorShortcutLabel = shortcutLabelForCommand( |
| 285 | + keybindings, |
| 286 | + "editor.openFavorite", |
294 | 287 | ); |
295 | 288 |
|
296 | 289 | useEffect(() => { |
|
0 commit comments