-
Notifications
You must be signed in to change notification settings - Fork 2
/
editor.tsx
154 lines (137 loc) · 4.06 KB
/
editor.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { useMemo, useRef } from 'react'
import { EditorView, placeholder } from '@codemirror/view'
import { EditorState } from '@codemirror/state'
import { SQLConfig } from '@codemirror/lang-sql'
import { SQLEditor } from '@tidbcloud/tisqleditor-react'
import { saveHelper } from '@tidbcloud/codemirror-extension-save-helper'
import { bbedit, oneDark } from '@tidbcloud/codemirror-extension-themes'
import { curSqlGutter } from '@tidbcloud/codemirror-extension-cur-sql-gutter'
import {
useDbLinter,
fullWidthCharLinter
} from '@tidbcloud/codemirror-extension-linters'
import { sqlAutoCompletion } from '@tidbcloud/codemirror-extension-sql-autocomplete'
import {
aiWidget,
isUnifiedMergeViewActive
} from '@tidbcloud/codemirror-extension-ai-widget'
import { getCurDatabase } from '@tidbcloud/codemirror-extension-cur-sql'
import { useFilesContext } from '@/contexts/files-context'
import { useTheme } from '@/components/darkmode-toggle/theme-provider'
import { SchemaRes, useSchemaContext } from '@/contexts/schema-context'
import { useChatContext } from '@/contexts/chat-context'
function convertSchemaToSQLConfig(dbList: SchemaRes): SQLConfig {
const schema: any = {}
const tables: any[] = []
dbList.forEach((d) => {
const db = d.name
tables.push({
label: db,
type: 'database'
})
d.tables.forEach((t: any) => {
const table = t.name
tables.push({ label: table, type: 'table' })
const columns = t.columns.map((c: any) => ({
label: c.col,
type: c.data_type
}))
tables.push(...columns)
schema[`${db}.${table}`] = columns
schema[table] = columns
})
})
return { schema, tables }
}
export function Editor() {
const {
api: { saveFile },
state: { activeFileId, openedFiles }
} = useFilesContext()
const { isDark } = useTheme()
const { schema } = useSchemaContext()
const sqlConfig = useMemo(
() => convertSchemaToSQLConfig(schema ?? []),
[schema]
)
const getDbListRef = useRef<() => string[]>()
getDbListRef.current = () => {
return schema?.map((d) => d.name) || []
}
const chatCtx = useChatContext()
const activeFile = useMemo(
() => openedFiles.find((f) => f.id === activeFileId),
[activeFileId, openedFiles]
)
const extraExts = useMemo(() => {
if (activeFile && activeFile.status === 'loaded') {
return [
saveHelper({
save: (view: EditorView) => {
saveFile(activeFile.id, view.state.doc.toString())
}
}),
sqlAutoCompletion(),
curSqlGutter({
whenHide: (view) => {
return isUnifiedMergeViewActive(view.state)
}
}),
useDbLinter({
whenDisable: (_view) => {
return false
}
}),
fullWidthCharLinter(),
aiWidget({
chat(view, chatId, req) {
const db = getCurDatabase(view.state)
req['extra']['db'] = db
return chatCtx.chat(chatId, { ...req })
},
cancelChat: chatCtx.cancelChat,
onEvent(_view, type, payload) {
chatCtx.onEvent(type, payload)
},
getDbList: getDbListRef.current!
})
]
}
return []
}, [activeFile])
if (!activeFile) {
return (
<div className="h-full flex items-center justify-center">
<h1 className="scroll-m-20 text-4xl font-extrabold tracking-tight lg:text-5xl">
TiSQLEditor
</h1>
</div>
)
}
if (activeFile.status === 'loading') {
return (
<SQLEditor
className="h-full"
editorId="loading"
doc=""
theme={isDark ? oneDark : bbedit}
extraExts={[
placeholder('loading...'),
// both needed to prevent user from typing
EditorView.editable.of(false),
EditorState.readOnly.of(true)
]}
/>
)
}
return (
<SQLEditor
className="h-full"
editorId={activeFile.id}
doc={activeFile.content}
sqlConfig={sqlConfig}
theme={isDark ? oneDark : bbedit}
extraExts={extraExts}
/>
)
}