Skip to content

Commit

Permalink
[FEATURE] Time tracking widget for significant events joelshepherd#626
Browse files Browse the repository at this point in the history
There is now two different way to count-up, but this one can also count time since something. I will be leaving both, choose whatever you want.
  • Loading branch information
BookCatKid committed Feb 9, 2025
1 parent 029674b commit 9789c3a
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/plugins/widgets/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import countdown from './countdown';
import since from './since';
import css from "./css";
import github from "./github";
import greeting from "./greeting";
Expand All @@ -23,6 +24,7 @@ import bookmarks from "./bookmarks";
export const widgetConfigs = [
countdown,
bookmarks,
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(),
};

0 comments on commit 9789c3a

Please sign in to comment.