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

[FEATURE] Time tracking widget for significant events #626

Open
wants to merge 1 commit into
base: main
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
2 changes: 2 additions & 0 deletions src/plugins/widgets/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import since from './since';
import css from "./css";
import github from "./github";
import greeting from "./greeting";
Expand All @@ -17,6 +18,7 @@ import workHours from "./workHours";
import joke from "./joke";

export const widgetConfigs = [
since,
css,
github,
greeting,
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/widgets/since/Since.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.Since
&.relativeTime
font-style: italic;
28 changes: 28 additions & 0 deletions src/plugins/widgets/since/Since.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React, { FC } from "react";
import { FormattedRelativeTime } from "react-intl";

import { useTime } from "../../../hooks";
import { Props, defaultData } from "./types";
import "./Since.sass";

const Since: FC<Props> = ({ data = defaultData }) => {

const from = useTime().getTime();
const to = data.time;
const diff = ((to - from) / 1000) | 0;


return (
<div className="Since">
<h3>
{data.title || "Since"}&nbsp;
{diff > 0 ? "will be" : "was"}&nbsp;
<span className={`Since relativeTime`}>
<FormattedRelativeTime value={diff} updateIntervalInSeconds={1} />
</span>
</h3>
</div>
);
};

export default Since;
63 changes: 63 additions & 0 deletions src/plugins/widgets/since/SinceSettings.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { format } from "date-fns";
import React, { FC } from "react";
import { defaultData, Props } from "./types";

function formatDate(time: number): string {
return format(time, "yyyy-MM-dd");
}

function formatTime(time: number): string {
return format(time, "HH:mm:ss");
}

function buildDateObject(time: number, timeStr: string): Date {
return new Date(`${formatDate(time)} ${timeStr || "00:00:00"}`);
}

const SinceSettings: FC<Props> = ({ data = defaultData, setData }) => (
<div className="CssSettings">
<label>
What
<input
type="text"
value={data.title || ""}
onChange={(event) => setData({ ...data, title: event.target.value })}
/>
</label>

<label>
When
<label>
Date
<input
type="date"
value={formatDate(data.time)}
onChange={(event) =>
setData({
...data,
time: (event.target.value
? new Date(event.target.value)
: new Date()
).getTime(),
})
}
/>
</label>
<label>
Time
<input
type="time"
value={formatTime(data.time)}
onChange={(event) => {
setData({
...data,
time: buildDateObject(data.time, event.target.value).getTime(),
});
}}
/>
</label>
</label>
</div>
);

export default SinceSettings;
13 changes: 13 additions & 0 deletions src/plugins/widgets/since/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Config } from '../../types';
import Since from './Since';
import SinceSettings from './SinceSettings';

const config: Config = {
key: 'widget/since',
name: 'Since',
description: 'This widget tracks the time elapsed since or remaining until a specified event.',
dashboardComponent: Since,
settingsComponent: SinceSettings,
};

export default config;
12 changes: 12 additions & 0 deletions src/plugins/widgets/since/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { API } from '../../types';

type Data = {
time: number;
title?: string;
};

export type Props = API<Data>;

export const defaultData: Data = {
time: Date.now(),
};
1 change: 1 addition & 0 deletions src/views/settings/Settings.sass
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
input[type=number],
input[type=text],
input[type=time],
input[type=date],
input[type=url],
textarea,
select
Expand Down