Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

引用の日付をメッセージのものと合わせるように #4385

Merged
merged 4 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 { getDisplayDate } 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 getDisplayDate(_date)
})
const { fetchFileMetaData } = useMessagesStore()

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 { getDisplayDate } 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 ? getDisplayDate(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 { getDisplayDate } 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 ?? '')
getDisplayDate(fileMeta.value?.createdAt ?? '')
)

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

export const getDateRepresentation = (date: string) => {
const displayDate = new Date(date)
export const getDateRepresentation = (date: Readonly<Date>) => {
const displayDate = date
nokhnaton marked this conversation as resolved.
Show resolved Hide resolved
const today = new Date()
const timeString = getTimeString(displayDate)
const yesterday = new Date(today.getTime() - 1000 * 60 * 60 * 24)
Expand All @@ -68,12 +68,16 @@ export const getDateRepresentation = (date: string) => {
}
}

export const getDisplayDate = (createdAt: string, updatedAt: string) => {
return getDateRepresentation(updatedAt)
}
export const getDisplayDate = (createdAt: string, updatedAt?: string) => {
const createdAtUnix = Date.parse(createdAt)
const updatedAtUnix = Date.parse(updatedAt ?? '')
if (!Number.isNaN(updatedAtUnix)) {
return getDateRepresentation(new Date(updatedAtUnix))
}

export const getCreatedDate = (createdAt: string) => {
return getDateRepresentation(createdAt)
return Number.isNaN(createdAtUnix)
? ''
: getDateRepresentation(new Date(createdAtUnix))
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

現状はcreatedAtとupdatedAtのうち片方しか使われない(制御結合)形になっているんですが、↓のようにどちらを使うかは外側で決めて変換には必要な日付のみを渡す(データ結合)方がモジュール結合度が低くて運用しやすいんじゃないかなって思います
つまりはgetDisplayDateは消してgetDateRepresentationを広く使いたいねって話です(関数名はgetDisplayDateでもいいんですが)

const date = computed(() => {
let _date: string
if (
props.currentSortKey === 'createdAt' ||
props.currentSortKey === '-createdAt'
) {
_date = props.message.createdAt
} else {
_date = props.message.updatedAt
}
return getDisplayDate(_date)
})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あー、確かにこんな感じの使い方がありえるならgetDateRepresentationを広く使う方が良さそうですね


export const compareDate = (date1: Date, date2: Date, inverse = false) => {
Expand Down
38 changes: 3 additions & 35 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,
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 Down Expand Up @@ -122,15 +96,9 @@ describe('getDisplayDate', () => {
'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')
it('with one parameter', () => {
vi.setSystemTime('2010-04-01T12:00:00')
expect(getDisplayDate(createdDateISO)).toBe('今日 12:34')
})
})

Expand Down
Loading