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

Add setting to enable 24-hour time format #1739

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
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
14 changes: 10 additions & 4 deletions src/app/components/message/Time.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import React from 'react';
import { Text, as } from 'folds';
import { timeDayMonYear, timeHourMinute, today, yesterday } from '../../utils/time';
import { useSetting } from '../../state/hooks/settings';
import { settingsAtom } from '../../state/settings';

export type TimeProps = {
compact?: boolean;
ts: number;
};

export const Time = as<'span', TimeProps>(({ compact, ts, ...props }, ref) => {
const [hour24Clock] = useSetting(settingsAtom, 'hour24Clock');
Copy link
Member

Choose a reason for hiding this comment

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

Subscribing to settingsAtom inside Time component can downgrade the perf of our timeline as every Time component in every message will be listening to it where more perf oriented approach can be to receive hour24Clock as component param and we can subscribe to settingsAtom once for all.

const formattedTime = timeHourMinute(ts, hour24Clock);

let time = '';

if (compact) {
time = timeHourMinute(ts);
time = formattedTime;
} else if (today(ts)) {
time = timeHourMinute(ts);
time = formattedTime;
} else if (yesterday(ts)) {
time = `Yesterday ${timeHourMinute(ts)}`;
time = `Yesterday ${formattedTime}`;
} else {
time = `${timeDayMonYear(ts)} ${timeHourMinute(ts)}`;
time = `${timeDayMonYear(ts)} ${formattedTime}`;
}

return (
Expand Down
6 changes: 5 additions & 1 deletion src/app/components/room-intro/RoomIntro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { useMatrixClient } from '../../hooks/useMatrixClient';
import { getMxIdLocalPart } from '../../utils/matrix';
import { AsyncStatus, useAsyncCallback } from '../../hooks/useAsyncCallback';
import { timeDayMonthYear, timeHourMinute } from '../../utils/time';
import { useSetting } from '../../state/hooks/settings';
import { settingsAtom } from '../../state/settings';

export type RoomIntroProps = {
room: Room;
Expand Down Expand Up @@ -36,6 +38,8 @@ export const RoomIntro = as<'div', RoomIntroProps>(({ room, ...props }, ref) =>
useCallback(async (roomId: string) => mx.joinRoom(roomId), [mx])
);

const [hour24Clock] = useSetting(settingsAtom, 'hour24Clock');

return (
<Box direction="Column" grow="Yes" gap="500" {...props} ref={ref}>
<Box>
Expand Down Expand Up @@ -66,7 +70,7 @@ export const RoomIntro = as<'div', RoomIntroProps>(({ room, ...props }, ref) =>
<Text size="T200" priority="300">
{'Created by '}
<b>@{creatorName}</b>
{` on ${timeDayMonthYear(ts)} ${timeHourMinute(ts)}`}
{` on ${timeDayMonthYear(ts)} ${timeHourMinute(ts, hour24Clock)}`}
</Text>
)}
</Box>
Expand Down
12 changes: 12 additions & 0 deletions src/app/organisms/settings/Settings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ function AppearanceSection() {
const [urlPreview, setUrlPreview] = useSetting(settingsAtom, 'urlPreview');
const [encUrlPreview, setEncUrlPreview] = useSetting(settingsAtom, 'encUrlPreview');
const [showHiddenEvents, setShowHiddenEvents] = useSetting(settingsAtom, 'showHiddenEvents');
const [hour24Clock, setHour24Clock] = useSetting(settingsAtom, 'hour24Clock');

const spacings = ['0', '100', '200', '300', '400', '500']

return (
Expand Down Expand Up @@ -223,6 +225,16 @@ function AppearanceSection() {
)}
content={<Text variant="b3">Show hidden state and message events.</Text>}
/>
<SettingTile
title="Use 24-hour time"
options={
<Toggle
isActive={hour24Clock}
onToggle={() => setHour24Clock(!hour24Clock)}
/>
}
content={<Text variant="b3">Use the 24-hour time format for all timestamps.</Text>}
/>
</div>
</div>
);
Expand Down
4 changes: 4 additions & 0 deletions src/app/state/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export interface Settings {

showNotifications: boolean;
isNotificationSounds: boolean;

hour24Clock: boolean;
}

const defaultSettings: Settings = {
Expand All @@ -48,6 +50,8 @@ const defaultSettings: Settings = {

showNotifications: true,
isNotificationSounds: true,

hour24Clock: false,
};

export const getSettings = () => {
Expand Down
3 changes: 2 additions & 1 deletion src/app/utils/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export const today = (ts: number): boolean => dayjs(ts).isToday();

export const yesterday = (ts: number): boolean => dayjs(ts).isYesterday();

export const timeHourMinute = (ts: number): string => dayjs(ts).format('hh:mm A');
export const timeHourMinute = (ts: number, hour24Clock: boolean = false): string =>
dayjs(ts).format(hour24Clock ? 'HH:mm' : 'hh:mm A');

export const timeDayMonYear = (ts: number): string => dayjs(ts).format('D MMM YYYY');

Expand Down
Loading