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 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
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 [useInternationalTime] = useSetting(settingsAtom, 'useInternationalTime');
const formattedTime = timeHourMinute(ts, useInternationalTime);

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 [useInternationalTime] = useSetting(settingsAtom, 'useInternationalTime');

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, useInternationalTime)}`}
</Text>
)}
</Box>
Expand Down
15 changes: 15 additions & 0 deletions src/app/organisms/settings/Settings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ function AppearanceSection() {
const [urlPreview, setUrlPreview] = useSetting(settingsAtom, 'urlPreview');
const [encUrlPreview, setEncUrlPreview] = useSetting(settingsAtom, 'encUrlPreview');
const [showHiddenEvents, setShowHiddenEvents] = useSetting(settingsAtom, 'showHiddenEvents');
const [useInternationalTime, setInternationalTime] = useSetting(
settingsAtom,
'useInternationalTime'
);

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

return (
Expand Down Expand Up @@ -223,6 +228,16 @@ function AppearanceSection() {
)}
content={<Text variant="b3">Show hidden state and message events.</Text>}
/>
<SettingTile
title="Use 24-hour time"
options={
<Toggle
isActive={useInternationalTime}
onToggle={() => setInternationalTime(!useInternationalTime)}
/>
}
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;

useInternationalTime: boolean;
}

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

showNotifications: true,
isNotificationSounds: true,

useInternationalTime: 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, international: boolean = false): string =>
dayjs(ts).format(international ? 'HH:mm' : 'hh:mm A');

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

Expand Down
Loading