Skip to content

Commit

Permalink
Move state variable from state to ref to prevent infinite loop (#46)
Browse files Browse the repository at this point in the history
* Update Contributing.md
  • Loading branch information
idastambuk authored May 9, 2023
1 parent 926a5dc commit 07bd2a2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ Want to install this repo locally?

Ready to release a new version?

- create a pr and update the package.json and changelog to be how you'd like
- merge the pr, that's it! (get's published to npm via https://github.com/grafana/grafana-aws-sdk-react/blob/main/.github/workflows/publish-npm.yml)
- create a pr and update the package.json with the new version, as well as the changelog to be how you'd like
- merge the pr, that's it! (the change in the version triggers a publish to npm via https://github.com/grafana/grafana-aws-sdk-react/blob/main/.github/workflows/publish-npm.yml)
- a note we don't currently make tags/releases on github, but perhaps in the future we can do so
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@grafana/aws-sdk",
"version": "0.0.45",
"version": "0.0.46",
"description": "Common AWS features for grafana",
"main": "dist/index.js",
"module": "dist/esm/index.js",
Expand Down
38 changes: 23 additions & 15 deletions src/sql/ResourceSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SelectableValue } from '@grafana/data';
import { InlineField, Select, SelectCommonProps } from '@grafana/ui';
import { isEqual } from 'lodash';
import React, { useEffect, useMemo, useState, DependencyList } from 'react';
import React, { useEffect, useMemo, useState, DependencyList, useRef } from 'react';

import { defaultKey } from './types';

Expand All @@ -25,11 +25,19 @@ export interface ResourceSelectorProps extends SelectCommonProps<string> {
}

export function ResourceSelector(props: ResourceSelectorProps) {
const [resource, setResource] = useState<string | null>(props.value || props.default || null);
const [resources, setResources] = useState<Array<string | SelectableValue>>(resource ? [resource] : []);
const [dependencies, setDependencies] = useState(props.dependencies);
const propsDependencies = props.dependencies;
const propsOnChange = props.onChange

const dependencies = useRef(props.dependencies);
const fetched = useRef<boolean>(false);
const resource = useRef<string | null>(props.value || props.default || null);

const [resources, setResources] = useState<Array<string | SelectableValue>>(
resource.current ? [resource.current] : []
);
const [isLoading, setIsLoading] = useState(false);
const [fetched, setFetched] = useState(false);


const defaultOpts = useMemo(() => {
const opts: Array<SelectableValue<string>> = [
{
Expand Down Expand Up @@ -66,16 +74,16 @@ export function ResourceSelector(props: ResourceSelectorProps) {

useEffect(() => {
// A change in the dependencies cause a state clean-up
if (!isEqual(props.dependencies, dependencies)) {
setFetched(false);
setResource(null);
props.onChange(null);
setDependencies(props.dependencies);
if (!isEqual(propsDependencies, dependencies.current)) {
fetched.current = false;
resource.current = null;
dependencies.current = propsDependencies
propsOnChange(null);
}
}, [props, dependencies]);
}, [propsDependencies, propsOnChange]);

const fetch = async () => {
if (fetched) {
if (fetched.current) {
return;
}
if (props.saveOptions) {
Expand All @@ -85,14 +93,14 @@ export function ResourceSelector(props: ResourceSelectorProps) {
const resources = (await props.fetch?.()) || [];
setResources(resources);
} finally {
setFetched(true);
fetched.current = true;
}
};

const onChange = (e: SelectableValue<string>) => {
props.onChange(e);
propsOnChange(e);
if (e.value) {
setResource(e.value);
resource.current = e.value;
}
};
const onClick = async () => {
Expand Down

0 comments on commit 07bd2a2

Please sign in to comment.