main

mattermost/focalboard

Last updated at: 29/12/2023 09:52

property.tsx

TLDR

This file contains a class called DateProperty which extends DatePropertyType. It also exports a function called timeZoneOffset and imports various dependencies. The DateProperty class defines methods for displaying and manipulating date values, as well as defining the properties of a date property.

Classes

DateProperty

This class extends the DatePropertyType class and represents a date property. It has the following properties and methods:

  • Editor: The component used to edit the date property.
  • name: The name of the date property.
  • type: The type of the date property.
  • displayName: A function that returns the translated display name of the date property.
  • calculationOptions: An array of options for calculating values based on the date property.
  • displayValue: A function that returns the display value of the date property.
  • getDateFrom: A function that returns the "from" date value of the date property.
  • getDateTo: A function that returns the "to" date value of the date property.

Methods

timeZoneOffset

This function takes a date parameter and returns the time zone offset in milliseconds.

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {IntlShape} from 'react-intl'
import {DateUtils} from 'react-day-picker'

import {Options} from '../../components/calculations/options'
import {IPropertyTemplate} from '../../blocks/board'
import {Card} from '../../blocks/card'
import {Utils} from '../../utils'

import {PropertyTypeEnum, DatePropertyType} from '../types'

import DateComponent, {createDatePropertyFromString} from './date'

const timeZoneOffset = (date: number): number => {
    return new Date(date).getTimezoneOffset() * 60 * 1000
}

export default class DateProperty extends DatePropertyType {
    Editor = DateComponent
    name = 'Date'
    type = 'date' as PropertyTypeEnum
    displayName = (intl: IntlShape) => intl.formatMessage({id: 'PropertyType.Date', defaultMessage: 'Date'})
    calculationOptions = [Options.none, Options.count, Options.countEmpty,
        Options.countNotEmpty, Options.percentEmpty, Options.percentNotEmpty,
        Options.countValue, Options.countUniqueValue]
    displayValue = (propertyValue: string | string[] | undefined, _1: Card, _2: IPropertyTemplate, intl: IntlShape) => {
        let displayValue = ''
        if (propertyValue && typeof propertyValue === 'string') {
            const singleDate = new Date(parseInt(propertyValue, 10))
            if (singleDate && DateUtils.isDate(singleDate)) {
                displayValue = Utils.displayDate(new Date(parseInt(propertyValue, 10)), intl)
            } else {
                try {
                    const dateValue = JSON.parse(propertyValue as string)
                    if (dateValue.from) {
                        displayValue = Utils.displayDate(new Date(dateValue.from), intl)
                    }
                    if (dateValue.to) {
                        displayValue += ' -> '
                        displayValue += Utils.displayDate(new Date(dateValue.to), intl)
                    }
                } catch {
                    // do nothing
                }
            }
        }
        return displayValue
    }

    getDateFrom = (value: string | string[] | undefined) => {
        const dateProperty = createDatePropertyFromString(value as string)
        if (!dateProperty.from) {
            return undefined
        }

        // date properties are stored as 12 pm UTC, convert to 12 am (00) UTC for calendar
        const dateFrom = dateProperty.from ? new Date(dateProperty.from + (dateProperty.includeTime ? 0 : timeZoneOffset(dateProperty.from))) : new Date()
        dateFrom.setHours(0, 0, 0, 0)
        return dateFrom
    }

    getDateTo = (value: string | string[] | undefined) => {
        const dateProperty = createDatePropertyFromString(value as string)
        if (!dateProperty.to) {
            return undefined
        }
        const dateToNumber = dateProperty.to + (dateProperty.includeTime ? 0 : timeZoneOffset(dateProperty.to))
        const dateTo = new Date(dateToNumber)
        dateTo.setHours(0, 0, 0, 0)
        return dateTo
    }
}