File size: 7,278 Bytes
4304c6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import type { FC } from 'react'
import {
  memo,
  useCallback,
} from 'react'
import { useTranslation } from 'react-i18next'
import { useContext } from 'use-context-selector'
import {
  useStore,
  useWorkflowStore,
} from '../store'
import {
  useChecklistBeforePublish,
  useNodesReadOnly,
  useNodesSyncDraft,
  useWorkflowMode,
  useWorkflowRun,
} from '../hooks'
import AppPublisher from '../../app/app-publisher'
import { ToastContext } from '../../base/toast'
import RunAndHistory from './run-and-history'
import EditingTitle from './editing-title'
import RunningTitle from './running-title'
import RestoringTitle from './restoring-title'
import ViewHistory from './view-history'
import Checklist from './checklist'
import { Grid01 } from '@/app/components/base/icons/src/vender/line/layout'
import Button from '@/app/components/base/button'
import { useStore as useAppStore } from '@/app/components/app/store'
import { publishWorkflow } from '@/service/workflow'
import { ArrowNarrowLeft } from '@/app/components/base/icons/src/vender/line/arrows'

const Header: FC = () => {
  const { t } = useTranslation()
  const workflowStore = useWorkflowStore()
  const appDetail = useAppStore(s => s.appDetail)
  const appSidebarExpand = useAppStore(s => s.appSidebarExpand)
  const appID = appDetail?.id
  const {
    nodesReadOnly,
    getNodesReadOnly,
  } = useNodesReadOnly()
  const publishedAt = useStore(s => s.publishedAt)
  const draftUpdatedAt = useStore(s => s.draftUpdatedAt)
  const {
    handleLoadBackupDraft,
    handleBackupDraft,
    handleRestoreFromPublishedWorkflow,
  } = useWorkflowRun()
  const { handleCheckBeforePublish } = useChecklistBeforePublish()
  const { handleSyncWorkflowDraft } = useNodesSyncDraft()
  const { notify } = useContext(ToastContext)
  const {
    normal,
    restoring,
    viewHistory,
  } = useWorkflowMode()

  const handleShowFeatures = useCallback(() => {
    const {
      isRestoring,
      setShowFeaturesPanel,
    } = workflowStore.getState()
    if (getNodesReadOnly() && !isRestoring)
      return

    setShowFeaturesPanel(true)
  }, [workflowStore, getNodesReadOnly])

  const handleCancelRestore = useCallback(() => {
    handleLoadBackupDraft()
    workflowStore.setState({ isRestoring: false })
  }, [workflowStore, handleLoadBackupDraft])

  const handleRestore = useCallback(() => {
    workflowStore.setState({ isRestoring: false })
    workflowStore.setState({ backupDraft: undefined })
    handleSyncWorkflowDraft(true)
  }, [handleSyncWorkflowDraft, workflowStore])

  const onPublish = useCallback(async () => {
    if (handleCheckBeforePublish()) {
      const res = await publishWorkflow(`/apps/${appID}/workflows/publish`)

      if (res) {
        notify({ type: 'success', message: t('common.api.actionSuccess') })
        workflowStore.getState().setPublishedAt(res.created_at)
      }
    }
    else {
      throw new Error('Checklist failed')
    }
  }, [appID, handleCheckBeforePublish, notify, t, workflowStore])

  const onStartRestoring = useCallback(() => {
    workflowStore.setState({ isRestoring: true })
    handleBackupDraft()
    handleRestoreFromPublishedWorkflow()
  }, [handleBackupDraft, handleRestoreFromPublishedWorkflow, workflowStore])

  const onPublisherToggle = useCallback((state: boolean) => {
    if (state)
      handleSyncWorkflowDraft(true)
  }, [handleSyncWorkflowDraft])

  const handleGoBackToEdit = useCallback(() => {
    handleLoadBackupDraft()
    workflowStore.setState({ historyWorkflowData: undefined })
  }, [workflowStore, handleLoadBackupDraft])

  return (
    <div

      className='absolute top-0 left-0 z-10 flex items-center justify-between w-full px-3 h-14'

      style={{

        background: 'linear-gradient(180deg, #F9FAFB 0%, rgba(249, 250, 251, 0.00) 100%)',

      }}

    >

      <div>

        {

          appSidebarExpand === 'collapse' && (

            <div className='text-xs font-medium text-gray-700'>{appDetail?.name}</div>

          )

        }

        {

          normal && <EditingTitle />

        }

        {

          viewHistory && <RunningTitle />

        }

        {

          restoring && <RestoringTitle />

        }

      </div>

      {

        normal && (

          <div className='flex items-center'>

            <RunAndHistory />

            <div className='mx-2 w-[1px] h-3.5 bg-gray-200'></div>

            <Button

              className={`

                mr-2 px-3 py-0 h-8 bg-white text-[13px] font-medium text-gray-700

                border-[0.5px] border-gray-200 shadow-xs

                ${nodesReadOnly && 'opacity-50 !cursor-not-allowed'}

              `}

              onClick={handleShowFeatures}

            >

              <Grid01 className='w-4 h-4 mr-1 text-gray-500' />

              {t('workflow.common.features')}

            </Button>

            <AppPublisher

              {...{

                publishedAt,

                draftUpdatedAt,

                disabled: Boolean(getNodesReadOnly()),

                onPublish,

                onRestore: onStartRestoring,

                onToggle: onPublisherToggle,

                crossAxisOffset: 53,

              }}

            />

            <div className='mx-2 w-[1px] h-3.5 bg-gray-200'></div>

            <Checklist disabled={nodesReadOnly} />

          </div>

        )

      }

      {

        viewHistory && (

          <div className='flex items-center'>

            <ViewHistory withText />

            <div className='mx-2 w-[1px] h-3.5 bg-gray-200'></div>

            <Button

              type='primary'

              className={`

                mr-2 px-3 py-0 h-8 text-[13px] font-medium

                border-[0.5px] border-gray-200 shadow-xs

              `}

              onClick={handleGoBackToEdit}

            >

              <ArrowNarrowLeft className='w-4 h-4 mr-1' />

              {t('workflow.common.goBackToEdit')}

            </Button>

          </div>

        )

      }

      {

        restoring && (

          <div className='flex items-center'>

            <Button

              className={`

                px-3 py-0 h-8 bg-white text-[13px] font-medium text-gray-700

                border-[0.5px] border-gray-200 shadow-xs

              `}

              onClick={handleShowFeatures}

            >

              <Grid01 className='w-4 h-4 mr-1 text-gray-500' />

              {t('workflow.common.features')}

            </Button>

            <div className='mx-2 w-[1px] h-3.5 bg-gray-200'></div>

            <Button

              className='mr-2 px-3 py-0 h-8 bg-white text-[13px] text-gray-700 font-medium border-[0.5px] border-gray-200 shadow-xs'

              onClick={handleCancelRestore}

            >

              {t('common.operation.cancel')}

            </Button>

            <Button

              className='px-3 py-0 h-8 text-[13px] font-medium shadow-xs'

              onClick={handleRestore}

              type='primary'

            >

              {t('workflow.common.restore')}

            </Button>

          </div>

        )

      }

    </div>
  )
}

export default memo(Header)