|
4 | 4 | DataSourceTable,
|
5 | 5 | } from '@briefer/types'
|
6 | 6 | import { Map, Set, List } from 'immutable'
|
7 |
| -import { distance as levenshtein } from 'fastest-levenshtein' |
8 | 7 | import { useCallback, useEffect, useMemo, useState } from 'react'
|
9 | 8 |
|
10 | 9 | export type SchemaItem =
|
@@ -172,19 +171,57 @@ function useSchemaList(schemas: Map<string, DataSourceSchema>): UseSchemaList {
|
172 | 171 | tableName: string
|
173 | 172 | column: DataSourceColumn
|
174 | 173 | }[] = []
|
175 |
| - schemas.forEach((schema, schemaName) => { |
176 |
| - Object.entries(schema.tables).forEach(([tableName, table]) => { |
177 |
| - table.columns.forEach((column) => { |
178 |
| - workQueue.push({ |
179 |
| - schema, |
180 |
| - schemaName, |
181 |
| - table, |
182 |
| - tableName, |
183 |
| - column, |
| 174 | + Array.from(schemas.entries()) |
| 175 | + .sort(([a], [b]) => { |
| 176 | + const schemaTerm = searchTerm.split('.')[0] |
| 177 | + |
| 178 | + // compute how many characters are left after removing the search term |
| 179 | + // the one that has less characters left should be prioritized |
| 180 | + const aDiff = a.replace(schemaTerm, '').length |
| 181 | + const bDiff = b.replace(schemaTerm, '').length |
| 182 | + |
| 183 | + const diff = aDiff - bDiff |
| 184 | + if (diff === 0) { |
| 185 | + return a.localeCompare(b) |
| 186 | + } else if (diff < 0) { |
| 187 | + return -1 |
| 188 | + } else { |
| 189 | + return 1 |
| 190 | + } |
| 191 | + }) |
| 192 | + .forEach(([schemaName, schema]) => { |
| 193 | + Object.entries(schema.tables) |
| 194 | + .sort(([a], [b]) => { |
| 195 | + const searchTerms = searchTerm.split('.') |
| 196 | + let tableTerm = searchTerms[0] |
| 197 | + if (searchTerms.length > 1 && schemaName.includes(searchTerms[0])) { |
| 198 | + tableTerm = searchTerms[1] |
| 199 | + } |
| 200 | + |
| 201 | + const aDiff = a.replace(tableTerm, '').length |
| 202 | + const bDiff = b.replace(tableTerm, '').length |
| 203 | + |
| 204 | + const diff = aDiff - bDiff |
| 205 | + if (diff === 0) { |
| 206 | + return a.localeCompare(b) |
| 207 | + } else if (diff < 0) { |
| 208 | + return -1 |
| 209 | + } else { |
| 210 | + return 1 |
| 211 | + } |
| 212 | + }) |
| 213 | + .forEach(([tableName, table]) => { |
| 214 | + table.columns.forEach((column) => { |
| 215 | + workQueue.push({ |
| 216 | + schema, |
| 217 | + schemaName, |
| 218 | + table, |
| 219 | + tableName, |
| 220 | + column, |
| 221 | + }) |
| 222 | + }) |
184 | 223 | })
|
185 |
| - }) |
186 | 224 | })
|
187 |
| - }) |
188 | 225 |
|
189 | 226 | Promise.resolve().then(async () => {
|
190 | 227 | if (!active) {
|
@@ -213,12 +250,7 @@ function useSchemaList(schemas: Map<string, DataSourceSchema>): UseSchemaList {
|
213 | 250 | fullColumnName
|
214 | 251 | .trim()
|
215 | 252 | .toLowerCase()
|
216 |
| - .includes(search.trim().toLowerCase()) || |
217 |
| - levenshtein( |
218 |
| - search.trim().toLowerCase(), |
219 |
| - fullColumnName.trim().toLowerCase() |
220 |
| - ) <= |
221 |
| - fullColumnName.length / 2 |
| 253 | + .includes(search.trim().toLowerCase()) |
222 | 254 | ) {
|
223 | 255 | if (!addedSchemas.has(work.schemaName)) {
|
224 | 256 | result.push({
|
|
0 commit comments