|
| 1 | +import { Select, Stack } from '@mantine/core'; |
| 2 | +import { observer } from 'mobx-react-lite'; |
| 3 | +import { useTranslation } from 'react-i18next'; |
| 4 | +import { useStorageData } from '~/components/plugins'; |
| 5 | +import { useEditContentModelContext } from '~/contexts'; |
| 6 | +import { IDashboardOperation, IDashboardOperationSchema, IOperationConfigProps } from '~/types/plugin'; |
| 7 | +import { getSelectChangeHandler } from '~/utils/mantine'; |
| 8 | + |
| 9 | +export interface ISwitchTabOperationConfig { |
| 10 | + viewID: string; |
| 11 | + tab: string; |
| 12 | +} |
| 13 | + |
| 14 | +const SwitchTabOperationSettings = observer((props: IOperationConfigProps) => { |
| 15 | + const { t } = useTranslation(); |
| 16 | + const model = useEditContentModelContext(); |
| 17 | + const { value, set } = useStorageData<ISwitchTabOperationConfig>(props.operation.operationData, 'config'); |
| 18 | + |
| 19 | + const { viewID = '', tab = '' } = value ?? {}; |
| 20 | + const setViewID = (viewID: string) => void set({ viewID, tab }); |
| 21 | + const setTab = (tab: string) => void set({ viewID, tab }); |
| 22 | + return ( |
| 23 | + <Stack gap="xs"> |
| 24 | + <Select |
| 25 | + defaultValue={viewID} |
| 26 | + value={viewID} |
| 27 | + onChange={getSelectChangeHandler(setViewID)} |
| 28 | + label={t('interactions.operation.switch_tab.view')} |
| 29 | + data={model.views.tabViewOptions} |
| 30 | + comboboxProps={{ |
| 31 | + withinPortal: true, |
| 32 | + zIndex: 340, |
| 33 | + }} |
| 34 | + maxDropdownHeight={500} |
| 35 | + /> |
| 36 | + <Select |
| 37 | + defaultValue={tab} |
| 38 | + value={tab} |
| 39 | + onChange={getSelectChangeHandler(setTab)} |
| 40 | + label={t('interactions.operation.switch_tab.tab')} |
| 41 | + data={model.views.tabOptions(viewID)} |
| 42 | + comboboxProps={{ |
| 43 | + withinPortal: true, |
| 44 | + zIndex: 340, |
| 45 | + }} |
| 46 | + maxDropdownHeight={500} |
| 47 | + /> |
| 48 | + </Stack> |
| 49 | + ); |
| 50 | +}); |
| 51 | + |
| 52 | +async function run(payload: Record<string, unknown>, operation: IDashboardOperation) { |
| 53 | + const config = await operation.operationData.getItem<ISwitchTabOperationConfig>('config'); |
| 54 | + const { viewID, tab } = config; |
| 55 | + window.dispatchEvent(new CustomEvent('switch_tab', { detail: { viewID, tab } })); |
| 56 | +} |
| 57 | + |
| 58 | +export const SwitchTab: IDashboardOperationSchema = { |
| 59 | + displayName: 'interactions.operation.switch_tab.label', |
| 60 | + id: 'builtin:op:switch_tab', |
| 61 | + configRender: SwitchTabOperationSettings, |
| 62 | + run, |
| 63 | +}; |
0 commit comments