@perses-dev/components 0.52.0 → 0.53.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/context/SnackbarProvider.js +58 -2
- package/dist/cjs/utils/browser-storage.js +62 -0
- package/dist/cjs/utils/index.js +1 -0
- package/dist/context/SnackbarProvider.d.ts +4 -1
- package/dist/context/SnackbarProvider.d.ts.map +1 -1
- package/dist/context/SnackbarProvider.js +18 -3
- package/dist/context/SnackbarProvider.js.map +1 -1
- package/dist/utils/browser-storage.d.ts +9 -0
- package/dist/utils/browser-storage.d.ts.map +1 -0
- package/dist/utils/browser-storage.js +58 -0
- package/dist/utils/browser-storage.js.map +1 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +1 -0
- package/dist/utils/index.js.map +1 -1
- package/package.json +2 -2
|
@@ -22,14 +22,70 @@ function _export(target, all) {
|
|
|
22
22
|
}
|
|
23
23
|
_export(exports, {
|
|
24
24
|
SnackbarProvider: function() {
|
|
25
|
-
return
|
|
25
|
+
return SnackbarProvider;
|
|
26
26
|
},
|
|
27
27
|
useSnackbar: function() {
|
|
28
28
|
return useSnackbar;
|
|
29
29
|
}
|
|
30
30
|
});
|
|
31
|
-
const
|
|
31
|
+
const _jsxruntime = require("react/jsx-runtime");
|
|
32
|
+
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
|
|
33
|
+
const _styles = require("@mui/material/styles");
|
|
32
34
|
const _notistack = require("notistack");
|
|
35
|
+
function _getRequireWildcardCache(nodeInterop) {
|
|
36
|
+
if (typeof WeakMap !== "function") return null;
|
|
37
|
+
var cacheBabelInterop = new WeakMap();
|
|
38
|
+
var cacheNodeInterop = new WeakMap();
|
|
39
|
+
return (_getRequireWildcardCache = function(nodeInterop) {
|
|
40
|
+
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
|
|
41
|
+
})(nodeInterop);
|
|
42
|
+
}
|
|
43
|
+
function _interop_require_wildcard(obj, nodeInterop) {
|
|
44
|
+
if (!nodeInterop && obj && obj.__esModule) {
|
|
45
|
+
return obj;
|
|
46
|
+
}
|
|
47
|
+
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
|
48
|
+
return {
|
|
49
|
+
default: obj
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
var cache = _getRequireWildcardCache(nodeInterop);
|
|
53
|
+
if (cache && cache.has(obj)) {
|
|
54
|
+
return cache.get(obj);
|
|
55
|
+
}
|
|
56
|
+
var newObj = {
|
|
57
|
+
__proto__: null
|
|
58
|
+
};
|
|
59
|
+
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
60
|
+
for(var key in obj){
|
|
61
|
+
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
62
|
+
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
|
63
|
+
if (desc && (desc.get || desc.set)) {
|
|
64
|
+
Object.defineProperty(newObj, key, desc);
|
|
65
|
+
} else {
|
|
66
|
+
newObj[key] = obj[key];
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
newObj.default = obj;
|
|
71
|
+
if (cache) {
|
|
72
|
+
cache.set(obj, newObj);
|
|
73
|
+
}
|
|
74
|
+
return newObj;
|
|
75
|
+
}
|
|
76
|
+
function SnackbarProvider({ children, ...props }) {
|
|
77
|
+
return /*#__PURE__*/ (0, _jsxruntime.jsx)(_notistack.SnackbarProvider, {
|
|
78
|
+
...props,
|
|
79
|
+
Components: {
|
|
80
|
+
error: (0, _styles.styled)(_notistack.MaterialDesignContent)(()=>({
|
|
81
|
+
'&.notistack-MuiContent-error': {
|
|
82
|
+
whiteSpace: 'pre-wrap'
|
|
83
|
+
}
|
|
84
|
+
}))
|
|
85
|
+
},
|
|
86
|
+
children: children
|
|
87
|
+
});
|
|
88
|
+
}
|
|
33
89
|
function useSnackbar() {
|
|
34
90
|
const { enqueueSnackbar, closeSnackbar } = (0, _notistack.useSnackbar)();
|
|
35
91
|
// Create variant-specific callbacks
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// Copyright 2023 The Perses Authors
|
|
2
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
3
|
+
// you may not use this file except in compliance with the License.
|
|
4
|
+
// You may obtain a copy of the License at
|
|
5
|
+
//
|
|
6
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
//
|
|
8
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
9
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
// See the License for the specific language governing permissions and
|
|
12
|
+
// limitations under the License.
|
|
13
|
+
"use strict";
|
|
14
|
+
Object.defineProperty(exports, "__esModule", {
|
|
15
|
+
value: true
|
|
16
|
+
});
|
|
17
|
+
Object.defineProperty(exports, "useLocalStorage", {
|
|
18
|
+
enumerable: true,
|
|
19
|
+
get: function() {
|
|
20
|
+
return useLocalStorage;
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
const _react = require("react");
|
|
24
|
+
function useLocalStorage(key, initialValue) {
|
|
25
|
+
const { value, setValueAndStore } = useStorage(window.localStorage, key, initialValue);
|
|
26
|
+
return [
|
|
27
|
+
value,
|
|
28
|
+
setValueAndStore
|
|
29
|
+
];
|
|
30
|
+
}
|
|
31
|
+
// Common functionality used by all storage hooks
|
|
32
|
+
function useStorage(storage, key, initialValue) {
|
|
33
|
+
// Use state so that changes cause the page to re-render
|
|
34
|
+
const [value, setValue] = (0, _react.useState)(()=>{
|
|
35
|
+
try {
|
|
36
|
+
const json = storage.getItem(key);
|
|
37
|
+
if (json !== null) {
|
|
38
|
+
return JSON.parse(json);
|
|
39
|
+
}
|
|
40
|
+
} catch {
|
|
41
|
+
// No-op
|
|
42
|
+
}
|
|
43
|
+
// Either the value isn't in storage yet or JSON parsing failed, so
|
|
44
|
+
// set to the initial value in both places
|
|
45
|
+
storage.setItem(key, JSON.stringify(initialValue));
|
|
46
|
+
return initialValue;
|
|
47
|
+
});
|
|
48
|
+
// Set in both places
|
|
49
|
+
const setValueAndStore = (0, _react.useCallback)((val)=>{
|
|
50
|
+
setValue(val);
|
|
51
|
+
storage.setItem(key, JSON.stringify(val));
|
|
52
|
+
}, [
|
|
53
|
+
setValue,
|
|
54
|
+
storage,
|
|
55
|
+
key
|
|
56
|
+
]);
|
|
57
|
+
return {
|
|
58
|
+
value,
|
|
59
|
+
setValue,
|
|
60
|
+
setValueAndStore
|
|
61
|
+
};
|
|
62
|
+
}
|
package/dist/cjs/utils/index.js
CHANGED
|
@@ -15,6 +15,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
15
15
|
value: true
|
|
16
16
|
});
|
|
17
17
|
_export_star(require("./axis"), exports);
|
|
18
|
+
_export_star(require("./browser-storage"), exports);
|
|
18
19
|
_export_star(require("./chart-actions"), exports);
|
|
19
20
|
_export_star(require("./combine-sx"), exports);
|
|
20
21
|
_export_star(require("./component-ids"), exports);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import React from 'react';
|
|
1
2
|
import { SnackbarProvider as NotistackProvider, ProviderContext as NotistackContext, SnackbarMessage, OptionsObject, SnackbarKey } from 'notistack';
|
|
2
3
|
export interface SnackbarContext extends NotistackContext {
|
|
3
4
|
errorSnackbar: EnqueueFunction;
|
|
@@ -14,10 +15,12 @@ type EnqueueFunction = (message: SnackbarMessage, options?: SnackbarOptions) =>
|
|
|
14
15
|
type SnackbarOptions = Omit<OptionsObject, 'variant'>;
|
|
15
16
|
/**
|
|
16
17
|
* Application-wide provider for showing snackbars/toasts.
|
|
18
|
+
* Customized to preserve formatting in error messages.
|
|
17
19
|
*/
|
|
18
|
-
export {
|
|
20
|
+
export declare function SnackbarProvider({ children, ...props }: React.ComponentProps<typeof NotistackProvider>): React.ReactElement;
|
|
19
21
|
/**
|
|
20
22
|
* Gets the SnackbarContext with methods for displaying snackbars/toasts.
|
|
21
23
|
*/
|
|
22
24
|
export declare function useSnackbar(): SnackbarContext;
|
|
25
|
+
export {};
|
|
23
26
|
//# sourceMappingURL=SnackbarProvider.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SnackbarProvider.d.ts","sourceRoot":"","sources":["../../src/context/SnackbarProvider.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"SnackbarProvider.d.ts","sourceRoot":"","sources":["../../src/context/SnackbarProvider.tsx"],"names":[],"mappings":"AAaA,OAAO,KAAsB,MAAM,OAAO,CAAC;AAE3C,OAAO,EACL,gBAAgB,IAAI,iBAAiB,EACrC,eAAe,IAAI,gBAAgB,EAEnC,eAAe,EACf,aAAa,EACb,WAAW,EAEZ,MAAM,WAAW,CAAC;AAEnB,MAAM,WAAW,eAAgB,SAAQ,gBAAgB;IACvD,aAAa,EAAE,eAAe,CAAC;IAC/B,YAAY,EAAE,eAAe,CAAC;IAC9B,eAAe,EAAE,eAAe,CAAC;IACjC,eAAe,EAAE,eAAe,CAAC;IAEjC;;;OAGG;IACH,iBAAiB,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,eAAe,KAAK,WAAW,CAAC;CAC/E;AAED,KAAK,eAAe,GAAG,CAAC,OAAO,EAAE,eAAe,EAAE,OAAO,CAAC,EAAE,eAAe,KAAK,WAAW,CAAC;AAE5F,KAAK,eAAe,GAAG,IAAI,CAAC,aAAa,EAAE,SAAS,CAAC,CAAC;AAEtD;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,EAC/B,QAAQ,EACR,GAAG,KAAK,EACT,EAAE,KAAK,CAAC,cAAc,CAAC,OAAO,iBAAiB,CAAC,GAAG,KAAK,CAAC,YAAY,CAerE;AAED;;GAEG;AACH,wBAAgB,WAAW,IAAI,eAAe,CA6B7C"}
|
|
@@ -10,11 +10,26 @@
|
|
|
10
10
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
11
|
// See the License for the specific language governing permissions and
|
|
12
12
|
// limitations under the License.
|
|
13
|
-
import {
|
|
14
|
-
import
|
|
13
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
14
|
+
import React, { useCallback } from 'react';
|
|
15
|
+
import { styled } from '@mui/material/styles';
|
|
16
|
+
import { SnackbarProvider as NotistackProvider, useSnackbar as useNotistack, MaterialDesignContent } from 'notistack';
|
|
15
17
|
/**
|
|
16
18
|
* Application-wide provider for showing snackbars/toasts.
|
|
17
|
-
|
|
19
|
+
* Customized to preserve formatting in error messages.
|
|
20
|
+
*/ export function SnackbarProvider({ children, ...props }) {
|
|
21
|
+
return /*#__PURE__*/ _jsx(NotistackProvider, {
|
|
22
|
+
...props,
|
|
23
|
+
Components: {
|
|
24
|
+
error: styled(MaterialDesignContent)(()=>({
|
|
25
|
+
'&.notistack-MuiContent-error': {
|
|
26
|
+
whiteSpace: 'pre-wrap'
|
|
27
|
+
}
|
|
28
|
+
}))
|
|
29
|
+
},
|
|
30
|
+
children: children
|
|
31
|
+
});
|
|
32
|
+
}
|
|
18
33
|
/**
|
|
19
34
|
* Gets the SnackbarContext with methods for displaying snackbars/toasts.
|
|
20
35
|
*/ export function useSnackbar() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/context/SnackbarProvider.tsx"],"sourcesContent":["// Copyright 2023 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport { useCallback } from 'react';\nimport {\n SnackbarProvider as NotistackProvider,\n ProviderContext as NotistackContext,\n useSnackbar as useNotistack,\n SnackbarMessage,\n OptionsObject,\n SnackbarKey,\n} from 'notistack';\n\nexport interface SnackbarContext extends NotistackContext {\n errorSnackbar: EnqueueFunction;\n infoSnackbar: EnqueueFunction;\n warningSnackbar: EnqueueFunction;\n successSnackbar: EnqueueFunction;\n\n /**\n * Useful for catch blocks where the error will be of type `unknown`, tries\n * to show the `message` property if passed an instance of `Error`.\n */\n exceptionSnackbar: (error: unknown, options?: SnackbarOptions) => SnackbarKey;\n}\n\ntype EnqueueFunction = (message: SnackbarMessage, options?: SnackbarOptions) => SnackbarKey;\n\ntype SnackbarOptions = Omit<OptionsObject, 'variant'>;\n\n/**\n * Application-wide provider for showing snackbars/toasts.\n */\nexport { NotistackProvider
|
|
1
|
+
{"version":3,"sources":["../../src/context/SnackbarProvider.tsx"],"sourcesContent":["// Copyright 2023 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport React, { useCallback } from 'react';\nimport { styled } from '@mui/material/styles';\nimport {\n SnackbarProvider as NotistackProvider,\n ProviderContext as NotistackContext,\n useSnackbar as useNotistack,\n SnackbarMessage,\n OptionsObject,\n SnackbarKey,\n MaterialDesignContent,\n} from 'notistack';\n\nexport interface SnackbarContext extends NotistackContext {\n errorSnackbar: EnqueueFunction;\n infoSnackbar: EnqueueFunction;\n warningSnackbar: EnqueueFunction;\n successSnackbar: EnqueueFunction;\n\n /**\n * Useful for catch blocks where the error will be of type `unknown`, tries\n * to show the `message` property if passed an instance of `Error`.\n */\n exceptionSnackbar: (error: unknown, options?: SnackbarOptions) => SnackbarKey;\n}\n\ntype EnqueueFunction = (message: SnackbarMessage, options?: SnackbarOptions) => SnackbarKey;\n\ntype SnackbarOptions = Omit<OptionsObject, 'variant'>;\n\n/**\n * Application-wide provider for showing snackbars/toasts.\n * Customized to preserve formatting in error messages.\n */\nexport function SnackbarProvider({\n children,\n ...props\n}: React.ComponentProps<typeof NotistackProvider>): React.ReactElement {\n return (\n <NotistackProvider\n {...props}\n Components={{\n error: styled(MaterialDesignContent)(() => ({\n '&.notistack-MuiContent-error': {\n whiteSpace: 'pre-wrap',\n },\n })),\n }}\n >\n {children}\n </NotistackProvider>\n );\n}\n\n/**\n * Gets the SnackbarContext with methods for displaying snackbars/toasts.\n */\nexport function useSnackbar(): SnackbarContext {\n const { enqueueSnackbar, closeSnackbar } = useNotistack();\n\n // Create variant-specific callbacks\n const errorSnackbar = useEnqueueFunction(enqueueSnackbar, 'error');\n const infoSnackbar = useEnqueueFunction(enqueueSnackbar, 'info');\n const warningSnackbar = useEnqueueFunction(enqueueSnackbar, 'warning');\n const successSnackbar = useEnqueueFunction(enqueueSnackbar, 'success');\n\n const exceptionSnackbar: SnackbarContext['exceptionSnackbar'] = useCallback(\n (error, options) => {\n // Try to use message prop, but fallback to a default message that\n // will just stringify the error provided\n const message = error instanceof Error ? error.message : `An unexpected error occurred: ${error}`;\n\n return errorSnackbar(message, options);\n },\n [errorSnackbar]\n );\n\n return {\n enqueueSnackbar,\n closeSnackbar,\n errorSnackbar,\n infoSnackbar,\n warningSnackbar,\n successSnackbar,\n exceptionSnackbar,\n };\n}\n\n// Helper to create a variant-specific enqueue function\nfunction useEnqueueFunction(\n enqueueSnackbar: NotistackContext['enqueueSnackbar'],\n variant: OptionsObject['variant']\n): EnqueueFunction {\n return useCallback(\n (message, options) => {\n const allOptions: OptionsObject = {\n ...options,\n variant,\n };\n return enqueueSnackbar(message, allOptions);\n },\n [enqueueSnackbar, variant]\n );\n}\n"],"names":["React","useCallback","styled","SnackbarProvider","NotistackProvider","useSnackbar","useNotistack","MaterialDesignContent","children","props","Components","error","whiteSpace","enqueueSnackbar","closeSnackbar","errorSnackbar","useEnqueueFunction","infoSnackbar","warningSnackbar","successSnackbar","exceptionSnackbar","options","message","Error","variant","allOptions"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;;AAEjC,OAAOA,SAASC,WAAW,QAAQ,QAAQ;AAC3C,SAASC,MAAM,QAAQ,uBAAuB;AAC9C,SACEC,oBAAoBC,iBAAiB,EAErCC,eAAeC,YAAY,EAI3BC,qBAAqB,QAChB,YAAY;AAmBnB;;;CAGC,GACD,OAAO,SAASJ,iBAAiB,EAC/BK,QAAQ,EACR,GAAGC,OAC4C;IAC/C,qBACE,KAACL;QACE,GAAGK,KAAK;QACTC,YAAY;YACVC,OAAOT,OAAOK,uBAAuB,IAAO,CAAA;oBAC1C,gCAAgC;wBAC9BK,YAAY;oBACd;gBACF,CAAA;QACF;kBAECJ;;AAGP;AAEA;;CAEC,GACD,OAAO,SAASH;IACd,MAAM,EAAEQ,eAAe,EAAEC,aAAa,EAAE,GAAGR;IAE3C,oCAAoC;IACpC,MAAMS,gBAAgBC,mBAAmBH,iBAAiB;IAC1D,MAAMI,eAAeD,mBAAmBH,iBAAiB;IACzD,MAAMK,kBAAkBF,mBAAmBH,iBAAiB;IAC5D,MAAMM,kBAAkBH,mBAAmBH,iBAAiB;IAE5D,MAAMO,oBAA0DnB,YAC9D,CAACU,OAAOU;QACN,kEAAkE;QAClE,yCAAyC;QACzC,MAAMC,UAAUX,iBAAiBY,QAAQZ,MAAMW,OAAO,GAAG,CAAC,8BAA8B,EAAEX,OAAO;QAEjG,OAAOI,cAAcO,SAASD;IAChC,GACA;QAACN;KAAc;IAGjB,OAAO;QACLF;QACAC;QACAC;QACAE;QACAC;QACAC;QACAC;IACF;AACF;AAEA,uDAAuD;AACvD,SAASJ,mBACPH,eAAoD,EACpDW,OAAiC;IAEjC,OAAOvB,YACL,CAACqB,SAASD;QACR,MAAMI,aAA4B;YAChC,GAAGJ,OAAO;YACVG;QACF;QACA,OAAOX,gBAAgBS,SAASG;IAClC,GACA;QAACZ;QAAiBW;KAAQ;AAE9B"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
type StorageTuple<T> = [T, (next: T) => void];
|
|
2
|
+
/**
|
|
3
|
+
* Just like useState but gets/sets the value in the browser's local storage.
|
|
4
|
+
* 'key' should be a constant string. 'initialValue' is returned when local
|
|
5
|
+
* storage does not have any data yet.
|
|
6
|
+
*/
|
|
7
|
+
export declare function useLocalStorage<T>(key: string, initialValue: T): StorageTuple<T>;
|
|
8
|
+
export {};
|
|
9
|
+
//# sourceMappingURL=browser-storage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser-storage.d.ts","sourceRoot":"","sources":["../../src/utils/browser-storage.ts"],"names":[],"mappings":"AAeA,KAAK,YAAY,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,IAAI,CAAC,CAAC;AAE9C;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,YAAY,EAAE,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC,CAGhF"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// Copyright 2023 The Perses Authors
|
|
2
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
3
|
+
// you may not use this file except in compliance with the License.
|
|
4
|
+
// You may obtain a copy of the License at
|
|
5
|
+
//
|
|
6
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
//
|
|
8
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
9
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
// See the License for the specific language governing permissions and
|
|
12
|
+
// limitations under the License.
|
|
13
|
+
import { useState, useCallback } from 'react';
|
|
14
|
+
/**
|
|
15
|
+
* Just like useState but gets/sets the value in the browser's local storage.
|
|
16
|
+
* 'key' should be a constant string. 'initialValue' is returned when local
|
|
17
|
+
* storage does not have any data yet.
|
|
18
|
+
*/ export function useLocalStorage(key, initialValue) {
|
|
19
|
+
const { value, setValueAndStore } = useStorage(window.localStorage, key, initialValue);
|
|
20
|
+
return [
|
|
21
|
+
value,
|
|
22
|
+
setValueAndStore
|
|
23
|
+
];
|
|
24
|
+
}
|
|
25
|
+
// Common functionality used by all storage hooks
|
|
26
|
+
function useStorage(storage, key, initialValue) {
|
|
27
|
+
// Use state so that changes cause the page to re-render
|
|
28
|
+
const [value, setValue] = useState(()=>{
|
|
29
|
+
try {
|
|
30
|
+
const json = storage.getItem(key);
|
|
31
|
+
if (json !== null) {
|
|
32
|
+
return JSON.parse(json);
|
|
33
|
+
}
|
|
34
|
+
} catch {
|
|
35
|
+
// No-op
|
|
36
|
+
}
|
|
37
|
+
// Either the value isn't in storage yet or JSON parsing failed, so
|
|
38
|
+
// set to the initial value in both places
|
|
39
|
+
storage.setItem(key, JSON.stringify(initialValue));
|
|
40
|
+
return initialValue;
|
|
41
|
+
});
|
|
42
|
+
// Set in both places
|
|
43
|
+
const setValueAndStore = useCallback((val)=>{
|
|
44
|
+
setValue(val);
|
|
45
|
+
storage.setItem(key, JSON.stringify(val));
|
|
46
|
+
}, [
|
|
47
|
+
setValue,
|
|
48
|
+
storage,
|
|
49
|
+
key
|
|
50
|
+
]);
|
|
51
|
+
return {
|
|
52
|
+
value,
|
|
53
|
+
setValue,
|
|
54
|
+
setValueAndStore
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
//# sourceMappingURL=browser-storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utils/browser-storage.ts"],"sourcesContent":["// Copyright 2023 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport { useState, useCallback } from 'react';\n\ntype StorageTuple<T> = [T, (next: T) => void];\n\n/**\n * Just like useState but gets/sets the value in the browser's local storage.\n * 'key' should be a constant string. 'initialValue' is returned when local\n * storage does not have any data yet.\n */\nexport function useLocalStorage<T>(key: string, initialValue: T): StorageTuple<T> {\n const { value, setValueAndStore } = useStorage(window.localStorage, key, initialValue);\n return [value, setValueAndStore];\n}\n\n// Common functionality used by all storage hooks\nfunction useStorage<T>(\n storage: Storage,\n key: string,\n initialValue: T\n): {\n setValueAndStore: (value: T) => void;\n setValue: (value: T) => void;\n value: T;\n} {\n // Use state so that changes cause the page to re-render\n const [value, setValue] = useState<T>(() => {\n try {\n const json = storage.getItem(key);\n if (json !== null) {\n return JSON.parse(json);\n }\n } catch {\n // No-op\n }\n\n // Either the value isn't in storage yet or JSON parsing failed, so\n // set to the initial value in both places\n storage.setItem(key, JSON.stringify(initialValue));\n return initialValue;\n });\n\n // Set in both places\n const setValueAndStore = useCallback(\n (val: T) => {\n setValue(val);\n storage.setItem(key, JSON.stringify(val));\n },\n [setValue, storage, key]\n );\n\n return { value, setValue, setValueAndStore };\n}\n"],"names":["useState","useCallback","useLocalStorage","key","initialValue","value","setValueAndStore","useStorage","window","localStorage","storage","setValue","json","getItem","JSON","parse","setItem","stringify","val"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC,SAASA,QAAQ,EAAEC,WAAW,QAAQ,QAAQ;AAI9C;;;;CAIC,GACD,OAAO,SAASC,gBAAmBC,GAAW,EAAEC,YAAe;IAC7D,MAAM,EAAEC,KAAK,EAAEC,gBAAgB,EAAE,GAAGC,WAAWC,OAAOC,YAAY,EAAEN,KAAKC;IACzE,OAAO;QAACC;QAAOC;KAAiB;AAClC;AAEA,iDAAiD;AACjD,SAASC,WACPG,OAAgB,EAChBP,GAAW,EACXC,YAAe;IAMf,wDAAwD;IACxD,MAAM,CAACC,OAAOM,SAAS,GAAGX,SAAY;QACpC,IAAI;YACF,MAAMY,OAAOF,QAAQG,OAAO,CAACV;YAC7B,IAAIS,SAAS,MAAM;gBACjB,OAAOE,KAAKC,KAAK,CAACH;YACpB;QACF,EAAE,OAAM;QACN,QAAQ;QACV;QAEA,mEAAmE;QACnE,0CAA0C;QAC1CF,QAAQM,OAAO,CAACb,KAAKW,KAAKG,SAAS,CAACb;QACpC,OAAOA;IACT;IAEA,qBAAqB;IACrB,MAAME,mBAAmBL,YACvB,CAACiB;QACCP,SAASO;QACTR,QAAQM,OAAO,CAACb,KAAKW,KAAKG,SAAS,CAACC;IACtC,GACA;QAACP;QAAUD;QAASP;KAAI;IAG1B,OAAO;QAAEE;QAAOM;QAAUL;IAAiB;AAC7C"}
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAaA,cAAc,QAAQ,CAAC;AACvB,cAAc,iBAAiB,CAAC;AAChC,cAAc,cAAc,CAAC;AAC7B,cAAc,iBAAiB,CAAC;AAChC,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAaA,cAAc,QAAQ,CAAC;AACvB,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,cAAc,CAAC;AAC7B,cAAc,iBAAiB,CAAC;AAChC,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC"}
|
package/dist/utils/index.js
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
// See the License for the specific language governing permissions and
|
|
12
12
|
// limitations under the License.
|
|
13
13
|
export * from './axis';
|
|
14
|
+
export * from './browser-storage';
|
|
14
15
|
export * from './chart-actions';
|
|
15
16
|
export * from './combine-sx';
|
|
16
17
|
export * from './component-ids';
|
package/dist/utils/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/index.ts"],"sourcesContent":["// Copyright 2023 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nexport * from './axis';\nexport * from './chart-actions';\nexport * from './combine-sx';\nexport * from './component-ids';\nexport * from './format';\nexport * from './theme-gen';\n"],"names":[],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC,cAAc,SAAS;AACvB,cAAc,kBAAkB;AAChC,cAAc,eAAe;AAC7B,cAAc,kBAAkB;AAChC,cAAc,WAAW;AACzB,cAAc,cAAc"}
|
|
1
|
+
{"version":3,"sources":["../../src/utils/index.ts"],"sourcesContent":["// Copyright 2023 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nexport * from './axis';\nexport * from './browser-storage';\nexport * from './chart-actions';\nexport * from './combine-sx';\nexport * from './component-ids';\nexport * from './format';\nexport * from './theme-gen';\n"],"names":[],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC,cAAc,SAAS;AACvB,cAAc,oBAAoB;AAClC,cAAc,kBAAkB;AAChC,cAAc,eAAe;AAC7B,cAAc,kBAAkB;AAChC,cAAc,WAAW;AACzB,cAAc,cAAc"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@perses-dev/components",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.53.0-beta.0",
|
|
4
4
|
"description": "Common UI components used across Perses features",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"homepage": "https://github.com/perses/perses/blob/main/README.md",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"@codemirror/lang-json": "^6.0.1",
|
|
34
34
|
"@fontsource/lato": "^4.5.10",
|
|
35
35
|
"@mui/x-date-pickers": "^7.23.1",
|
|
36
|
-
"@perses-dev/core": "0.
|
|
36
|
+
"@perses-dev/core": "0.53.0-beta.0",
|
|
37
37
|
"@tanstack/react-table": "^8.20.5",
|
|
38
38
|
"@uiw/react-codemirror": "^4.19.1",
|
|
39
39
|
"date-fns": "^4.1.0",
|