Skip to content

Commit

Permalink
Cache Zap details when zaps are ready on dashboard page load (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
JishnuM authored May 2, 2021
1 parent adf81bc commit 0b64364
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion source/events/on-div-hover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ function onDivHover(selector: string, callback: DelegateFieldEvent): void {
});
}

export function onDashboardZapHover(callback: DelegateFieldEvent): void {
export function onDashboardZapIconsHover(callback: DelegateFieldEvent): void {
onDivHover('.zap-icons', callback);
}
19 changes: 15 additions & 4 deletions source/features/show-zap-details-on-hover.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import React from 'dom-chef';
import delegate from 'delegate-it';
import elementReady from 'element-ready';
import select from 'select-dom';

import features from '.';
import './show-zap-details-on-hover.css';
import {isZaps} from '../helpers/page-detect';
import {onDashboardZapHover} from '../events/on-div-hover';
import {onDashboardZapIconsHover} from '../events/on-div-hover';
import { fetchZapDetails } from '../helpers/api';

async function handleZapHover(event: delegate.Event<MouseEvent>): Promise<void> {
async function handleZapIconsHover(event: delegate.Event<MouseEvent>): Promise<void> {
const zapIconsDiv = event.delegateTarget;
const zapIconsDivWrapper = zapIconsDiv.parentElement?.parentElement?.parentElement;
const zapId = zapIconsDivWrapper!.getAttribute('data-zap-id');
Expand Down Expand Up @@ -38,9 +39,19 @@ async function handleZapHover(event: delegate.Event<MouseEvent>): Promise<void>
}
}

async function cacheTooltips(): Promise<void> {
await elementReady('.zap-mini', {'stopOnDomReady': false});

const zapIds = select.all('.zap-mini').map(el => el!.getAttribute('data-zap-id'));
// Fire API requests to load the necessary data.
// These are memoized in session so loads on hover are fast.
zapIds.filter(zapId => zapId !== null).map(zapId => fetchZapDetails(zapId!));
}

async function init(): Promise<false | void> {
onDashboardZapHover(handleZapHover);
// Pre-cache the hover information
onDashboardZapIconsHover(handleZapIconsHover);

cacheTooltips();
}

void features.add(__filebasename, {
Expand Down

0 comments on commit 0b64364

Please sign in to comment.