-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* runner improvements * runner improvement * maintain runner state in context * fix lint * save file object to context * add request list to runner context to keep order * fix ts error * add eventbus & clear runner context state * also delete folder runner tab when delete a folder * clean * change runner context data structure * clean up
- Loading branch information
1 parent
57edf80
commit cbbc8c2
Showing
11 changed files
with
429 additions
and
161 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import React, { createContext, type FC, type PropsWithChildren, useCallback, useContext, useEffect } from 'react'; | ||
import type { Selection } from 'react-aria-components'; | ||
|
||
import type { UploadDataType } from '../../components/modals/upload-runner-data-modal'; | ||
import uiEventBus, { UIEventType } from '../../eventBus'; | ||
import useStateRef from '../../hooks/use-state-ref'; | ||
import type { RequestRow } from '../../routes/runner'; | ||
|
||
interface RunnerState { | ||
selectedKeys: Selection; | ||
iterationCount: number; | ||
delay: number; | ||
uploadData: UploadDataType[]; | ||
advancedConfig: Record<string, boolean>; | ||
file: File | null; | ||
reqList: RequestRow[]; | ||
} | ||
|
||
interface OrgRunnerStateMap { | ||
[runnerId: string]: Partial<RunnerState>; | ||
} | ||
|
||
interface RunnerStateMap { | ||
[orgId: string]: OrgRunnerStateMap; | ||
}; | ||
interface ContextProps { | ||
runnerStateMap: RunnerStateMap; | ||
runnerStateRef?: React.MutableRefObject<RunnerStateMap>; | ||
updateRunnerState: (organizationId: string, runnerId: string, patch: Partial<RunnerState>) => void; | ||
} | ||
const RunnerContext = createContext<ContextProps>({ | ||
runnerStateMap: {}, | ||
updateRunnerState: () => { }, | ||
}); | ||
|
||
export const RunnerProvider: FC<PropsWithChildren> = ({ children }) => { | ||
|
||
const [runnerState, setRunnerState, runnerStateRef] = useStateRef<RunnerStateMap>({}); | ||
|
||
const updateRunnerState = useCallback((organizationId: string, runnerId: string, patch: Partial<RunnerState>) => { | ||
setRunnerState(prevState => { | ||
const newState = { | ||
...prevState, | ||
[organizationId]: { | ||
...prevState[organizationId], | ||
[runnerId]: { ...prevState[organizationId]?.[runnerId], ...patch }, | ||
}, | ||
}; | ||
return newState; | ||
}); | ||
}, [setRunnerState]); | ||
|
||
const handleTabClose = useCallback((organizationId: string, ids: 'all' | string[]) => { | ||
if (ids === 'all') { | ||
setRunnerState(prevState => { | ||
const newState = { ...prevState }; | ||
delete newState[organizationId]; | ||
return newState; | ||
}); | ||
return; | ||
} | ||
|
||
setRunnerState(prevState => { | ||
const newOrgState = { ...prevState?.[organizationId] }; | ||
ids.forEach(id => { | ||
// runner tab id starts with 'runner' prefix, but the runnerId in this context doesn't have the prefix, so we need to remove it | ||
if (id.startsWith('runner')) { | ||
const runnerId = id.replace('runner_', ''); | ||
delete newOrgState[runnerId]; | ||
} | ||
}); | ||
return { | ||
...prevState, | ||
[organizationId]: newOrgState, | ||
}; | ||
}); | ||
}, [setRunnerState]); | ||
|
||
useEffect(() => { | ||
uiEventBus.on(UIEventType.CLOSE_TAB, handleTabClose); | ||
return () => { | ||
uiEventBus.off(UIEventType.CLOSE_TAB, handleTabClose); | ||
}; | ||
}, [handleTabClose]); | ||
|
||
return ( | ||
<RunnerContext.Provider | ||
value={{ | ||
runnerStateMap: runnerState, | ||
runnerStateRef, | ||
updateRunnerState, | ||
}} | ||
> | ||
{children} | ||
</RunnerContext.Provider> | ||
); | ||
}; | ||
|
||
export const useRunnerContext = () => useContext(RunnerContext); |
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,37 @@ | ||
type EventHandler = (...args: any[]) => void; | ||
|
||
export enum UIEventType { | ||
CLOSE_TAB = 'closeTab', | ||
} | ||
class EventBus { | ||
private events: Record<UIEventType, EventHandler[]> = { | ||
[UIEventType.CLOSE_TAB]: [], | ||
}; | ||
|
||
// Subscribe to event | ||
on(event: UIEventType, handler: EventHandler): void { | ||
if (!this.events[event]) { | ||
this.events[event] = []; | ||
} | ||
this.events[event].push(handler); | ||
} | ||
|
||
// Unsubscribe from event | ||
off(event: UIEventType, handler: EventHandler): void { | ||
if (!this.events[event]) { | ||
return; | ||
} | ||
this.events[event] = this.events[event].filter(h => h !== handler); | ||
} | ||
|
||
// emit event | ||
emit(event: UIEventType, ...args: any[]): void { | ||
if (!this.events[event]) { | ||
return; | ||
} | ||
this.events[event].forEach(handler => handler(...args)); | ||
} | ||
} | ||
|
||
const uiEventBus = new EventBus(); | ||
export default uiEventBus; |
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.