@tanstack/react-query-devtools
Version:
114 lines (103 loc) • 3.73 kB
JavaScript
/**
* react-query-devtools
*
* Copyright (c) TanStack
*
* This source code is licensed under the MIT license found in the
* LICENSE.md file in the root directory of this source tree.
*
* @license MIT
*/
;
Object.defineProperty(exports, '__esModule', { value: true });
var React = require('react');
var theme = require('./theme.js');
var useMediaQuery = require('./useMediaQuery.js');
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
}
n["default"] = e;
return Object.freeze(n);
}
var React__namespace = /*#__PURE__*/_interopNamespace(React);
const isServer = typeof window === 'undefined';
function getQueryStatusColor({
queryState,
observerCount,
isStale,
theme
}) {
return queryState.fetchStatus === 'fetching' ? theme.active : !observerCount ? theme.gray : queryState.fetchStatus === 'paused' ? theme.paused : isStale ? theme.warning : theme.success;
}
function getQueryStatusLabel(query) {
return query.state.fetchStatus === 'fetching' ? 'fetching' : !query.getObserversCount() ? 'inactive' : query.state.fetchStatus === 'paused' ? 'paused' : query.isStale() ? 'stale' : 'fresh';
}
function styled(type, newStyles, queries = {}) {
return /*#__PURE__*/React__namespace.forwardRef(({
style,
...rest
}, ref) => {
const theme$1 = theme.useTheme();
const mediaStyles = Object.entries(queries).reduce((current, [key, value]) => {
// eslint-disable-next-line react-hooks/rules-of-hooks
return useMediaQuery["default"](key) ? { ...current,
...(typeof value === 'function' ? value(rest, theme$1) : value)
} : current;
}, {});
return /*#__PURE__*/React__namespace.createElement(type, { ...rest,
style: { ...(typeof newStyles === 'function' ? newStyles(rest, theme$1) : newStyles),
...style,
...mediaStyles
},
ref
});
});
}
function useIsMounted() {
const mountedRef = React__namespace.useRef(false);
const isMounted = React__namespace.useCallback(() => mountedRef.current, []);
React__namespace[isServer ? 'useEffect' : 'useLayoutEffect'](() => {
mountedRef.current = true;
return () => {
mountedRef.current = false;
};
}, []);
return isMounted;
}
/**
* Displays a string regardless the type of the data
* @param {unknown} value Value to be stringified
*/
const displayValue = value => {
const name = Object.getOwnPropertyNames(Object(value));
const newValue = typeof value === 'bigint' ? value.toString() + "n" : value;
return JSON.stringify(newValue, name);
};
const getStatusRank = q => q.state.fetchStatus !== 'idle' ? 0 : !q.getObserversCount() ? 3 : q.isStale() ? 2 : 1;
const sortFns = {
'Status > Last Updated': (a, b) => {
var _sortFns$LastUpdated;
return getStatusRank(a) === getStatusRank(b) ? (_sortFns$LastUpdated = sortFns['Last Updated']) == null ? void 0 : _sortFns$LastUpdated.call(sortFns, a, b) : getStatusRank(a) > getStatusRank(b) ? 1 : -1;
},
'Query Hash': (a, b) => a.queryHash > b.queryHash ? 1 : -1,
'Last Updated': (a, b) => a.state.dataUpdatedAt < b.state.dataUpdatedAt ? 1 : -1
};
exports.displayValue = displayValue;
exports.getQueryStatusColor = getQueryStatusColor;
exports.getQueryStatusLabel = getQueryStatusLabel;
exports.isServer = isServer;
exports.sortFns = sortFns;
exports.styled = styled;
exports.useIsMounted = useIsMounted;
//# sourceMappingURL=utils.js.map