Updated fetch hook.

This commit is contained in:
Mike Cao 2020-10-10 22:36:55 -07:00
parent 3374f875f3
commit 1fcb610bdd
6 changed files with 57 additions and 30 deletions

View File

@ -94,14 +94,24 @@ export default function RealtimeLog({ data, websites }) {
} }
} }
function getTime({ created_at }) {
return format(new Date(created_at), 'h:mm:ss');
}
function getColor(row) {
const { session_id } = row;
return stringToColor(uuids[session_id] || `${session_id}${getWebsite(row)}`);
}
const Row = ({ index, style }) => { const Row = ({ index, style }) => {
const row = logs[index]; const row = logs[index];
return ( return (
<div className={styles.row} style={style}> <div className={styles.row} style={style}>
<div> <div>
<Dot color={stringToColor(uuids[row.session_id] || `${row.session_id}`)} /> <Dot color={getColor(row)} />
</div> </div>
<div className={styles.time}>{format(new Date(row.created_at), 'h:mm:ss')}</div> <div className={styles.time}>{getTime(row)}</div>
<div className={styles.detail}> <div className={styles.detail}>
<Icon className={styles.icon} icon={getIcon(row)} /> <Icon className={styles.icon} icon={getIcon(row)} />
{getDetail(row)} {getDetail(row)}

View File

@ -34,15 +34,12 @@
.website { .website {
text-align: right; text-align: right;
padding-right: 20px; padding: 0 20px;
} }
.detail { .detail {
display: flex; display: flex;
flex: 1; flex: 1;
}
.detail span {
white-space: nowrap; white-space: nowrap;
text-overflow: ellipsis; text-overflow: ellipsis;
overflow: hidden; overflow: hidden;

View File

@ -80,7 +80,7 @@ export default function RealtimeDashboard() {
); );
} }
return []; return [];
}, [realtimeData]); }, [realtimeData?.sessions]);
const referrers = useMemo(() => { const referrers = useMemo(() => {
if (realtimeData?.pageviews) { if (realtimeData?.pageviews) {
@ -89,12 +89,15 @@ export default function RealtimeDashboard() {
.reduce((arr, { referrer }) => { .reduce((arr, { referrer }) => {
if (referrer?.startsWith('http')) { if (referrer?.startsWith('http')) {
const { hostname } = new URL(referrer); const { hostname } = new URL(referrer);
const row = arr.find(({ x }) => x === hostname);
if (!row) { if (!data.domains.includes(hostname)) {
arr.push({ x: hostname, y: 1 }); const row = arr.find(({ x }) => x === hostname);
} else {
row.y += 1; if (!row) {
arr.push({ x: hostname, y: 1 });
} else {
row.y += 1;
}
} }
} }
return arr; return arr;
@ -103,7 +106,7 @@ export default function RealtimeDashboard() {
); );
} }
return []; return [];
}, [realtimeData]); }, [realtimeData?.pageviews]);
useEffect(() => { useEffect(() => {
if (init && !data) { if (init && !data) {
@ -111,7 +114,11 @@ export default function RealtimeDashboard() {
const domains = init.websites.map(({ domain }) => domain); const domains = init.websites.map(({ domain }) => domain);
setData({ websites, domains, ...data }); setData({ websites, domains, ...data });
} else if (updates) { }
}, [init]);
useEffect(() => {
if (updates) {
const { pageviews, sessions, events, timestamp } = updates; const { pageviews, sessions, events, timestamp } = updates;
const time = subMinutes(startOfMinute(new Date()), REALTIME_RANGE).getTime(); const time = subMinutes(startOfMinute(new Date()), REALTIME_RANGE).getTime();
@ -123,16 +130,14 @@ export default function RealtimeDashboard() {
timestamp, timestamp,
})); }));
} }
}, [init, updates]); }, [updates]);
if (!init || loading || !data) { if (!init || !data || loading) {
return null; return null;
} }
const { websites } = data; const { websites } = data;
//console.log({realtimeData, countries});
return ( return (
<Page> <Page>
<RealtimeHeader <RealtimeHeader

View File

@ -10,10 +10,11 @@ export default function useFetch(url, options = {}, update = []) {
const [status, setStatus] = useState(); const [status, setStatus] = useState();
const [error, setError] = useState(); const [error, setError] = useState();
const [loading, setLoadiing] = useState(false); const [loading, setLoadiing] = useState(false);
const [count, setCount] = useState(0);
const { basePath } = useRouter(); const { basePath } = useRouter();
const { params, disabled, headers, interval, delay = 0, onDataLoad } = options; const { params = {}, disabled, headers, delay = 0, interval, onDataLoad } = options;
async function loadData() { async function loadData(params) {
try { try {
setLoadiing(true); setLoadiing(true);
setError(null); setError(null);
@ -41,17 +42,23 @@ export default function useFetch(url, options = {}, update = []) {
useEffect(() => { useEffect(() => {
if (url && !disabled) { if (url && !disabled) {
setTimeout(() => loadData(), delay); const id = setTimeout(() => loadData(params), delay);
return () => {
clearTimeout(id);
};
} }
}, [url, disabled, ...update]); }, [url, !!disabled, count, ...update]);
useEffect(() => { useEffect(() => {
const id = interval ? setInterval(() => loadData(), interval) : null; if (interval && !disabled) {
const id = setInterval(() => setCount(state => state + 1), interval);
return () => { return () => {
clearInterval(id); clearInterval(id);
}; };
}, [interval, params]); }
}, [interval, !!disabled]);
return { data, status, error, loading }; return { data, status, error, loading };
} }

View File

@ -51,6 +51,8 @@ export const EVENT_COLORS = [
'#ffec16', '#ffec16',
]; ];
export const DEFAULT_LOCALE = 'en-US';
export const DEFAULT_THEME = 'light';
export const DEFAUL_CHART_HEIGHT = 400; export const DEFAUL_CHART_HEIGHT = 400;
export const DEFAULT_ANIMATION_DURATION = 300; export const DEFAULT_ANIMATION_DURATION = 300;
export const DEFAULT_DATE_RANGE = '24hour'; export const DEFAULT_DATE_RANGE = '24hour';

View File

@ -1,13 +1,19 @@
import { createSlice } from '@reduxjs/toolkit'; import { createSlice } from '@reduxjs/toolkit';
import { getItem } from 'lib/web'; import { getItem } from 'lib/web';
import { LOCALE_CONFIG, THEME_CONFIG, VERSION_CHECK } from 'lib/constants'; import {
DEFAULT_LOCALE,
DEFAULT_THEME,
LOCALE_CONFIG,
THEME_CONFIG,
VERSION_CHECK,
} from 'lib/constants';
import semver from 'semver'; import semver from 'semver';
const app = createSlice({ const app = createSlice({
name: 'app', name: 'app',
initialState: { initialState: {
locale: getItem(LOCALE_CONFIG) || 'en-US', locale: getItem(LOCALE_CONFIG) || DEFAULT_LOCALE,
theme: getItem(THEME_CONFIG) || 'light', theme: getItem(THEME_CONFIG) || DEFAULT_THEME,
versions: { versions: {
current: process.env.VERSION, current: process.env.VERSION,
latest: null, latest: null,