-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
50 changed files
with
2,283 additions
and
439 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
packages/headless/src/controllers/core/tab-manager/headless-core-tab-manager.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import {tabSetReducer as tabSet} from '../../../features/tab-set/tab-set-slice'; | ||
import { | ||
buildMockSearchEngine, | ||
MockedSearchEngine, | ||
} from '../../../test/mock-engine-v2'; | ||
import {createMockState} from '../../../test/mock-state'; | ||
import {buildCoreTabManager} from './headless-core-tab-manager'; | ||
|
||
jest.mock('../../../features/tab-set/tab-set-actions'); | ||
|
||
describe('Core Tab Manager', () => { | ||
let engine: MockedSearchEngine; | ||
|
||
function initTabManager() { | ||
buildCoreTabManager(engine); | ||
} | ||
|
||
beforeEach(() => { | ||
engine = buildMockSearchEngine(createMockState()); | ||
initTabManager(); | ||
}); | ||
|
||
it('returns the current active tab', () => { | ||
expect(engine.state.tabSet).toEqual({}); | ||
|
||
const tabId = '1'; | ||
const expectedTabSet = { | ||
[tabId]: { | ||
id: tabId, | ||
expression: 'test', | ||
isActive: true, | ||
}, | ||
}; | ||
|
||
engine.state.tabSet = expectedTabSet; | ||
|
||
expect(engine.state.tabSet).toEqual(expectedTabSet); | ||
}); | ||
|
||
it('adds the correct reducers to engine', () => { | ||
expect(engine.addReducers).toHaveBeenCalledWith({ | ||
tabSet, | ||
}); | ||
}); | ||
}); |
55 changes: 55 additions & 0 deletions
55
packages/headless/src/controllers/core/tab-manager/headless-core-tab-manager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import {createSelector} from '@reduxjs/toolkit'; | ||
import {CoreEngine} from '../../../app/engine'; | ||
import {tabSetReducer as tabSet} from '../../../features/tab-set/tab-set-slice'; | ||
import {TabSection} from '../../../state/state-sections'; | ||
import {loadReducerError} from '../../../utils/errors'; | ||
import { | ||
buildController, | ||
Controller, | ||
} from '../../controller/headless-controller'; | ||
|
||
export interface TabManager extends Controller { | ||
/** | ||
* The state of the `TabManager` controller. | ||
*/ | ||
state: TabManagerState; | ||
} | ||
|
||
/** | ||
* A scoped and simplified part of the headless state that is relevant to the `TabManager` controller. | ||
*/ | ||
export interface TabManagerState { | ||
activeTab: string; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function buildCoreTabManager(engine: CoreEngine): TabManager { | ||
if (!loadTabReducers(engine)) { | ||
throw loadReducerError; | ||
} | ||
|
||
const controller = buildController(engine); | ||
|
||
const currentTab = createSelector( | ||
(state: typeof engine.state) => state.tabSet, | ||
(state) => { | ||
const activeTab = Object.values(state).find((tab) => tab.isActive); | ||
return activeTab?.id ?? ''; | ||
} | ||
); | ||
|
||
return { | ||
...controller, | ||
|
||
get state() { | ||
return {activeTab: currentTab(engine.state)}; | ||
}, | ||
}; | ||
} | ||
|
||
function loadTabReducers(engine: CoreEngine): engine is CoreEngine<TabSection> { | ||
engine.addReducers({tabSet}); | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
packages/headless/src/controllers/tab-manager/headless-tab-manager.ssr.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {SearchEngine} from '../../app/search-engine/search-engine'; | ||
import {ControllerDefinitionWithoutProps} from '../../app/ssr-engine/types/common'; | ||
import {TabManager, buildTabManager} from './headless-tab-manager'; | ||
|
||
export * from './headless-tab-manager'; | ||
|
||
export interface TabManagerDefinition | ||
extends ControllerDefinitionWithoutProps<SearchEngine, TabManager> {} | ||
|
||
/** | ||
* Defines a `TabManager` controller instance. | ||
* | ||
* @returns The `TabManager` controller definition. | ||
* */ | ||
export function defineTabManager(): TabManagerDefinition { | ||
return { | ||
build: (engine) => buildTabManager(engine), | ||
}; | ||
} |
27 changes: 27 additions & 0 deletions
27
packages/headless/src/controllers/tab-manager/headless-tab-manager.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { | ||
MockedSearchEngine, | ||
buildMockSearchEngine, | ||
} from '../../test/mock-engine-v2'; | ||
import {createMockState} from '../../test/mock-state'; | ||
import {buildTabManager, TabManager} from './headless-tab-manager'; | ||
|
||
jest.mock('../../features/search/search-actions'); | ||
|
||
describe('Tab', () => { | ||
let engine: MockedSearchEngine; | ||
let tabManager: TabManager; | ||
|
||
function initTabManager() { | ||
tabManager = buildTabManager(engine); | ||
} | ||
|
||
beforeEach(() => { | ||
engine = buildMockSearchEngine(createMockState()); | ||
|
||
initTabManager(); | ||
}); | ||
|
||
it('initializes', () => { | ||
expect(tabManager).toBeTruthy(); | ||
}); | ||
}); |
27 changes: 27 additions & 0 deletions
27
packages/headless/src/controllers/tab-manager/headless-tab-manager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import {SearchEngine} from '../../app/search-engine/search-engine'; | ||
import { | ||
TabManager, | ||
TabManagerState, | ||
buildCoreTabManager, | ||
} from '../core/tab-manager/headless-core-tab-manager'; | ||
|
||
export type {TabManager, TabManagerState}; | ||
|
||
/** | ||
* Creates a `Tab Manager` controller instance. | ||
* | ||
* @param engine - The headless engine. | ||
* @param props - The configurable `Tab Manager` properties. | ||
* @returns A `Tab Manager` controller instance. | ||
*/ | ||
export function buildTabManager(engine: SearchEngine): TabManager { | ||
const tabManager = buildCoreTabManager(engine); | ||
|
||
return { | ||
...tabManager, | ||
|
||
get state() { | ||
return tabManager.state; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.