Skip to content

Commit

Permalink
fix: Entryオブジェクトの時刻はタイムゾーンを持つ文字列にする/dayjs().tz()の強制
Browse files Browse the repository at this point in the history
  • Loading branch information
kyonenya committed Jul 4, 2023
1 parent 3f8928f commit 0301dd2
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/ListItem.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { StarIcon } from '@heroicons/react/24/solid';
import dayjs from 'dayjs';
import Link from 'next/link';
import { generateSummaryEntity, SummaryEntity } from 'search-summary';
import { twMerge } from 'tailwind-merge';
import { Entry } from '../domain/Entry';
import { SearchQuery } from '../domain/SearchQuery';
import dayjs from '../infra/dayjs';
import { Skelton } from './_components/Skelton';
import { Tags } from './_components/Tags';

Expand Down
4 changes: 2 additions & 2 deletions app/Preview.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { StarIcon } from '@heroicons/react/24/solid';
import dayjs from 'dayjs';
import { Entry } from '../domain/Entry';
import dayjs from '../infra/dayjs';
import { MarkdownText } from './_components/MarkdownText';

export const Preview = (props: { entry: Entry }) => {
return (
<div className="mx-auto max-w-3xl px-10 pt-10 text-gray-700 dark:bg-gray-800 dark:text-gray-300">
<div className="mb-3 flex flex-row items-center space-x-3">
<p className="text-xl text-gray-500 dark:text-gray-400">
{dayjs(props.entry.createdAt).format('YYYY-MM-DD HH:mm')}
{dayjs(props.entry.createdAt).tz().format('YYYY-MM-DD HH:mm')}
</p>

{props.entry.starred && (
Expand Down
4 changes: 2 additions & 2 deletions app/[uuid]/Article.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dayjs from 'dayjs';
import { PropsWithChildren } from 'react';
import { Entry } from '../../domain/Entry';
import dayjs from '../../infra/dayjs';
import { MarkdownText } from '../_components/MarkdownText';
import { Skelton } from '../_components/Skelton';
import { Tags } from '../_components/Tags';
Expand All @@ -19,7 +19,7 @@ export const Article = (props: { entry: Entry }) => {
<MarkdownText>{props.entry.text}</MarkdownText>
<div className="mt-3 flex flex-row space-x-4">
<p className="text-gray-600 dark:text-gray-400">
{dayjs(props.entry.createdAt).format('YYYY-MM-DD')}
{dayjs(props.entry.createdAt).tz().format('YYYY-MM-DD')}
</p>
<Tags tags={props.entry.tags} />
</div>
Expand Down
4 changes: 2 additions & 2 deletions app/[uuid]/ArticleHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const ArticleHeader = ({
const router = useRouter();
const { register, setValue, control, handleSubmit } = useForm<Form>({
defaultValues: {
createdAt: dayjs(entry.createdAt).format('YYYY-MM-DDTHH:mm'),
createdAt: dayjs(entry.createdAt).tz().format('YYYY-MM-DDTHH:mm'),
tags: entry.tags,
starred: entry.starred,
},
Expand Down Expand Up @@ -102,7 +102,7 @@ export const ArticleHeader = ({
<Popover
triggerButton={
<Button rightIcon={<ChevronDownIcon />} className="w-auto">
{dayjs(entry.createdAt).format('YYYY-MM-DD')}
{dayjs(entry.createdAt).tz().format('YYYY-MM-DD')}
</Button>
}
side="bottom"
Expand Down
9 changes: 5 additions & 4 deletions domain/Entry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import crypto from 'crypto';
import dayjs, { Dayjs } from 'dayjs';
import { Dayjs } from 'dayjs';
import dayjs from '../infra/dayjs';

export type Entry = {
text: string;
Expand All @@ -23,8 +24,8 @@ export const newEntry = (props: {
starred: props.starred ?? false,
uuid: props.uuid ?? crypto.randomUUID().replace(/-/g, '').toUpperCase(),
tags: props.tags ?? [],
createdAt: dayjs(props.createdAt).format(), // current time if empty
modifiedAt: dayjs(props.modifiedAt).format(),
createdAt: dayjs(props.createdAt).tz().format(), // with timezone
modifiedAt: dayjs(props.modifiedAt).tz().format(),
};
};

Expand All @@ -33,7 +34,7 @@ export const extractTagHistory = (posts: Entry[]): string[] => [
]; // uniq

// 基準になる日時
const baseDate = dayjs('2023-06-23T02:25:00+09:00');
const baseDate = dayjs.tz('2023-06-23T02:25');

export const sampleEntries: Entry[] = [
newEntry({
Expand Down
2 changes: 1 addition & 1 deletion domain/__tests__/Tag.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from 'assert';
import dayjs from 'dayjs';
import dayjs from '../../infra/dayjs';
import { newEntry } from '../Entry';
import { tagNameToId, entriesTagToABs } from '../Tag';

Expand Down
2 changes: 1 addition & 1 deletion infra/__tests__/entryRepository.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from 'assert';
import dayjs from 'dayjs';
import { newEntry } from '../../domain/Entry';
import dayjs from '../../infra/dayjs';
import * as entryRepository from '../entryRepository';

describe('Query:entriesRepository', () => {
Expand Down
2 changes: 1 addition & 1 deletion infra/__tests__/queue.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from 'assert';
import dayjs from 'dayjs';
import { newEntry } from '../../domain/Entry';
import dayjs from '../../infra/dayjs';
import * as entryRepository from '../entryRepository';
import { entriesQueue } from '../queue';

Expand Down

0 comments on commit 0301dd2

Please sign in to comment.