PromucFlow_constructor/app/client/src/utils/editorContextUtils.ts
sneha122 49fb6ae852
feat: gsheet disable new query when no files selected (#21912)
## Description

This PR adds:
- When gsheet datasource is created with "Selected Sheets" Modality, and
if user fails to select any files from file picker
    - we should disable new query creation
    - we should show error message banner on datasource review page

**How to test:**
- Create google sheet datasource, break the authorisation flow, check
review page, error message like "Data source is not authorized, please
authorize to continue." would be shown.
- Edit datasource, complete the authorisation but do not pick any files
from file picker, error message like "Datasource does not have access to
any files, please authorize google sheets to use this data source" would
be shown


Fixes #20290

Media
> A video or a GIF is preferred. when using Loom, don’t embed because it
looks like it’s a GIF. instead, just link to the video


## Type of change

> Please delete options that are not relevant.

- New feature (non-breaking change which adds functionality)

## How Has This Been Tested?

- Manual

### Test Plan
> Add Testsmith test cases links that relate to this PR

### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)


## Checklist:
### Dev activity
- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [x] PR is being merged under a feature flag


### QA activity:
- [ ] Test plan has been approved by relevant developers
- [ ] Test plan has been peer reviewed by QA
- [ ] Cypress test cases have been added and approved by either SDET or
manual QA
- [ ] Organized project review call with relevant stakeholders after
Round 1/2 of QA
- [ ] Added Test Plan Approved label after reveiwing all Cypress test

---------

Co-authored-by: “sneha122” <“sneha@appsmith.com”>
2023-04-06 19:42:34 +05:30

124 lines
3.5 KiB
TypeScript

import type { Plugin } from "api/PluginApi";
import { PluginPackageName } from "entities/Action";
import type { Datasource } from "entities/Datasource";
import { AuthenticationStatus, AuthType } from "entities/Datasource";
export function isCurrentFocusOnInput() {
return (
["input", "textarea"].indexOf(
document.activeElement?.tagName?.toLowerCase() || "",
) >= 0
);
}
/**
* This method returns boolean if the propertyControl is focused.
* @param domElement
* @returns
*/
export function shouldFocusOnPropertyControl(
domElement?: HTMLDivElement | null,
) {
let isCurrentFocusOnProperty = false;
if (domElement) {
isCurrentFocusOnProperty = domElement.contains(document.activeElement);
}
return !(isCurrentFocusOnInput() || isCurrentFocusOnProperty);
}
/**
* Returns a focusable field of PropertyControl.
* @param element
* @returns
*/
export function getPropertyControlFocusElement(
element: HTMLDivElement | null,
): HTMLElement | undefined {
if (element?.children) {
const [, propertyInputElement] = element.children;
if (propertyInputElement) {
const uiInputElement = propertyInputElement.querySelector(
'button:not([tabindex="-1"]), input, [tabindex]:not([tabindex="-1"])',
) as HTMLElement | undefined;
if (uiInputElement) {
return uiInputElement;
}
const codeEditorInputElement =
propertyInputElement.getElementsByClassName("CodeEditorTarget")[0] as
| HTMLElement
| undefined;
if (codeEditorInputElement) {
return codeEditorInputElement;
}
const lazyCodeEditorInputElement =
propertyInputElement.getElementsByClassName("LazyCodeEditor")[0] as
| HTMLElement
| undefined;
if (lazyCodeEditorInputElement) {
return lazyCodeEditorInputElement;
}
}
}
}
/**
* Returns true if :
* - authentication type is not oauth2 or is not a Google Sheet Plugin
* - authentication type is oauth2 and authorized status success and is a Google Sheet Plugin
* @param datasource Datasource
* @param plugin Plugin
* @returns boolean
*/
export function isDatasourceAuthorizedForQueryCreation(
datasource: Datasource,
plugin: Plugin,
): boolean {
if (!datasource) return false;
const authType =
datasource &&
datasource?.datasourceConfiguration?.authentication?.authenticationType;
/*
TODO: This flag will be removed once the multiple environment is merged to avoid design inconsistency between different datasources.
Search for: GoogleSheetPluginFlag to check for all the google sheet conditional logic throughout the code.
*/
const isGoogleSheetPlugin =
plugin.packageName === PluginPackageName.GOOGLE_SHEETS;
if (isGoogleSheetPlugin && authType === AuthType.OAUTH2) {
const isAuthorized =
datasource?.datasourceConfiguration?.authentication
?.authenticationStatus === AuthenticationStatus.SUCCESS;
return isAuthorized;
}
return true;
}
/**
* Returns datasource property value from datasource?.datasourceConfiguration?.properties
* @param datasource Datasource
* @param propertyKey string
* @returns string | null
*/
export function getDatasourcePropertyValue(
datasource: Datasource,
propertyKey: string,
): string | null {
if (!datasource) {
return null;
}
const properties = datasource?.datasourceConfiguration?.properties;
if (!!properties && properties.length > 0) {
const propertyObj = properties.find((prop) => prop.key === propertyKey);
if (!!propertyObj) {
return propertyObj.value;
}
}
return null;
}