Skip to content

Commit de8b9ee

Browse files
committed
temp [ci skip]
1 parent 6236588 commit de8b9ee

11 files changed

Lines changed: 73 additions & 46 deletions

File tree

vscode/bus/src/callbacks.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ export type Callback = {
55
queryRequest: {
66
requestId: string
77
url: string
8-
params: Record<string, string>
9-
body: Record<string, any>
8+
method?: string
9+
params?: Record<string, string>
10+
body?: Record<string, any>
1011
}
1112
}
1213

@@ -29,6 +30,10 @@ export type VSCodeCallback = {
2930
changeFocusOnFile: {
3031
path: string
3132
}
33+
queryResponse: {
34+
requestId: string
35+
response: any
36+
}
3237
}
3338

3439
export type VSCodeEvent = {

vscode/react/src/api/instance.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { sendVSCodeMessage } from '@/hooks/vscode'
1+
import { sendVSCodeMessage } from '@/utils/vscodeapi'
22
import { isErr } from '@bus/result'
33

44
declare global {
@@ -42,7 +42,6 @@ export async function fetchAPI<T = any, B extends object = any>(
4242
// Create a promise that will resolve when we get a response with matching ID
4343
return new Promise((resolve, reject) => {
4444
const requestId = `query_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
45-
// Set up listener for the response
4645
const messageHandler = (event: MessageEvent) => {
4746
if (
4847
event.data &&
@@ -68,7 +67,7 @@ export async function fetchAPI<T = any, B extends object = any>(
6867
requestId,
6968
url: config.url,
7069
params: config.params as any,
71-
data: config.data,
70+
body: config.data,
7271
method: config.method,
7372
})
7473

vscode/react/src/utils/additional-components.ts renamed to vscode/react/src/components-additional/additional-components.ts

File renamed without changes.
File renamed without changes.

vscode/react/src/hooks/vscode.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,10 @@
1-
import { getVSCodeAPI } from '@/utilsutils/vscodeapi'
2-
import type { Callback } from '@bus/callbacks'
1+
import { sendVSCodeMessage } from '@/utils/vscodeapi'
32

43
/**
54
* use this hook to send messages to the vscode extension
65
*
76
* when deving the extension, we use an iframe to load the react app
87
* so we need to send messages to the parent window
98
*/
10-
export const useVSCode = (): (<K extends keyof Callback>(
11-
callbackName: K,
12-
payload: Callback[K],
13-
) => void) => {
14-
return sendVSCodeMessage
15-
}
9+
export const useVSCode = () => sendVSCodeMessage
1610

17-
export const sendVSCodeMessage = (
18-
callbackName: string,
19-
payload: any,
20-
): void => {
21-
const eventPayload = {
22-
key: callbackName,
23-
payload: payload,
24-
}
25-
if (window !== window.parent) {
26-
window.parent.postMessage(eventPayload, '*')
27-
} else {
28-
getVSCodeAPI().postMessage(eventPayload)
29-
}
30-
}

vscode/react/src/main.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { routeTree } from './routeTree.gen'
77

88
import './styles.css'
99
import reportWebVitals from './reportWebVitals.ts'
10-
import { EventBusProvider } from './utilsutils/eventBus.tsx'
10+
import { EventBusProvider } from './hooks/eventBus.tsx'
1111

1212
// Create a new router instance
1313
const router = createRouter({

vscode/react/src/pages/lineage.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ import { ModelLineage } from '@/components/graph/ModelLineage'
1010
import { useVSCode } from '@/hooks/vscode'
1111
import React from 'react'
1212
import { ModelSQLMeshModel } from '@/domain/sqlmesh-model'
13-
import { useEventBus } from '@/utilsutils/eventBus'
1413

1514
export function LineagePage() {
16-
const { emit } = useEventBus()
1715
// Handle messages from VSCode extension
1816
React.useEffect(() => {
1917
const handleMessage = (event: MessageEvent) => {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { VSCodeCallback } from "@bus/callbacks"
2+
3+
/**
4+
* add event listener to the window.
5+
*
6+
* returns a function to remove the event listener.
7+
*/
8+
export const addEventListener = <K extends keyof VSCodeCallback>(
9+
handlerKey: K,
10+
handler: (payload: VSCodeCallback[K]) => void,
11+
): (() => void) => {
12+
const handleMessage = (event: MessageEvent) => {
13+
const { key, payload } = event.data
14+
if (key === handlerKey) {
15+
handler(payload)
16+
}
17+
}
18+
window.addEventListener('message', handleMessage)
19+
return () => {
20+
window.removeEventListener('message', handleMessage)
21+
}
22+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import type { Callback } from "@bus/callbacks"
2+
3+
/**
4+
* send a message to the vscode extension.
5+
*
6+
* This should generally not be used directly, but rather through the useVSCode hook.
7+
*/
8+
export const sendVSCodeMessage = <K extends keyof Callback> (
9+
callbackName: K,
10+
payload: Callback[K],
11+
): void => {
12+
const eventPayload = {
13+
key: callbackName,
14+
payload: payload,
15+
}
16+
if (window !== window.parent) {
17+
window.parent.postMessage(eventPayload, '*')
18+
} else {
19+
getVSCodeAPI().postMessage(eventPayload)
20+
}
21+
}
22+
23+
let VSCODE_API: VSCodeAPI | undefined
24+
25+
interface VSCodeAPI {
26+
postMessage: (message: any) => void
27+
}
28+
29+
function getVSCodeAPI(): VSCodeAPI {
30+
if (!VSCODE_API) {
31+
// @ts-ignore
32+
VSCODE_API = acquireVsCodeApi()
33+
}
34+
if (!VSCODE_API) {
35+
throw new Error('VSCode API not initialized')
36+
}
37+
return VSCODE_API
38+
}
39+

0 commit comments

Comments
 (0)