2018-04-11 23:58:04 +01:00
|
|
|
/*
|
|
|
|
|
Copyright 2018 Michael Telatynski <7t3chguy@gmail.com>
|
2021-06-22 20:41:26 +01:00
|
|
|
Copyright 2015 - 2021 The Matrix.org Foundation C.I.C.
|
2018-04-11 23:58:04 +01:00
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import React from "react";
|
2022-01-27 16:32:12 -06:00
|
|
|
import { Direction } from "matrix-js-sdk/src/models/event-timeline";
|
|
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2018-04-11 23:58:04 +01:00
|
|
|
|
2021-06-22 20:41:26 +01:00
|
|
|
import { _t } from "../../../languageHandler";
|
|
|
|
|
import { formatFullDateNoTime } from "../../../DateUtils";
|
2022-01-27 16:32:12 -06:00
|
|
|
import { MatrixClientPeg } from "../../../MatrixClientPeg";
|
|
|
|
|
import dis from "../../../dispatcher/dispatcher";
|
|
|
|
|
import { Action } from "../../../dispatcher/actions";
|
2021-12-21 10:08:22 +01:00
|
|
|
import SettingsStore from "../../../settings/SettingsStore";
|
|
|
|
|
import { UIFeature } from "../../../settings/UIFeature";
|
2022-01-27 16:32:12 -06:00
|
|
|
import Modal from "../../../Modal";
|
|
|
|
|
import ErrorDialog from "../dialogs/ErrorDialog";
|
|
|
|
|
import { contextMenuBelow } from "../rooms/RoomTile";
|
|
|
|
|
import { ContextMenuTooltipButton } from "../../structures/ContextMenu";
|
|
|
|
|
import IconizedContextMenu, {
|
|
|
|
|
IconizedContextMenuOption,
|
|
|
|
|
IconizedContextMenuOptionList,
|
|
|
|
|
} from "../context_menus/IconizedContextMenu";
|
|
|
|
|
import JumpToDatePicker from "./JumpToDatePicker";
|
2022-02-10 14:29:55 +00:00
|
|
|
import { ViewRoomPayload } from "../../../dispatcher/payloads/ViewRoomPayload";
|
2021-06-22 20:41:26 +01:00
|
|
|
|
|
|
|
|
function getDaysArray(): string[] {
|
2018-04-11 23:58:04 +01:00
|
|
|
return [_t("Sunday"), _t("Monday"), _t("Tuesday"), _t("Wednesday"), _t("Thursday"), _t("Friday"), _t("Saturday")];
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-22 20:41:26 +01:00
|
|
|
interface IProps {
|
2022-01-27 16:32:12 -06:00
|
|
|
roomId: string;
|
2021-06-22 20:41:26 +01:00
|
|
|
ts: number;
|
2021-07-02 10:49:24 +05:30
|
|
|
forExport?: boolean;
|
2021-06-22 20:41:26 +01:00
|
|
|
}
|
2018-04-11 23:58:04 +01:00
|
|
|
|
2022-01-27 16:32:12 -06:00
|
|
|
interface IState {
|
|
|
|
|
contextMenuPosition?: DOMRect;
|
|
|
|
|
jumpToDateEnabled: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default class DateSeparator extends React.Component<IProps, IState> {
|
2023-02-13 11:39:16 +00:00
|
|
|
private settingWatcherRef?: string;
|
2022-01-27 16:32:12 -06:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
public constructor(props: IProps) {
|
|
|
|
|
super(props);
|
2022-01-27 16:32:12 -06:00
|
|
|
this.state = {
|
|
|
|
|
jumpToDateEnabled: SettingsStore.getValue("feature_jump_to_date"),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// We're using a watcher so the date headers in the timeline are updated
|
|
|
|
|
// when the lab setting is toggled.
|
|
|
|
|
this.settingWatcherRef = SettingsStore.watchSetting(
|
|
|
|
|
"feature_jump_to_date",
|
|
|
|
|
null,
|
|
|
|
|
(settingName, roomId, level, newValAtLevel, newVal) => {
|
|
|
|
|
this.setState({ jumpToDateEnabled: newVal });
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public componentWillUnmount(): void {
|
2022-01-27 16:32:12 -06:00
|
|
|
SettingsStore.unwatchSetting(this.settingWatcherRef);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private onContextMenuOpenClick = (e: React.MouseEvent): void => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
const target = e.target as HTMLButtonElement;
|
|
|
|
|
this.setState({ contextMenuPosition: target.getBoundingClientRect() });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private onContextMenuCloseClick = (): void => {
|
|
|
|
|
this.closeMenu();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private closeMenu = (): void => {
|
|
|
|
|
this.setState({
|
|
|
|
|
contextMenuPosition: null,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private getLabel(): string {
|
2018-04-11 23:58:04 +01:00
|
|
|
const date = new Date(this.props.ts);
|
2021-12-21 10:08:22 +01:00
|
|
|
const disableRelativeTimestamps = !SettingsStore.getValue(UIFeature.TimelineEnableRelativeDates);
|
2021-05-31 19:01:32 +05:30
|
|
|
|
|
|
|
|
// During the time the archive is being viewed, a specific day might not make sense, so we return the full date
|
2021-12-21 10:08:22 +01:00
|
|
|
if (this.props.forExport || disableRelativeTimestamps) return formatFullDateNoTime(date);
|
2021-05-31 19:01:32 +05:30
|
|
|
|
2018-04-11 23:58:04 +01:00
|
|
|
const today = new Date();
|
|
|
|
|
const yesterday = new Date();
|
2021-06-22 20:41:26 +01:00
|
|
|
const days = getDaysArray();
|
2018-04-11 23:58:04 +01:00
|
|
|
yesterday.setDate(today.getDate() - 1);
|
|
|
|
|
|
|
|
|
|
if (date.toDateString() === today.toDateString()) {
|
|
|
|
|
return _t("Today");
|
|
|
|
|
} else if (date.toDateString() === yesterday.toDateString()) {
|
|
|
|
|
return _t("Yesterday");
|
|
|
|
|
} else if (today.getTime() - date.getTime() < 6 * 24 * 60 * 60 * 1000) {
|
|
|
|
|
return days[date.getDay()];
|
|
|
|
|
} else {
|
|
|
|
|
return formatFullDateNoTime(date);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
private pickDate = async (inputTimestamp: number | string | Date): Promise<void> => {
|
2022-01-27 16:32:12 -06:00
|
|
|
const unixTimestamp = new Date(inputTimestamp).getTime();
|
|
|
|
|
|
|
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
|
try {
|
|
|
|
|
const roomId = this.props.roomId;
|
|
|
|
|
const { event_id: eventId, origin_server_ts: originServerTs } = await cli.timestampToEvent(
|
|
|
|
|
roomId,
|
|
|
|
|
unixTimestamp,
|
|
|
|
|
Direction.Forward,
|
|
|
|
|
);
|
|
|
|
|
logger.log(
|
|
|
|
|
`/timestamp_to_event: ` +
|
|
|
|
|
`found ${eventId} (${originServerTs}) for timestamp=${unixTimestamp} (looking forward)`,
|
|
|
|
|
);
|
|
|
|
|
|
2022-02-10 14:29:55 +00:00
|
|
|
dis.dispatch<ViewRoomPayload>({
|
2022-01-27 16:32:12 -06:00
|
|
|
action: Action.ViewRoom,
|
|
|
|
|
event_id: eventId,
|
|
|
|
|
highlighted: true,
|
|
|
|
|
room_id: roomId,
|
2022-02-17 18:03:27 +00:00
|
|
|
metricsTrigger: undefined, // room doesn't change
|
2022-01-27 16:32:12 -06:00
|
|
|
});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
const code = e.errcode || e.statusCode;
|
|
|
|
|
// only show the dialog if failing for something other than a network error
|
|
|
|
|
// (e.g. no errcode or statusCode) as in that case the redactions end up in the
|
|
|
|
|
// detached queue and we show the room status bar to allow retry
|
|
|
|
|
if (typeof code !== "undefined") {
|
|
|
|
|
// display error message stating you couldn't delete this.
|
2022-06-14 17:51:51 +01:00
|
|
|
Modal.createDialog(ErrorDialog, {
|
2022-01-27 16:32:12 -06:00
|
|
|
title: _t("Error"),
|
|
|
|
|
description: _t("Unable to find event at that date. (%(code)s)", { code }),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private onLastWeekClicked = (): void => {
|
|
|
|
|
const date = new Date();
|
|
|
|
|
date.setDate(date.getDate() - 7);
|
|
|
|
|
this.pickDate(date);
|
|
|
|
|
this.closeMenu();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private onLastMonthClicked = (): void => {
|
|
|
|
|
const date = new Date();
|
|
|
|
|
// Month numbers are 0 - 11 and `setMonth` handles the negative rollover
|
|
|
|
|
date.setMonth(date.getMonth() - 1, 1);
|
|
|
|
|
this.pickDate(date);
|
|
|
|
|
this.closeMenu();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private onTheBeginningClicked = (): void => {
|
|
|
|
|
const date = new Date(0);
|
|
|
|
|
this.pickDate(date);
|
|
|
|
|
this.closeMenu();
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
private onDatePicked = (dateString: string): void => {
|
2022-01-27 16:32:12 -06:00
|
|
|
this.pickDate(dateString);
|
|
|
|
|
this.closeMenu();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private renderJumpToDateMenu(): React.ReactElement {
|
|
|
|
|
let contextMenu: JSX.Element;
|
|
|
|
|
if (this.state.contextMenuPosition) {
|
|
|
|
|
contextMenu = (
|
|
|
|
|
<IconizedContextMenu
|
|
|
|
|
{...contextMenuBelow(this.state.contextMenuPosition)}
|
|
|
|
|
onFinished={this.onContextMenuCloseClick}
|
|
|
|
|
>
|
|
|
|
|
<IconizedContextMenuOptionList first>
|
|
|
|
|
<IconizedContextMenuOption label={_t("Last week")} onClick={this.onLastWeekClicked} />
|
|
|
|
|
<IconizedContextMenuOption label={_t("Last month")} onClick={this.onLastMonthClicked} />
|
|
|
|
|
<IconizedContextMenuOption
|
|
|
|
|
label={_t("The beginning of the room")}
|
|
|
|
|
onClick={this.onTheBeginningClicked}
|
|
|
|
|
/>
|
|
|
|
|
</IconizedContextMenuOptionList>
|
|
|
|
|
|
|
|
|
|
<IconizedContextMenuOptionList>
|
|
|
|
|
<JumpToDatePicker ts={this.props.ts} onDatePicked={this.onDatePicked} />
|
|
|
|
|
</IconizedContextMenuOptionList>
|
|
|
|
|
</IconizedContextMenu>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<ContextMenuTooltipButton
|
|
|
|
|
className="mx_DateSeparator_jumpToDateMenu"
|
|
|
|
|
onClick={this.onContextMenuOpenClick}
|
|
|
|
|
isExpanded={!!this.state.contextMenuPosition}
|
|
|
|
|
title={_t("Jump to date")}
|
|
|
|
|
>
|
2022-08-01 08:31:14 +01:00
|
|
|
<h2 aria-hidden="true">{this.getLabel()}</h2>
|
2022-01-27 16:32:12 -06:00
|
|
|
<div className="mx_DateSeparator_chevron" />
|
|
|
|
|
{contextMenu}
|
|
|
|
|
</ContextMenuTooltipButton>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-13 17:01:43 +00:00
|
|
|
public render(): React.ReactNode {
|
2021-12-21 10:08:22 +01:00
|
|
|
const label = this.getLabel();
|
2022-01-27 16:32:12 -06:00
|
|
|
|
|
|
|
|
let dateHeaderContent;
|
|
|
|
|
if (this.state.jumpToDateEnabled) {
|
|
|
|
|
dateHeaderContent = this.renderJumpToDateMenu();
|
|
|
|
|
} else {
|
2022-08-01 08:31:14 +01:00
|
|
|
dateHeaderContent = <h2 aria-hidden="true">{label}</h2>;
|
2022-01-27 16:32:12 -06:00
|
|
|
}
|
|
|
|
|
|
2019-10-21 16:51:40 +01:00
|
|
|
// ARIA treats <hr/>s as separators, here we abuse them slightly so manually treat this entire thing as one
|
2019-11-04 10:16:16 +00:00
|
|
|
// tab-index=-1 to allow it to be focusable but do not add tab stop for it, primarily for screen readers
|
2022-08-01 08:31:14 +01:00
|
|
|
return (
|
|
|
|
|
<div className="mx_DateSeparator" role="separator" tabIndex={-1} aria-label={label}>
|
2019-10-21 16:51:40 +01:00
|
|
|
<hr role="none" />
|
2022-01-27 16:32:12 -06:00
|
|
|
{dateHeaderContent}
|
2019-10-21 16:51:40 +01:00
|
|
|
<hr role="none" />
|
2022-08-01 08:31:14 +01:00
|
|
|
</div>
|
|
|
|
|
);
|
2018-04-11 23:58:04 +01:00
|
|
|
}
|
|
|
|
|
}
|