1- import { type IAction , IApp , ITrigger } from '@plumber/types'
1+ import type { IAction , IApp , ITrigger } from '@plumber/types'
22
33import { useCallback , useContext , useMemo } from 'react'
44import { BiArrowFromRight , BiChevronRight } from 'react-icons/bi'
@@ -14,12 +14,19 @@ import {
1414import { Badge , ModalCloseButton } from '@opengovsg/design-system-react'
1515import { groupBy } from 'lodash'
1616
17- import { getAppFlag } from '@/config/flags'
17+ import { getAppActionFlag , getAppFlag } from '@/config/flags'
1818import { LaunchDarklyContext } from '@/contexts/LaunchDarkly'
19+ import {
20+ TOOLBOX_ACTIONS ,
21+ TOOLBOX_APP_KEY ,
22+ useIfThenInitializer ,
23+ useIsIfThenSelectable ,
24+ } from '@/helpers/toolbox'
1925
2026import { FlowStepConfigurationContext } from '../FlowStepConfigurationContext'
2127
2228import FeedbackFooter from './FeedbackFooter'
29+ import ToolboxEvent from './ToolboxEvent'
2330
2431const OTHERS_CATEGORY = 'Other'
2532
@@ -31,10 +38,12 @@ interface ChooseAppProps {
3138export default function ChooseApp ( props : ChooseAppProps ) {
3239 const { apps, onSelectAppEvent } = props
3340 const launchDarkly = useContext ( LaunchDarklyContext )
34- const { patchModalState, isTrigger } = useContext (
41+ const { patchModalState, isTrigger, isLastStep } = useContext (
3542 FlowStepConfigurationContext ,
3643 )
37- const isLoading = launchDarkly . isLoading
44+
45+ const [ _ , isInitializingIfThen ] = useIfThenInitializer ( )
46+ const isLoading = launchDarkly . isLoading || isInitializingIfThen
3847
3948 const onSelectApp = useCallback (
4049 ( app : IApp ) => {
@@ -46,6 +55,30 @@ export default function ChooseApp(props: ChooseAppProps) {
4655 [ patchModalState ] ,
4756 )
4857
58+ const isIfThenSelectable = useIsIfThenSelectable ( { isLastStep } )
59+ const filteredToolboxActions = useMemo ( ( ) => {
60+ if ( isLoading || ! launchDarkly . flags ) {
61+ return [ ]
62+ }
63+
64+ const ldToolboxAppFlag = getAppFlag ( TOOLBOX_APP_KEY )
65+ if ( ! launchDarkly . flags [ ldToolboxAppFlag ] ) {
66+ return [ ]
67+ }
68+
69+ const toolboxActions =
70+ apps ?. find ( ( app ) => app . key === TOOLBOX_APP_KEY ) ?. actions ?? [ ]
71+ return toolboxActions . filter ( ( action ) => {
72+ // Filter away actions hidden behind feature flags
73+ if ( isLoading || ! launchDarkly . flags ) {
74+ return true
75+ }
76+
77+ const ldToolboxActionFlag = getAppActionFlag ( TOOLBOX_APP_KEY , action . key )
78+ return launchDarkly . flags [ ldToolboxActionFlag ] ?? true
79+ } )
80+ } , [ apps , isLoading , launchDarkly . flags ] )
81+
4982 // Combine filtering and grouping logic into a single operation
5083 const groupedApps = useMemo ( ( ) => {
5184 const filteredApps = apps ?. filter ( ( app ) => {
@@ -107,10 +140,10 @@ export default function ChooseApp(props: ChooseAppProps) {
107140 linear-gradient(rgba(255, 255, 255, 0), white 70%) center bottom,
108141
109142 /* Shadow TOP */
110- radial-gradient(farthest-side at 50% 0, rgba(0, 0, 0, 0.2 ), rgba(0, 0, 0, 0)) center top,
143+ radial-gradient(farthest-side at 50% 0, rgba(0, 0, 0, 0.1 ), rgba(0, 0, 0, 0)) center top,
111144
112145 /* Shadow BOTTOM */
113- radial-gradient(farthest-side at 50% 100%, rgba(0, 0, 0, 0.2 ), rgba(0, 0, 0, 0)) center bottom
146+ radial-gradient(farthest-side at 50% 100%, rgba(0, 0, 0, 0.1 ), rgba(0, 0, 0, 0)) center bottom
114147 ` ,
115148 backgroundRepeat : 'no-repeat' ,
116149 backgroundSize : '100% 40px, 100% 40px, 100% 14px, 100% 14px' ,
@@ -138,6 +171,21 @@ export default function ChooseApp(props: ChooseAppProps) {
138171
139172 < Flex flexDir = "column" gap = { 3 } >
140173 { apps . map ( ( app ) => {
174+ // For toolbox app specifically, show all the toolbox actions
175+ if ( app . key === TOOLBOX_APP_KEY ) {
176+ return filteredToolboxActions . map ( ( action ) => (
177+ < ToolboxEvent
178+ key = { action . key }
179+ action = { action }
180+ onSelectAppEvent = { ( ) => onSelectAppEvent ( app , action ) }
181+ isDisabled = {
182+ action . key === TOOLBOX_ACTIONS . IfThen &&
183+ ! isIfThenSelectable
184+ }
185+ />
186+ ) )
187+ }
188+
141189 const triggersOrActions = isTrigger
142190 ? app . triggers
143191 : app . actions
0 commit comments