1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { CollectionDirectory, CollectionFile } from "models/collection-file";
6 import { Middleware, Store } from "redux";
7 import { ServiceRepository } from "services/services";
8 import { RootState } from "store/store";
9 import tippy, { createSingleton } from 'tippy.js';
10 import 'tippy.js/dist/tippy.css';
13 let tooltipsContents = null;
14 let tooltipsFetchFailed = false;
15 export const TOOLTIP_LOCAL_STORAGE_KEY = "TOOLTIP_LOCAL_STORAGE_KEY";
17 const tippySingleton = createSingleton([], {delay: 10});
19 export const tooltipsMiddleware = (services: ServiceRepository): Middleware => (store: Store) => next => action => {
20 const state: RootState = store.getState();
21 const hideTooltip = localStorage.getItem(TOOLTIP_LOCAL_STORAGE_KEY);
22 const { BannerUUID } = (state.auth.config.clusterConfig.Workbench as any);
24 const bannerUUID = BannerUUID || 'tordo-4zz18-1buneu6sb8zxiti';
26 if (bannerUUID && !tooltipsContents && !hideTooltip && !tooltipsFetchFailed && !running) {
28 fetchTooltips(services, bannerUUID);
29 } else if (tooltipsContents && !hideTooltip && !tooltipsFetchFailed) {
36 const fetchTooltips = (services, bannerUUID) => {
37 services.collectionService.files(bannerUUID)
39 const tooltipsFile: CollectionDirectory | CollectionFile | undefined = results.find(({ name }) => name === 'tooltips.json');
43 services.collectionService.getFileContents(tooltipsFile as CollectionFile)
45 tooltipsContents = JSON.parse(data);
53 tooltipsFetchFailed = true;
62 const applyTooltips = () => {
63 const tippyInstances: any[] = Object.keys(tooltipsContents as any)
65 const content = (tooltipsContents as any)[key]
66 const element = document.querySelector(key);
69 const hasTippyAttatched = !!(element as any)._tippy;
71 if (!hasTippyAttatched && tooltipsContents) {
72 return tippy(element as any, { content });
78 .filter(data => !!data);
80 if (tippyInstances.length > 0) {
81 tippySingleton.setInstances(tippyInstances);