mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-09 07:29:24 +08:00
chore: date time variables
This commit is contained in:
parent
ffdd03b4bc
commit
06f57e3b0f
@ -210,36 +210,17 @@ export const getDateRanges = (props?: {
|
||||
};
|
||||
return {
|
||||
now: () => toString(dayjs()),
|
||||
today: (params?: any) => withParams(toString([getStart(0, 'day'), getEnd(0, 'day')]), params),
|
||||
yesterday: (params?: any) => withParams(toString([getStart(-1, 'day'), getEnd(-1, 'day')]), params),
|
||||
tomorrow: (params?: any) => withParams(toString([getStart(1, 'day'), getEnd(1, 'day')]), params),
|
||||
thisWeek: (params?: any) => withParams(toString([getStart(0, 'isoWeek'), getEnd(0, 'isoWeek')]), params),
|
||||
lastWeek: (params?: any) => withParams(toString([getStart(-1, 'isoWeek'), getEnd(-1, 'isoWeek')]), params),
|
||||
nextWeek: (params?: any) => withParams(toString([getStart(1, 'isoWeek'), getEnd(1, 'isoWeek')]), params),
|
||||
thisIsoWeek: (params?: any) => withParams(toString([getStart(0, 'isoWeek'), getEnd(0, 'isoWeek')]), params),
|
||||
lastIsoWeek: (params?: any) => withParams(toString([getStart(-1, 'isoWeek'), getEnd(-1, 'isoWeek')]), params),
|
||||
nextIsoWeek: (params?: any) => withParams(toString([getStart(1, 'isoWeek'), getEnd(1, 'isoWeek')]), params),
|
||||
thisMonth: (params?: any) => withParams(toString([getStart(0, 'month'), getEnd(0, 'month')]), params),
|
||||
lastMonth: (params?: any) => withParams(toString([getStart(-1, 'month'), getEnd(-1, 'month')]), params),
|
||||
nextMonth: (params?: any) => withParams(toString([getStart(1, 'month'), getEnd(1, 'month')]), params),
|
||||
thisQuarter: (params?: any) => withParams(toString([getStart(0, 'quarter'), getEnd(0, 'quarter')]), params),
|
||||
lastQuarter: (params?: any) => withParams(toString([getStart(-1, 'quarter'), getEnd(-1, 'quarter')]), params),
|
||||
nextQuarter: (params?: any) => withParams(toString([getStart(1, 'quarter'), getEnd(1, 'quarter')]), params),
|
||||
thisYear: (params?: any) => withParams(toString([getStart(0, 'year'), getEnd(0, 'year')]), params),
|
||||
lastYear: (params?: any) => withParams(toString([getStart(-1, 'year'), getEnd(-1, 'year')]), params),
|
||||
nextYear: (params?: any) => withParams(toString([getStart(1, 'year'), getEnd(1, 'year')]), params),
|
||||
last7Days: (params?: any) => withParams(toString([getStart(-6, 'days'), getEnd(0, 'days')]), params),
|
||||
next7Days: (params?: any) => withParams(toString([getStart(1, 'day'), getEnd(7, 'days')]), params),
|
||||
last30Days: (params?: any) => withParams(toString([getStart(-29, 'days'), getEnd(0, 'days')]), params),
|
||||
next30Days: (params?: any) => withParams(toString([getStart(1, 'day'), getEnd(30, 'days')]), params),
|
||||
last90Days: (params?: any) => withParams(toString([getStart(-89, 'days'), getEnd(0, 'days')]), params),
|
||||
next90Days: (params?: any) => withParams(toString([getStart(1, 'day'), getEnd(90, 'days')]), params),
|
||||
today: () => toString(getStart(0, 'day')),
|
||||
yesterday: () => toString(getStart(-1, 'day')),
|
||||
tomorrow: () => toString(getStart(1, 'day')),
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
now: () => dayjs().toISOString(),
|
||||
today: (params?: any) => withParams([getStart(0, 'day'), getEnd(0, 'day')], params),
|
||||
today: (params?: any) => getStart(0, 'day').toISOString(),
|
||||
// today: (params?: any) => withParams([getStart(0, 'day'), getEnd(0, 'day')], params),
|
||||
|
||||
yesterday: (params?: any) => withParams([getStart(-1, 'day'), getEnd(-1, 'day')], params),
|
||||
tomorrow: (params?: any) => withParams([getStart(1, 'day'), getEnd(1, 'day')], params),
|
||||
thisWeek: (params?: any) => withParams([getStart(0, 'isoWeek'), getEnd(0, 'isoWeek')], params),
|
||||
|
@ -216,9 +216,9 @@ export const useDateVariable = ({ operator, schema, noDisabled }: Props) => {
|
||||
* 变量:`日期变量`的上下文
|
||||
* @returns
|
||||
*/
|
||||
export const useDatetimeVariableContext = () => {
|
||||
export const useDatetimeVariableContext = (shouldBeString = false) => {
|
||||
const { utc = true } = useDatePickerContext();
|
||||
const datetimeCtx = useMemo(() => getDateRanges({ shouldBeString: true, utc }), [utc]);
|
||||
const datetimeCtx = useMemo(() => getDateRanges({ shouldBeString, utc }), [utc]);
|
||||
|
||||
return {
|
||||
datetimeCtx,
|
||||
@ -226,6 +226,9 @@ export const useDatetimeVariableContext = () => {
|
||||
};
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
* 该 hook 已废弃,请使用 `useDateTimeVariables` 代替
|
||||
*
|
||||
* 变量:`日期变量`
|
||||
* @param param0
|
||||
* @returns
|
||||
@ -244,163 +247,218 @@ export const useDatetimeVariable = ({ operator, schema, noDisabled, targetFieldS
|
||||
key: 'now',
|
||||
value: 'now',
|
||||
label: t('Current time'),
|
||||
disabled: noDisabled ? false : schema?.['x-component'] !== 'DatePicker' || operatorValue === '$dateBetween',
|
||||
disabled: true,
|
||||
operators: datetime,
|
||||
},
|
||||
{
|
||||
key: 'yesterday',
|
||||
value: 'yesterday',
|
||||
label: t('Yesterday'),
|
||||
disabled,
|
||||
disabled: true,
|
||||
operators: datetime,
|
||||
},
|
||||
{
|
||||
key: 'today',
|
||||
value: 'today',
|
||||
label: t('Today'),
|
||||
disabled,
|
||||
disabled: true,
|
||||
operators: datetime,
|
||||
},
|
||||
{
|
||||
key: 'tomorrow',
|
||||
value: 'tomorrow',
|
||||
label: t('Tomorrow'),
|
||||
disabled,
|
||||
disabled: true,
|
||||
operators: datetime,
|
||||
},
|
||||
{
|
||||
key: 'lastIsoWeek',
|
||||
value: 'lastIsoWeek',
|
||||
label: t('Last week'),
|
||||
disabled,
|
||||
disabled: true,
|
||||
operators: datetime,
|
||||
},
|
||||
{
|
||||
key: 'thisIsoWeek',
|
||||
value: 'thisIsoWeek',
|
||||
label: t('This week'),
|
||||
disabled,
|
||||
disabled: true,
|
||||
operators: datetime,
|
||||
},
|
||||
{
|
||||
key: 'nextIsoWeek',
|
||||
value: 'nextIsoWeek',
|
||||
label: t('Next week'),
|
||||
disabled,
|
||||
disabled: true,
|
||||
operators: datetime,
|
||||
},
|
||||
{
|
||||
key: 'lastMonth',
|
||||
value: 'lastMonth',
|
||||
label: t('Last month'),
|
||||
disabled,
|
||||
disabled: true,
|
||||
operators: datetime,
|
||||
},
|
||||
{
|
||||
key: 'thisMonth',
|
||||
value: 'thisMonth',
|
||||
label: t('This month'),
|
||||
disabled,
|
||||
disabled: true,
|
||||
operators: datetime,
|
||||
},
|
||||
{
|
||||
key: 'nextMonth',
|
||||
value: 'nextMonth',
|
||||
label: t('Next month'),
|
||||
disabled,
|
||||
disabled: true,
|
||||
operators: datetime,
|
||||
},
|
||||
{
|
||||
key: 'lastQuarter',
|
||||
value: 'lastQuarter',
|
||||
label: t('Last quarter'),
|
||||
disabled,
|
||||
disabled: true,
|
||||
operators: datetime,
|
||||
},
|
||||
{
|
||||
key: 'thisQuarter',
|
||||
value: 'thisQuarter',
|
||||
label: t('This quarter'),
|
||||
disabled,
|
||||
disabled: true,
|
||||
operators: datetime,
|
||||
},
|
||||
{
|
||||
key: 'nextQuarter',
|
||||
value: 'nextQuarter',
|
||||
label: t('Next quarter'),
|
||||
disabled,
|
||||
disabled: true,
|
||||
operators: datetime,
|
||||
},
|
||||
{
|
||||
key: 'lastYear',
|
||||
value: 'lastYear',
|
||||
label: t('Last year'),
|
||||
disabled,
|
||||
disabled: true,
|
||||
operators: datetime,
|
||||
},
|
||||
{
|
||||
key: 'thisYear',
|
||||
value: 'thisYear',
|
||||
label: t('This year'),
|
||||
disabled,
|
||||
disabled: true,
|
||||
operators: datetime,
|
||||
},
|
||||
{
|
||||
key: 'nextYear',
|
||||
value: 'nextYear',
|
||||
label: t('Next year'),
|
||||
disabled,
|
||||
disabled: true,
|
||||
operators: datetime,
|
||||
},
|
||||
{
|
||||
key: 'last7Days',
|
||||
value: 'last7Days',
|
||||
label: t('Last 7 days'),
|
||||
disabled,
|
||||
disabled: true,
|
||||
operators: datetime,
|
||||
},
|
||||
{
|
||||
key: 'next7Days',
|
||||
value: 'next7Days',
|
||||
label: t('Next 7 days'),
|
||||
disabled,
|
||||
disabled: true,
|
||||
operators: datetime,
|
||||
},
|
||||
{
|
||||
key: 'last30Days',
|
||||
value: 'last30Days',
|
||||
label: t('Last 30 days'),
|
||||
disabled,
|
||||
disabled: true,
|
||||
operators: datetime,
|
||||
},
|
||||
{
|
||||
key: 'next30Days',
|
||||
value: 'next30Days',
|
||||
label: t('Next 30 days'),
|
||||
disabled,
|
||||
disabled: true,
|
||||
operators: datetime,
|
||||
},
|
||||
{
|
||||
key: 'last90Days',
|
||||
value: 'last90Days',
|
||||
label: t('Last 90 days'),
|
||||
disabled,
|
||||
disabled: true,
|
||||
operators: datetime,
|
||||
},
|
||||
{
|
||||
key: 'next90Days',
|
||||
value: 'next90Days',
|
||||
label: t('Next 90 days'),
|
||||
disabled,
|
||||
disabled: true,
|
||||
operators: datetime,
|
||||
},
|
||||
];
|
||||
|
||||
return {
|
||||
label: t('Date variables(Deprecated)'),
|
||||
value: '$nDate',
|
||||
key: '$nDate',
|
||||
disabled: true,
|
||||
children: dateOptions,
|
||||
};
|
||||
}, [schema?.['x-component'], targetFieldSchema]);
|
||||
|
||||
const { datetimeCtx } = useDatetimeVariableContext(true);
|
||||
|
||||
return {
|
||||
datetimeSettings,
|
||||
datetimeCtx,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* 变量:`日期变量`
|
||||
* @param param0
|
||||
* @returns
|
||||
*/
|
||||
export const useDateTimeVariables = ({ operator, schema, noDisabled, targetFieldSchema }: Props = {}) => {
|
||||
const { t } = useTranslation();
|
||||
const { getOperator } = useOperators();
|
||||
|
||||
const dateTimePresetSettings = useMemo(() => {
|
||||
const operatorValue = operator?.value || getOperator(targetFieldSchema?.name) || '';
|
||||
|
||||
const dateOptions = [
|
||||
{
|
||||
key: 'now',
|
||||
value: 'now',
|
||||
label: t('Current time'),
|
||||
operators: datetime,
|
||||
},
|
||||
{
|
||||
key: 'yesterday',
|
||||
value: 'yesterday',
|
||||
label: t('Yesterday'),
|
||||
operators: datetime,
|
||||
},
|
||||
{
|
||||
key: 'today',
|
||||
value: 'today',
|
||||
label: t('Today'),
|
||||
operators: datetime,
|
||||
},
|
||||
{
|
||||
key: 'tomorrow',
|
||||
value: 'tomorrow',
|
||||
label: t('Tomorrow'),
|
||||
operators: datetime,
|
||||
},
|
||||
];
|
||||
|
||||
return {
|
||||
label: t('Date variables'),
|
||||
value: '$nDate',
|
||||
key: '$nDate',
|
||||
disabled: dateOptions.every((option) => option.disabled),
|
||||
value: '$nDateTime',
|
||||
key: '$nDateTime',
|
||||
children: dateOptions,
|
||||
};
|
||||
}, [schema?.['x-component'], targetFieldSchema]);
|
||||
@ -408,7 +466,7 @@ export const useDatetimeVariable = ({ operator, schema, noDisabled, targetFieldS
|
||||
const { datetimeCtx } = useDatetimeVariableContext();
|
||||
|
||||
return {
|
||||
datetimeSettings,
|
||||
datetimeCtx,
|
||||
dateTimePresetSettings,
|
||||
dateTimePresetCtx: datetimeCtx,
|
||||
};
|
||||
};
|
||||
|
@ -13,7 +13,7 @@ import { useMemo } from 'react';
|
||||
import { useVariables } from '../../../';
|
||||
import { CollectionFieldOptions_deprecated } from '../../../collection-manager';
|
||||
import { useAPITokenVariable } from './useAPITokenVariable';
|
||||
import { useDatetimeVariable } from './useDateVariable';
|
||||
import { useDatetimeVariable, useDateTimeVariables } from './useDateVariable';
|
||||
import { useCurrentFormVariable } from './useFormVariable';
|
||||
import { useCurrentObjectVariable } from './useIterationVariable';
|
||||
import { useParentObjectVariable } from './useParentIterationVariable';
|
||||
@ -77,6 +77,7 @@ export const useVariableOptions = ({
|
||||
});
|
||||
const { apiTokenSettings } = useAPITokenVariable({ noDisabled });
|
||||
const { datetimeSettings } = useDatetimeVariable({ operator, schema: uiSchema, noDisabled: true, targetFieldSchema });
|
||||
const { dateTimePresetSettings } = useDateTimeVariables();
|
||||
const { currentFormSettings, shouldDisplayCurrentForm } = useCurrentFormVariable({
|
||||
schema: uiSchema,
|
||||
collectionField,
|
||||
@ -128,6 +129,7 @@ export const useVariableOptions = ({
|
||||
currentRoleSettings,
|
||||
apiTokenSettings,
|
||||
datetimeSettings,
|
||||
dateTimePresetSettings,
|
||||
shouldDisplayCurrentForm && currentFormSettings,
|
||||
shouldDisplayCurrentObject && currentObjectSettings,
|
||||
shouldDisplayParentObject && parentObjectSettings,
|
||||
|
@ -10,7 +10,7 @@
|
||||
import { dayjs } from '@nocobase/utils/client';
|
||||
import { useMemo } from 'react';
|
||||
import { DEFAULT_DATA_SOURCE_KEY } from '../../data-source/data-source/DataSourceManager';
|
||||
import { useCurrentUserVariable, useDatetimeVariable } from '../../schema-settings';
|
||||
import { useCurrentUserVariable, useDatetimeVariable, useDateTimeVariables } from '../../schema-settings';
|
||||
import { useAPITokenVariable } from '../../schema-settings/VariableInput/hooks/useAPITokenVariable';
|
||||
import { useCurrentRoleVariable } from '../../schema-settings/VariableInput/hooks/useRoleVariable';
|
||||
import { useURLSearchParamsVariable } from '../../schema-settings/VariableInput/hooks/useURLSearchParamsVariable';
|
||||
@ -26,6 +26,8 @@ const useBuiltInVariables = () => {
|
||||
const { currentRoleCtx } = useCurrentRoleVariable();
|
||||
const { apiTokenCtx } = useAPITokenVariable();
|
||||
const { datetimeCtx } = useDatetimeVariable();
|
||||
const { dateTimePresetCtx } = useDateTimeVariables();
|
||||
|
||||
const { urlSearchParamsCtx, name: urlSearchParamsName, defaultValue } = useURLSearchParamsVariable();
|
||||
const envVariableCtx = useGlobalVariableCtx('$env');
|
||||
const builtinVariables: VariableOption[] = useMemo(() => {
|
||||
@ -60,6 +62,10 @@ const useBuiltInVariables = () => {
|
||||
name: '$nDate',
|
||||
ctx: datetimeCtx,
|
||||
},
|
||||
{
|
||||
name: '$nDateTime',
|
||||
ctx: dateTimePresetCtx,
|
||||
},
|
||||
/**
|
||||
* @deprecated
|
||||
* 兼容旧版本的 `$date` 变量,新版本已弃用
|
||||
@ -68,6 +74,7 @@ const useBuiltInVariables = () => {
|
||||
name: '$date',
|
||||
ctx: datetimeCtx,
|
||||
},
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
* 兼容旧版本的 `$system` 变量,新版本已弃用
|
||||
|
Loading…
x
Reference in New Issue
Block a user