Skip to content

Commit

Permalink
Workspace list pr (opensearch-project#285)
Browse files Browse the repository at this point in the history
* feat: add workspace list

Signed-off-by: tygao <tygao@amazon.com>

* update index.ts

Signed-off-by: tygao <tygao@amazon.com>

* test: update workspace plugin test

Signed-off-by: tygao <tygao@amazon.com>

* remove extra file and add test for modal

Signed-off-by: tygao <tygao@amazon.com>

* test: add tests for workspace util

Signed-off-by: tygao <tygao@amazon.com>

* test: update test for delete_workspace_modal

Signed-off-by: tygao <tygao@amazon.com>

* test: update test for delete_workspace_modal

Signed-off-by: tygao <tygao@amazon.com>

* test: update test for workspace list

Signed-off-by: tygao <tygao@amazon.com>

* rename and update test

Signed-off-by: tygao <tygao@amazon.com>

* add tests for workspace list

Signed-off-by: tygao <tygao@amazon.com>

---------

Signed-off-by: tygao <tygao@amazon.com>
  • Loading branch information
raintygao authored and SuZhou-Joe committed Mar 18, 2024
1 parent 6f12320 commit d287778
Show file tree
Hide file tree
Showing 17 changed files with 1,426 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/core/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,5 @@ export { __osdBootstrap__ } from './osd_bootstrap';
export { WorkspacesStart, WorkspacesSetup, WorkspacesService } from './workspace';

export { WORKSPACE_TYPE, cleanWorkspaceId, DEFAULT_WORKSPACE_ID } from '../utils';

export { debounce } from './utils';
55 changes: 55 additions & 0 deletions src/core/public/utils/debounce.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import { debounce } from './debounce';

describe('debounce', () => {
let fn: Function;
beforeEach(() => {
fn = jest.fn();
jest.useFakeTimers();
});
afterEach(() => {
jest.clearAllTimers();
});

test('it should call the debounced fn once at the end of the quiet time', () => {
const debounced = debounce(fn, 1000);

for (let i = 0; i < 100; i++) {
debounced(i);
}

jest.advanceTimersByTime(1001);
expect(fn).toBeCalledTimes(1);
expect(fn).toBeCalledWith(99);
});

test("with a leading invocation, it should call the debounced fn once, if the time doens't pass", () => {
const debounced = debounce(fn, 1000, true);

for (let i = 0; i < 100; i++) {
debounced(i);
}

jest.advanceTimersByTime(999);

expect(fn).toBeCalledTimes(1);
expect(fn).toBeCalledWith(0);
});

test('with a leading invocation, it should call the debounced fn twice (at the beginning and at the end)', () => {
const debounced = debounce(fn, 1000, true);

for (let i = 0; i < 100; i++) {
debounced(i);
}

jest.advanceTimersByTime(1500);

expect(fn).toBeCalledTimes(2);
expect(fn).toBeCalledWith(0);
expect(fn).toBeCalledWith(99);
});
});
23 changes: 23 additions & 0 deletions src/core/public/utils/debounce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @param func The function to be debounced.
* @param delay The time in milliseconds to wait before invoking the function again after the last invocation.
* @param leading An optional parameter that, when true, allows the function to be invoked immediately upon the first call.
*/
export const debounce = (func: Function, delay: number, leading?: boolean) => {
let timerId: NodeJS.Timeout;

return (...args: any) => {
if (!timerId && leading) {
func(...args);
}
clearTimeout(timerId);

timerId = setTimeout(() => func(...args), delay);
};
};
1 change: 1 addition & 0 deletions src/core/public/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ export {
getWorkspaceIdFromUrl,
cleanWorkspaceId,
} from '../../utils';
export { debounce } from './debounce';
14 changes: 14 additions & 0 deletions src/plugins/workspace/public/application.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React from 'react';
import ReactDOM from 'react-dom';
import { AppMountParameters, ScopedHistory } from '../../../core/public';
import { OpenSearchDashboardsContextProvider } from '../../opensearch_dashboards_react/public';
import { WorkspaceListApp } from './components/workspace_list_app';
import { WorkspaceFatalError } from './components/workspace_fatal_error';
import { Services } from './types';

Expand All @@ -24,3 +25,16 @@ export const renderFatalErrorApp = (params: AppMountParameters, services: Servic
ReactDOM.unmountComponentAtNode(element);
};
};

export const renderListApp = ({ element }: AppMountParameters, services: Services) => {
ReactDOM.render(
<OpenSearchDashboardsContextProvider services={services}>
<WorkspaceListApp />
</OpenSearchDashboardsContextProvider>,
element
);

return () => {
ReactDOM.unmountComponentAtNode(element);
};
};

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d287778

Please sign in to comment.