michael-s-molina commented on code in PR #24677: URL: https://github.com/apache/superset/pull/24677#discussion_r1269515373
########## superset-frontend/src/SqlLab/components/AceEditorWrapper/useKeywords.ts: ########## @@ -0,0 +1,200 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { useEffect, useMemo, useRef } from 'react'; +import { useSelector, useDispatch, shallowEqual, useStore } from 'react-redux'; +import { t } from '@superset-ui/core'; + +import { Editor } from 'src/components/AsyncAceEditor'; +import sqlKeywords from 'src/SqlLab/utils/sqlKeywords'; +import { addTable, addDangerToast } from 'src/SqlLab/actions/sqlLab'; +import { + SCHEMA_AUTOCOMPLETE_SCORE, + TABLE_AUTOCOMPLETE_SCORE, + COLUMN_AUTOCOMPLETE_SCORE, + SQL_FUNCTIONS_AUTOCOMPLETE_SCORE, +} from 'src/SqlLab/constants'; +import { + useSchemas, + useTables, + tableEndpoints, + skipToken, +} from 'src/hooks/apiResources'; +import { api } from 'src/hooks/apiResources/queryApi'; +import { useDatabaseFunctionsQuery } from 'src/hooks/apiResources/databaseFunctions'; +import useEffectEvent from 'src/hooks/useEffectEvent'; +import { SqlLabRootState } from 'src/SqlLab/types'; + +type Params = { + queryEditorId: string | number; + dbId?: string | number; + schema?: string; +}; + +const EMPTY_LIST = [] as typeof sqlKeywords; + +export function useKeywords( + { queryEditorId, dbId, schema }: Params, + skip = false, +) { + const dispatch = useDispatch(); + const hasFetchedKeywords = useRef(false); + // skipFetch is used to prevent re-evaluating memoized keywords + // due to updated api results by skip flag + const skipFetch = hasFetchedKeywords && skip; + const { data: schemaOptions } = useSchemas({ + ...(!skipFetch && { dbId }), + }); + const { data: tableData } = useTables({ + ...(!skipFetch && { + dbId, + schema, + }), + }); + + const { data: functionNames, isError } = useDatabaseFunctionsQuery( + { dbId }, + { skip: skipFetch || !dbId }, + ); + + useEffect(() => { + if (isError) { + dispatch( + addDangerToast(t('An error occurred while fetching function names.')), + ); + } + }, [dispatch, isError]); + + const tablesForColumnMetadata = useSelector<SqlLabRootState, string[]>( + ({ sqlLab }) => + skip + ? [] + : (sqlLab?.tables ?? []) + .filter(table => table.queryEditorId === queryEditorId) + .map(table => table.name), + shallowEqual, + ); + + const store = useStore(); + const apiState = store.getState()[api.reducerPath]; + + const allColumns = useMemo(() => { + const columns = new Set<string>(); + tablesForColumnMetadata.forEach(table => { + tableEndpoints.tableMetadata + .select( + dbId && schema + ? { + dbId, + schema, + table, + } + : skipToken, + )({ + [api.reducerPath]: apiState, + }) + .data?.columns?.forEach(({ name }) => { + columns.add(name); + }); + }); + return [...columns]; + }, [dbId, schema, apiState, tablesForColumnMetadata]); + + const insertMatch = useEffectEvent((editor: Editor, data: any) => { + if (data.meta === 'table') { + dispatch(addTable({ id: queryEditorId, dbId }, data.value, schema)); + } + + let { caption } = data; + if (data.meta === 'table' && caption.includes(' ')) { + caption = `"${caption}"`; + } + + // executing https://github.com/thlorenz/brace/blob/3a00c5d59777f9d826841178e1eb36694177f5e6/ext/language_tools.js#L1448 + editor.completer.insertMatch( + `${caption}${['function', 'schema'].includes(data.meta) ? '' : ' '}`, + ); + }); + + const schemaKeywords = useMemo( + () => + (schemaOptions ?? []).map(s => ({ + name: s.label, + value: s.value, + score: SCHEMA_AUTOCOMPLETE_SCORE, + meta: 'schema', + completer: { + insertMatch, + }, + })), + [schemaOptions, insertMatch], + ); + + const tableKeywords = useMemo( + () => + (tableData?.options ?? []).map(({ value, label }) => ({ + name: label, + value, + score: TABLE_AUTOCOMPLETE_SCORE, + meta: 'table', + completer: { + insertMatch, + }, + })), + [tableData?.options, insertMatch], + ); + + const columnKeywords = useMemo( + () => + allColumns.map(col => ({ + name: col, + value: col, + score: COLUMN_AUTOCOMPLETE_SCORE, + meta: 'column', + })), + [allColumns], + ); + + const functionKeywords = useMemo( + () => + (functionNames ?? []).map(func => ({ + name: func, + value: func, + score: SQL_FUNCTIONS_AUTOCOMPLETE_SCORE, + meta: 'function', + completer: { + insertMatch, + }, + })), + [functionNames, insertMatch], + ); + + const keywords = useMemo( + () => + columnKeywords + .concat(schemaKeywords) + .concat(tableKeywords) + .concat(functionKeywords) + .concat(sqlKeywords), + [schemaKeywords, tableKeywords, columnKeywords, functionKeywords], + ); + + hasFetchedKeywords.current = !skip; + + return skip ? EMPTY_LIST : keywords; Review Comment: 🤦🏼♂️ ########## superset-frontend/src/SqlLab/components/AceEditorWrapper/useKeywords.ts: ########## @@ -0,0 +1,200 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { useEffect, useMemo, useRef } from 'react'; +import { useSelector, useDispatch, shallowEqual, useStore } from 'react-redux'; +import { t } from '@superset-ui/core'; + +import { Editor } from 'src/components/AsyncAceEditor'; +import sqlKeywords from 'src/SqlLab/utils/sqlKeywords'; +import { addTable, addDangerToast } from 'src/SqlLab/actions/sqlLab'; +import { + SCHEMA_AUTOCOMPLETE_SCORE, + TABLE_AUTOCOMPLETE_SCORE, + COLUMN_AUTOCOMPLETE_SCORE, + SQL_FUNCTIONS_AUTOCOMPLETE_SCORE, +} from 'src/SqlLab/constants'; +import { + useSchemas, + useTables, + tableEndpoints, + skipToken, +} from 'src/hooks/apiResources'; +import { api } from 'src/hooks/apiResources/queryApi'; +import { useDatabaseFunctionsQuery } from 'src/hooks/apiResources/databaseFunctions'; +import useEffectEvent from 'src/hooks/useEffectEvent'; +import { SqlLabRootState } from 'src/SqlLab/types'; + +type Params = { + queryEditorId: string | number; + dbId?: string | number; + schema?: string; +}; + +const EMPTY_LIST = [] as typeof sqlKeywords; + +export function useKeywords( + { queryEditorId, dbId, schema }: Params, + skip = false, +) { + const dispatch = useDispatch(); + const hasFetchedKeywords = useRef(false); + // skipFetch is used to prevent re-evaluating memoized keywords + // due to updated api results by skip flag + const skipFetch = hasFetchedKeywords && skip; + const { data: schemaOptions } = useSchemas({ + ...(!skipFetch && { dbId }), + }); + const { data: tableData } = useTables({ + ...(!skipFetch && { + dbId, + schema, + }), + }); + + const { data: functionNames, isError } = useDatabaseFunctionsQuery( + { dbId }, + { skip: skipFetch || !dbId }, + ); + + useEffect(() => { + if (isError) { + dispatch( + addDangerToast(t('An error occurred while fetching function names.')), + ); + } + }, [dispatch, isError]); + + const tablesForColumnMetadata = useSelector<SqlLabRootState, string[]>( + ({ sqlLab }) => + skip + ? [] + : (sqlLab?.tables ?? []) + .filter(table => table.queryEditorId === queryEditorId) + .map(table => table.name), + shallowEqual, + ); + + const store = useStore(); + const apiState = store.getState()[api.reducerPath]; + + const allColumns = useMemo(() => { + const columns = new Set<string>(); + tablesForColumnMetadata.forEach(table => { + tableEndpoints.tableMetadata + .select( + dbId && schema + ? { + dbId, + schema, + table, + } + : skipToken, + )({ + [api.reducerPath]: apiState, + }) + .data?.columns?.forEach(({ name }) => { + columns.add(name); + }); + }); + return [...columns]; + }, [dbId, schema, apiState, tablesForColumnMetadata]); + + const insertMatch = useEffectEvent((editor: Editor, data: any) => { + if (data.meta === 'table') { + dispatch(addTable({ id: queryEditorId, dbId }, data.value, schema)); + } + + let { caption } = data; + if (data.meta === 'table' && caption.includes(' ')) { + caption = `"${caption}"`; + } + + // executing https://github.com/thlorenz/brace/blob/3a00c5d59777f9d826841178e1eb36694177f5e6/ext/language_tools.js#L1448 + editor.completer.insertMatch( + `${caption}${['function', 'schema'].includes(data.meta) ? '' : ' '}`, + ); + }); + + const schemaKeywords = useMemo( + () => + (schemaOptions ?? []).map(s => ({ + name: s.label, + value: s.value, + score: SCHEMA_AUTOCOMPLETE_SCORE, + meta: 'schema', + completer: { + insertMatch, + }, + })), + [schemaOptions, insertMatch], + ); + + const tableKeywords = useMemo( + () => + (tableData?.options ?? []).map(({ value, label }) => ({ + name: label, + value, + score: TABLE_AUTOCOMPLETE_SCORE, + meta: 'table', + completer: { + insertMatch, + }, + })), + [tableData?.options, insertMatch], + ); + + const columnKeywords = useMemo( + () => + allColumns.map(col => ({ + name: col, + value: col, + score: COLUMN_AUTOCOMPLETE_SCORE, + meta: 'column', + })), + [allColumns], + ); + + const functionKeywords = useMemo( + () => + (functionNames ?? []).map(func => ({ + name: func, + value: func, + score: SQL_FUNCTIONS_AUTOCOMPLETE_SCORE, + meta: 'function', + completer: { + insertMatch, + }, + })), + [functionNames, insertMatch], + ); + + const keywords = useMemo( + () => + columnKeywords + .concat(schemaKeywords) + .concat(tableKeywords) + .concat(functionKeywords) + .concat(sqlKeywords), + [schemaKeywords, tableKeywords, columnKeywords, functionKeywords], + ); + + hasFetchedKeywords.current = !skip; + + return skip ? EMPTY_LIST : keywords; Review Comment: 🤦🏼♂️ -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For additional commands, e-mail: notifications-h...@superset.apache.org