Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

Change date filter format.

Mike Cao0c8d9eacd3f8
diff --git a/components/DateFilter.js b/components/DateFilter.js
index 586044b00..b2540c6ad 100644
--- a/components/DateFilter.js
+++ b/components/DateFilter.js
@@ -1,10 +1,10 @@
import React, { useState } from 'react';
import { getDateRange } from 'lib/date';
-const filterOptions = ['24h', '7d', '30d'];
+const filterOptions = ['24hour', '7day', '30day', '60day', '90day'];
export default function DateFilter({ onChange }) {
- const [selected, setSelected] = useState('7d');
+ const [selected, setSelected] = useState('7day');
function handleChange(e) {
const value = e.target.value;
@@ -15,7 +15,7 @@ export default function DateFilter({ onChange }) {
return (
<select value={selected} onChange={handleChange}>
{filterOptions.map(option => (
- <option key={option} name={option}>
+ <option key={option} value={option}>
{option}
</option>
))}
diff --git a/components/PageviewsChart.js b/components/PageviewsChart.js
index 819cc3a0a..20d5e02c0 100644
--- a/components/PageviewsChart.js
+++ b/components/PageviewsChart.js
@@ -11,10 +11,11 @@ export default function PageviewsChart({ data, unit }) {
const renderLabel = useCallback(
(label, index, values) => {
const d = new Date(values[index].value);
+ const n = data.pageviews.length;
switch (unit) {
case 'day':
- if (data.pageviews.length > 7) {
- return index % 2 !== 0 ? format(d, 'MMM d') : '';
+ if (n > 7) {
+ return index % (n / 15) === 0 ? format(d, 'MMM d') : '';
}
return format(d, 'EEE M/d');
default:
diff --git a/components/WebsiteList.js b/components/WebsiteList.js
index 4b1eee0f5..27d912824 100644
--- a/components/WebsiteList.js
+++ b/components/WebsiteList.js
@@ -2,11 +2,12 @@ import React, { useState, useEffect } from 'react';
import { get } from 'lib/web';
import WebsiteStats from './WebsiteStats';
import DateFilter from './DateFilter';
-import { getDateRange } from '../lib/date';
+import { getDateRange } from 'lib/date';
+import styles from './WebsiteList.module.css';
export default function WebsiteList() {
const [data, setData] = useState();
- const [dateRange, setDateRange] = useState(getDateRange('7d'));
+ const [dateRange, setDateRange] = useState(getDateRange('7day'));
const { startDate, endDate, unit } = dateRange;
async function loadData() {
@@ -22,19 +23,18 @@ export default function WebsiteList() {
}, []);
return (
- <div>
+ <div className={styles.container}>
<DateFilter onChange={handleDateChange} />
{data &&
data.websites.map(({ website_id, label }) => (
- <div key={website_id}>
- <h2>{label}</h2>
- <WebsiteStats
- websiteId={website_id}
- startDate={startDate}
- endDate={endDate}
- unit={unit}
- />
- </div>
+ <WebsiteStats
+ key={website_id}
+ title={label}
+ websiteId={website_id}
+ startDate={startDate}
+ endDate={endDate}
+ unit={unit}
+ />
))}
</div>
);
diff --git a/components/WebsiteList.module.css b/components/WebsiteList.module.css
new file mode 100644
index 000000000..f52e78b9b
--- /dev/null
+++ b/components/WebsiteList.module.css
@@ -0,0 +1,3 @@
+.container > div {
+ margin-bottom: 30px;
+}
diff --git a/components/WebsiteStats.js b/components/WebsiteStats.js
index 7896168a3..8b5ef7a22 100644
--- a/components/WebsiteStats.js
+++ b/components/WebsiteStats.js
@@ -3,8 +3,9 @@ import PageviewsChart from './PageviewsChart';
import { get } from 'lib/web';
import { getDateArray, getTimezone } from 'lib/date';
import WebsiteSummary from './WebsiteSummary';
+import styles from './WebsiteStats.module.css';
-export default function WebsiteStats({ websiteId, startDate, endDate, unit }) {
+export default function WebsiteStats({ title, websiteId, startDate, endDate, unit }) {
const [data, setData] = useState();
const [pageviews, uniques] = useMemo(() => {
if (data) {
@@ -32,7 +33,8 @@ export default function WebsiteStats({ websiteId, startDate, endDate, unit }) {
}, [websiteId, startDate, endDate, unit]);
return (
- <div>
+ <div className={styles.container}>
+ <div className={styles.title}>{title}</div>
<WebsiteSummary websiteId={websiteId} startDate={startDate} endDate={endDate} />
<PageviewsChart data={{ pageviews, uniques }} unit={unit} />
</div>
diff --git a/components/WebsiteStats.module.css b/components/WebsiteStats.module.css
new file mode 100644
index 000000000..a108a030a
--- /dev/null
+++ b/components/WebsiteStats.module.css
@@ -0,0 +1,10 @@
+.container {
+ display: flex;
+ flex-direction: column;
+}
+
+.title {
+ font-size: 24px;
+ line-height: 60px;
+ font-weight: 600;
+}
diff --git a/lib/date.js b/lib/date.js
index a3daf1390..bd49a0f8c 100644
--- a/lib/date.js
+++ b/lib/date.js
@@ -30,24 +30,20 @@ export function getDateRange(value) {
const hour = endOfHour(now);
const day = endOfDay(now);
- switch (value) {
- case '7d':
- return {
- startDate: subDays(day, 7),
- endDate: day,
- unit: 'day',
- };
- case '30d':
+ const { num, unit } = value.match(/^(?<num>[0-9]+)(?<unit>hour|day)$/).groups;
+
+ switch (unit) {
+ case 'day':
return {
- startDate: subDays(day, 30),
+ startDate: subDays(day, num),
endDate: day,
- unit: 'day',
+ unit,
};
- default:
+ case 'hour':
return {
- startDate: subHours(hour, 24),
+ startDate: subHours(hour, num),
endDate: hour,
- unit: 'hour',
+ unit,
};
}
}