Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<script lang="ts" setup>
import type { DeepReadonly } from 'vue'
import { computed, ref, shallowRef, watchEffect } from 'vue'
import { getCreatedDate } from '/@/lib/basic/date'
import { getDateRepresentation } from '/@/lib/basic/date'
import type { MessageId } from '/@/types/entity-ids'
import useChannelPath from '/@/composables/useChannelPath'
import type { Message } from '@traptitech/traq'
Expand Down Expand Up @@ -92,7 +92,7 @@ const date = computed(() => {
} else {
_date = props.message.updatedAt
}
return getCreatedDate(_date)
return getDateRepresentation(_date)
})
const { fetchFileMetaData } = useMessagesStore()

Expand Down
7 changes: 5 additions & 2 deletions src/components/Main/MainView/MessageElement/MessageHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ import GradeBadge from './GradeBadge.vue'
import AIcon from '/@/components/UI/AIcon.vue'
import { computed } from 'vue'
import type { UserId } from '/@/types/entity-ids'
import { getDisplayDate, getFullDayWithTimeString } from '/@/lib/basic/date'
import {
getDateRepresentation,
getFullDayWithTimeString
} from '/@/lib/basic/date'
import { useUsersStore } from '/@/store/entities/users'

const props = defineProps<{
Expand All @@ -49,7 +52,7 @@ if (user.value === undefined) {
const createdDate = computed(() =>
getFullDayWithTimeString(new Date(props.createdAt))
)
const date = computed(() => getDisplayDate(props.createdAt, props.updatedAt))
const date = computed(() => getDateRepresentation(props.updatedAt))
</script>

<style lang="scss" module>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import { computed } from 'vue'
import type { Message } from '@traptitech/traq'
import useChannelPath from '/@/composables/useChannelPath'
import { getCreatedDate } from '/@/lib/basic/date'
import { getDateRepresentation } from '/@/lib/basic/date'
import { constructMessagesPath } from '/@/router'

const props = defineProps<{
Expand All @@ -32,7 +32,7 @@ const channelLink = computed(() =>
props.message ? channelIdToLink(props.message.channelId) : ''
)
const date = computed(() =>
props.message ? getCreatedDate(props.message.createdAt) : ''
props.message ? getDateRepresentation(props.message.createdAt) : ''
)
const messageLink = computed(() =>
props.message ? constructMessagesPath(props.message.id) : ''
Expand Down
4 changes: 2 additions & 2 deletions src/components/Modal/FileModal/FileModalContentFooter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import FileModalContentFooterUsername from './FileModalContentFooterUsername.vue
import { computed } from 'vue'
import useFileMeta from '/@/composables/files/useFileMeta'
import useChannelPath from '/@/composables/useChannelPath'
import { getCreatedDate } from '/@/lib/basic/date'
import { getDateRepresentation } from '/@/lib/basic/date'
import { useOpenLinkAndClearModal } from '../composables/useOpenLinkFromModal'
import { useUsersStore } from '/@/store/entities/users'

Expand All @@ -39,7 +39,7 @@ const user = computed(() =>
usersMap.value.get(fileMeta.value?.uploaderId ?? '')
)
const createdAt = computed(() =>
getCreatedDate(fileMeta.value?.createdAt ?? '')
getDateRepresentation(fileMeta.value?.createdAt ?? '')
)

const { channelIdToPathString, channelIdToLink } = useChannelPath()
Expand Down
13 changes: 5 additions & 8 deletions src/lib/basic/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ export const getDateRepresentationWithoutSameDate = (
return timeString
}

export const getDisplayDate = (createdAt: string, updatedAt: string) => {
const displayDate = new Date(updatedAt)
export const getDateRepresentation = (date: Readonly<Date> | string) => {
const displayDate = new Date(date)
if (Number.isNaN(displayDate.getTime())) {
return ''
}
const today = new Date()
const timeString = getTimeString(displayDate)
const yesterday = new Date(today.getTime() - 1000 * 60 * 60 * 24)
Expand All @@ -68,12 +71,6 @@ export const getDisplayDate = (createdAt: string, updatedAt: string) => {
}
}

export const getCreatedDate = (createdAt: string) => {
const createdDate = new Date(createdAt)
const now = new Date()
return getDateRepresentationWithoutSameDate(createdDate, now)
}

export const compareDate = (date1: Date, date2: Date, inverse = false) => {
const _inv = inverse ? -1 : 1
const _t1 = date1.getTime()
Expand Down
51 changes: 6 additions & 45 deletions tests/unit/lib/basic/date.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import {
getFullDayString,
getFullDayWithTimeString,
getISOFullDayString,
getDateRepresentationWithoutSameDate,
getDisplayDate,
getDateRepresentation,
compareDate,
compareDateString,
getCreatedDate,
getCurrentTimeString
} from '/@/lib/basic/date'

Expand Down Expand Up @@ -69,30 +67,6 @@ describe('getCurrentTimeString', () => {
})
})

describe('getDateRepresentationWithoutSameDate', () => {
const date = new Date(defaultDate1.getTime())

it('can get time string', () => {
const date2 = new Date(defaultDate1.getTime())
date2.setMinutes(date2.getMinutes() + 2)
expect(getDateRepresentationWithoutSameDate(date2, date)).toBe('15:23')
})
it('can get date with time string', () => {
const date2 = new Date(defaultDate1.getTime())
date2.setDate(date2.getDate() + 2)
expect(getDateRepresentationWithoutSameDate(date2, date)).toBe(
'12/06 15:21'
)
})
it('can get date with time string and year', () => {
const date2 = new Date(defaultDate1.getTime())
date2.setFullYear(date2.getFullYear() + 2)
expect(getDateRepresentationWithoutSameDate(date2, date)).toBe(
'1987/12/04 15:21'
)
})
})

describe('getDisplayDate', () => {
beforeEach(() => {
vi.useFakeTimers()
Expand All @@ -101,36 +75,23 @@ describe('getDisplayDate', () => {
vi.useRealTimers()
})

const createdDateISO = '2010-04-01T12:34:56'
const updatedDateISO = '2010-05-02T14:28:57'
const dateISO = '2010-05-02T14:28:57'

it('should say 今日 when updated today', () => {
vi.setSystemTime('2010-05-02T15:00:00')
expect(getDisplayDate(createdDateISO, updatedDateISO)).toBe('今日 14:28')
expect(getDateRepresentation(dateISO)).toBe('今日 14:28')
})
it('should say 昨日 when updated yesterday', () => {
vi.setSystemTime('2010-05-03T15:00:00')
expect(getDisplayDate(createdDateISO, updatedDateISO)).toBe('昨日 14:28')
expect(getDateRepresentation(dateISO)).toBe('昨日 14:28')
})
it('should get MM/DD when updated in the same year', () => {
vi.setSystemTime('2010-07-07T15:00:00')
expect(getDisplayDate(createdDateISO, updatedDateISO)).toBe('05/02 14:28')
expect(getDateRepresentation(dateISO)).toBe('05/02 14:28')
})
it('should get YYYY/MM/DD when updated before last year', () => {
vi.setSystemTime('2015-10-10T15:00:00')
expect(getDisplayDate(createdDateISO, updatedDateISO)).toBe(
'2010/05/02 14:28'
)
})
})

describe('getCreatedDate', () => {
const today = new Date()
today.setHours(1)
today.setMinutes(23)

it('can get', () => {
expect(getCreatedDate(today.toISOString())).toBe('01:23')
expect(getDateRepresentation(dateISO)).toBe('2010/05/02 14:28')
})
})

Expand Down
Loading