Browse Source

refactor(nc-gui): move common datetime utils functions to sdk

pull/6870/head
աɨռɢӄաօռɢ 1 year ago
parent
commit
9bc12eaee3
  1. 2
      packages/nc-gui/components/cell/DatePicker.vue
  2. 4
      packages/nc-gui/components/cell/DateTimePicker.vue
  3. 3
      packages/nc-gui/components/dashboard/settings/AuditTab.vue
  4. 3
      packages/nc-gui/components/dashboard/settings/BaseAudit.vue
  5. 2
      packages/nc-gui/components/notification/Item/Wrapper.vue
  6. 11
      packages/nc-gui/components/project/AccessSettings.vue
  7. 3
      packages/nc-gui/components/smartsheet/column/DateOptions.vue
  8. 3
      packages/nc-gui/components/smartsheet/column/DateTimeOptions.vue
  9. 3
      packages/nc-gui/components/smartsheet/column/FormulaOptions.vue
  10. 3
      packages/nc-gui/components/smartsheet/expanded-form/Comments.vue
  11. 5
      packages/nc-gui/components/template/Editor.vue
  12. 3
      packages/nc-gui/components/virtual-cell/Formula.vue
  13. 3
      packages/nc-gui/components/webhook/CallLog.vue
  14. 4
      packages/nc-gui/components/workspace/CollaboratorsList.vue
  15. 3
      packages/nc-gui/components/workspace/ProjectList.vue
  16. 5
      packages/nc-gui/composables/useMultiSelect/index.ts
  17. 3
      packages/nc-gui/helpers/parsers/CSVTemplateAdapter.ts
  18. 3
      packages/nc-gui/helpers/parsers/ExcelTemplateAdapter.ts
  19. 94
      packages/nc-gui/utils/dateTimeUtils.ts
  20. 1
      packages/nc-gui/utils/index.ts

2
packages/nc-gui/components/cell/DatePicker.vue

@ -1,5 +1,6 @@
<script setup lang="ts">
import dayjs from 'dayjs'
import { isDateMonthFormat } from 'nocodb-sdk'
import {
ActiveCellInj,
CellClickHookInj,
@ -10,7 +11,6 @@ import {
ReadonlyInj,
computed,
inject,
isDateMonthFormat,
isDrawerOrModalExist,
onClickOutside,
onMounted,

4
packages/nc-gui/components/cell/DateTimePicker.vue

@ -1,18 +1,16 @@
<script setup lang="ts">
import dayjs from 'dayjs'
import { isSystemColumn } from 'nocodb-sdk'
import { dateFormats, isSystemColumn, timeFormats } from 'nocodb-sdk'
import {
ActiveCellInj,
CellClickHookInj,
ColumnInj,
EditColumnInj,
ReadonlyInj,
dateFormats,
inject,
isDrawerOrModalExist,
parseProp,
ref,
timeFormats,
useBase,
useSelectedCellKeyupListener,
watch,

3
packages/nc-gui/components/dashboard/settings/AuditTab.vue

@ -1,7 +1,8 @@
<script setup lang="ts">
import { Tooltip as ATooltip, Empty } from 'ant-design-vue'
import type { AuditType } from 'nocodb-sdk'
import { ProjectIdInj, h, iconMap, onMounted, storeToRefs, timeAgo, useBase, useGlobal, useI18n, useNuxtApp } from '#imports'
import { timeAgo } from 'nocodb-sdk'
import { ProjectIdInj, h, iconMap, onMounted, storeToRefs, useBase, useGlobal, useI18n, useNuxtApp } from '#imports'
const { $api } = useNuxtApp()

3
packages/nc-gui/components/dashboard/settings/BaseAudit.vue

@ -1,7 +1,8 @@
<script setup lang="ts">
import { Tooltip as ATooltip, Empty } from 'ant-design-vue'
import type { AuditType } from 'nocodb-sdk'
import { h, iconMap, onMounted, storeToRefs, timeAgo, useBase, useGlobal, useI18n, useNuxtApp } from '#imports'
import { timeAgo } from 'nocodb-sdk'
import { h, iconMap, onMounted, storeToRefs, useBase, useGlobal, useI18n, useNuxtApp } from '#imports'
interface Props {
sourceId: string

2
packages/nc-gui/components/notification/Item/Wrapper.vue

@ -1,5 +1,5 @@
<script setup lang="ts">
import { timeAgo } from '#imports'
import { timeAgo } from 'nocodb-sdk'
const props = defineProps<{
item: {

11
packages/nc-gui/components/project/AccessSettings.vue

@ -1,8 +1,15 @@
<script lang="ts" setup>
import {
OrderedProjectRoles,
OrgUserRoles,
ProjectRoles,
WorkspaceRolesToProjectRoles,
extractRolesObj,
timeAgo,
} from 'nocodb-sdk'
import type { WorkspaceUserRoles } from 'nocodb-sdk'
import { OrderedProjectRoles, OrgUserRoles, ProjectRoles, WorkspaceRolesToProjectRoles, extractRolesObj } from 'nocodb-sdk'
import InfiniteLoading from 'v3-infinite-loading'
import { isEeUI, storeToRefs, timeAgo } from '#imports'
import { isEeUI, storeToRefs } from '#imports'
const basesStore = useBases()
const { getProjectUsers, createProjectUser, updateProjectUser, removeProjectUser } = basesStore

3
packages/nc-gui/components/smartsheet/column/DateOptions.vue

@ -1,5 +1,6 @@
<script setup lang="ts">
import { dateFormats, useVModel } from '#imports'
import { dateFormats } from 'nocodb-sdk'
import { useVModel } from '#imports'
const props = defineProps<{
value: any

3
packages/nc-gui/components/smartsheet/column/DateTimeOptions.vue

@ -1,5 +1,6 @@
<script setup lang="ts">
import { dateFormats, timeFormats, useVModel } from '#imports'
import { dateFormats, timeFormats } from 'nocodb-sdk'
import { useVModel } from '#imports'
const props = defineProps<{
value: any

3
packages/nc-gui/components/smartsheet/column/FormulaOptions.vue

@ -3,7 +3,7 @@ import type { Ref } from 'vue'
import type { ListItem as AntListItem } from 'ant-design-vue'
import jsep from 'jsep'
import type { ColumnType, FormulaType } from 'nocodb-sdk'
import { UITypes, jsepCurlyHook, substituteColumnIdWithAliasInFormula } from 'nocodb-sdk'
import { UITypes, jsepCurlyHook, substituteColumnIdWithAliasInFormula, validateDateWithUnknownFormat } from 'nocodb-sdk'
import {
MetaInj,
NcAutocompleteTree,
@ -18,7 +18,6 @@ import {
useColumnCreateStoreOrThrow,
useDebounceFn,
useVModel,
validateDateWithUnknownFormat,
} from '#imports'
const props = defineProps<{

3
packages/nc-gui/components/smartsheet/expanded-form/Comments.vue

@ -1,8 +1,9 @@
<script setup lang="ts">
import type { VNodeRef } from '@vue/runtime-core'
import type { AuditType } from 'nocodb-sdk'
import { timeAgo } from 'nocodb-sdk'
import { Icon } from '@iconify/vue'
import { ref, timeAgo, useExpandedFormStoreOrThrow, useGlobal, useRoles, watch } from '#imports'
import { ref, useExpandedFormStoreOrThrow, useGlobal, useRoles, watch } from '#imports'
const props = defineProps<{
isLoading: boolean

5
packages/nc-gui/components/template/Editor.vue

@ -2,7 +2,7 @@
import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'
import type { ColumnType, TableType } from 'nocodb-sdk'
import { UITypes, isSystemColumn, isVirtualCol } from 'nocodb-sdk'
import { UITypes, getDateFormat, getDateTimeFormat, isSystemColumn, isVirtualCol, parseStringDate } from 'nocodb-sdk'
import type { CheckboxChangeEvent } from 'ant-design-vue/es/checkbox/interface'
import { srcDestMappingColumns, tableColumns } from './utils'
import {
@ -18,15 +18,12 @@ import {
extractSdkResponseErrorMsg,
fieldLengthValidator,
fieldRequiredValidator,
getDateFormat,
getDateTimeFormat,
getUIDTIcon,
iconMap,
inject,
message,
nextTick,
onMounted,
parseStringDate,
reactive,
ref,
storeToRefs,

3
packages/nc-gui/components/virtual-cell/Formula.vue

@ -1,7 +1,8 @@
<script lang="ts" setup>
import { handleTZ } from 'nocodb-sdk'
import type { ColumnType } from 'nocodb-sdk'
import type { Ref } from 'vue'
import { CellValueInj, ColumnInj, computed, handleTZ, inject, renderValue, replaceUrlsWithLink, useBase } from '#imports'
import { CellValueInj, ColumnInj, computed, inject, renderValue, replaceUrlsWithLink, useBase } from '#imports'
// todo: column type doesn't have required property `error` - throws in typecheck
const column = inject(ColumnInj) as Ref<ColumnType & { colOptions: { error: any } }>

3
packages/nc-gui/components/webhook/CallLog.vue

@ -1,6 +1,7 @@
<script setup lang="ts">
import type { HookLogType, HookType } from 'nocodb-sdk'
import { AutomationLogLevel, extractSdkResponseErrorMsg, onBeforeMount, parseProp, timeAgo, useApi, useGlobal } from '#imports'
import { timeAgo } from 'nocodb-sdk'
import { AutomationLogLevel, extractSdkResponseErrorMsg, onBeforeMount, parseProp, useApi, useGlobal } from '#imports'
interface Props {
hook: HookType

4
packages/nc-gui/components/workspace/CollaboratorsList.vue

@ -1,6 +1,6 @@
<script lang="ts" setup>
import { OrderedWorkspaceRoles, WorkspaceUserRoles } from 'nocodb-sdk'
import { storeToRefs, timeAgo, useWorkspace } from '#imports'
import { OrderedWorkspaceRoles, WorkspaceUserRoles, timeAgo } from 'nocodb-sdk'
import { storeToRefs, useWorkspace } from '#imports'
const { workspaceRoles, loadRoles } = useRoles()

3
packages/nc-gui/components/workspace/ProjectList.vue

@ -1,7 +1,7 @@
<script lang="ts" setup>
import { Empty } from 'ant-design-vue'
import type { BaseType } from 'nocodb-sdk'
import { ProjectRoles, ProjectStatus, WorkspaceUserRoles } from 'nocodb-sdk'
import { ProjectRoles, ProjectStatus, WorkspaceUserRoles, timeAgo } from 'nocodb-sdk'
import { nextTick } from '@vue/runtime-core'
import {
NcProjectType,
@ -12,7 +12,6 @@ import {
navigateTo,
ref,
storeToRefs,
timeAgo,
useBases,
useGlobal,
useRoles,

5
packages/nc-gui/composables/useMultiSelect/index.ts

@ -3,17 +3,15 @@ import dayjs from 'dayjs'
import type { Ref } from 'vue'
import type { MaybeRef } from '@vueuse/core'
import type { ColumnType, LinkToAnotherRecordType, TableType } from 'nocodb-sdk'
import { RelationTypes, UITypes, isSystemColumn, isVirtualCol } from 'nocodb-sdk'
import { RelationTypes, UITypes, dateFormats, isDateMonthFormat, isSystemColumn, isVirtualCol, timeFormats } from 'nocodb-sdk'
import { parse } from 'papaparse'
import type { Cell } from './cellRange'
import { CellRange } from './cellRange'
import convertCellData from './convertCellData'
import type { Nullable, Row } from '#imports'
import {
dateFormats,
extractPkFromRow,
extractSdkResponseErrorMsg,
isDateMonthFormat,
isDrawerOrModalExist,
isExpandedCellInputExist,
isMac,
@ -21,7 +19,6 @@ import {
message,
reactive,
ref,
timeFormats,
unref,
useBase,
useCopy,

3
packages/nc-gui/helpers/parsers/CSVTemplateAdapter.ts

@ -1,7 +1,6 @@
import { parse } from 'papaparse'
import type { UploadFile } from 'ant-design-vue'
import { UITypes } from 'nocodb-sdk'
import { getDateFormat, validateDateWithUnknownFormat } from '../../utils/dateTimeUtils'
import { UITypes, getDateFormat, validateDateWithUnknownFormat } from 'nocodb-sdk'
import {
extractMultiOrSingleSelectProps,
getCheckboxValue,

3
packages/nc-gui/helpers/parsers/ExcelTemplateAdapter.ts

@ -1,5 +1,4 @@
import { UITypes } from 'nocodb-sdk'
import { getDateFormat } from '../../utils/dateTimeUtils'
import { UITypes, getDateFormat } from 'nocodb-sdk'
import TemplateGenerator from './TemplateGenerator'
import {
extractMultiOrSingleSelectProps,

94
packages/nc-gui/utils/dateTimeUtils.ts

@ -1,94 +0,0 @@
import dayjs from 'dayjs'
export const timeAgo = (date: any) => {
if (/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(date)) {
// if there is no timezone info, consider as UTC
// e.g. 2023-01-01 08:00:00 (MySQL)
date += '+00:00'
}
// show in local time
return dayjs(date).fromNow()
}
export const dateMonthFormats = ['YYYY-MM', 'YYYY MM']
export const isDateMonthFormat = (format: string) => dateMonthFormats.includes(format)
export const dateFormats = [
'YYYY-MM-DD',
'YYYY/MM/DD',
'DD-MM-YYYY',
'MM-DD-YYYY',
'DD/MM/YYYY',
'MM/DD/YYYY',
'DD MM YYYY',
'MM DD YYYY',
'YYYY MM DD',
...dateMonthFormats,
]
export const timeFormats = ['HH:mm', 'HH:mm:ss']
export const handleTZ = (val: any) => {
if (val === undefined || val === null) {
return
}
if (typeof val !== 'string') {
return val
}
return val.replace(
/((?:-?(?:[1-9][0-9]*)?[0-9]{4})-(?:1[0-2]|0[1-9])-(?:3[01]|0[1-9]|[12][0-9])T(?:2[0-3]|[01][0-9]):(?:[0-5][0-9]):(?:[0-5][0-9])(?:\.[0-9]+)?(?:Z|[+-](?:2[0-3]|[01][0-9]):[0-5][0-9]))/g,
(i, v) => {
return dayjs(v).format('YYYY-MM-DD HH:mm')
},
)
}
export function validateDateFormat(v: string) {
return dateFormats.includes(v)
}
export function validateDateWithUnknownFormat(v: string) {
for (const format of dateFormats) {
if (dayjs(v, format, true).isValid() as any) {
return true
}
for (const timeFormat of ['HH:mm', 'HH:mm:ss', 'HH:mm:ss.SSS']) {
if (dayjs(v, `${format} ${timeFormat}`, true).isValid() as any) {
return true
}
}
}
return false
}
export function getDateFormat(v: string) {
for (const format of dateFormats) {
if (dayjs(v, format, true).isValid()) {
return format
}
}
return 'YYYY/MM/DD'
}
export function getDateTimeFormat(v: string) {
for (const format of dateFormats) {
for (const timeFormat of timeFormats) {
const dateTimeFormat = `${format} ${timeFormat}`
if (dayjs(v, dateTimeFormat, true).isValid() as any) {
return dateTimeFormat
}
}
}
return 'YYYY/MM/DD'
}
export function parseStringDate(v: string, dateFormat: string) {
const dayjsObj = dayjs(v)
if (dayjsObj.isValid()) {
v = dayjsObj.format('YYYY-MM-DD')
} else {
v = dayjs(v, dateFormat).format('YYYY-MM-DD')
}
return v
}

1
packages/nc-gui/utils/index.ts

@ -1,6 +1,5 @@
export * from './NcAutocompleteTree'
export * from './colorsUtils'
export * from './dateTimeUtils'
export * from './deepCompare'
export * from './formulaUtils'
export * from './durationUtils'

Loading…
Cancel
Save