@ouroboros/react-errors 1.0.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/LICENSE +21 -0
- package/README.md +47 -0
- package/example.png +0 -0
- package/index.d.ts +48 -0
- package/index.js +104 -0
- package/index.tsx +149 -0
- package/package.json +46 -0
- package/style.css +37 -0
- package/tsconfig.json +20 -0
- package/tslint.json +10 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Ouroboros Coding Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# @ouroboros/react-errors
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@ouroboros/react-errors) 
|
|
4
|
+
|
|
5
|
+
A React component that displays error messages.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
npm
|
|
9
|
+
```bash
|
|
10
|
+
npm install @ouroboros/react-errors
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Using
|
|
14
|
+
```javascript
|
|
15
|
+
import Errors, { addError } from '@ouroboros/react-errors';
|
|
16
|
+
|
|
17
|
+
function SomeComponent(props) {
|
|
18
|
+
return (
|
|
19
|
+
<div>
|
|
20
|
+
<button onClick={() => addError('Test Error!')}>Add Error</button>
|
|
21
|
+
</div>
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function App(props) {
|
|
26
|
+
return (
|
|
27
|
+
<SomeComponent />
|
|
28
|
+
<Errors />
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+

|
|
34
|
+
|
|
35
|
+
## Props
|
|
36
|
+
|
|
37
|
+
| Name | Type | Required | Description |
|
|
38
|
+
|--|--|--|--|
|
|
39
|
+
| position | "bottom" \| "top" | no | Default, "bottom" |
|
|
40
|
+
|
|
41
|
+
## addError
|
|
42
|
+
|
|
43
|
+
The `addError` function takes a string, or an object. In the case of an object the data will be JSON stringified and then displayed in a PRE elemenent to maintain the structure.
|
|
44
|
+
|
|
45
|
+
## Style
|
|
46
|
+
|
|
47
|
+
You can set your own styles using the #oc_errors ID, or import / include `node_modules/@ouroboros/react-errors/style.css`.
|
package/example.png
ADDED
|
Binary file
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Errors
|
|
3
|
+
*
|
|
4
|
+
* Shows error messages
|
|
5
|
+
*
|
|
6
|
+
* @author Chris Nasr <chris@ouroboroscoding.com>
|
|
7
|
+
* @copyright Ouroboros Coding Inc.
|
|
8
|
+
* @created 2024-09-24
|
|
9
|
+
*/
|
|
10
|
+
import PropTypes from 'prop-types';
|
|
11
|
+
import React from 'react';
|
|
12
|
+
export type ErrorsPositionType = 'bottom' | 'top';
|
|
13
|
+
export type ErrorsProps = {
|
|
14
|
+
position: ErrorsPositionType;
|
|
15
|
+
};
|
|
16
|
+
export type ErrorMessageType = {
|
|
17
|
+
_id: string;
|
|
18
|
+
content?: React.JSX.Element | React.JSX.Element[];
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Add Error
|
|
22
|
+
*
|
|
23
|
+
* Sends an error message to the Error component
|
|
24
|
+
*
|
|
25
|
+
* @name addError
|
|
26
|
+
* @access public
|
|
27
|
+
* @param error The string or object to display
|
|
28
|
+
* @returns void
|
|
29
|
+
*/
|
|
30
|
+
export declare function addError(error: string | object): void;
|
|
31
|
+
/**
|
|
32
|
+
* Errors
|
|
33
|
+
*
|
|
34
|
+
* @name Errors
|
|
35
|
+
* @access public
|
|
36
|
+
* @param Object props Properties passed to the component
|
|
37
|
+
* @returns React.Component
|
|
38
|
+
*/
|
|
39
|
+
declare function Errors({ position }: ErrorsProps): React.JSX.Element | null;
|
|
40
|
+
declare namespace Errors {
|
|
41
|
+
var propTypes: {
|
|
42
|
+
position: PropTypes.Requireable<string>;
|
|
43
|
+
};
|
|
44
|
+
var defaultProps: {
|
|
45
|
+
position: string;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
export default Errors;
|
package/index.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Errors
|
|
3
|
+
*
|
|
4
|
+
* Shows error messages
|
|
5
|
+
*
|
|
6
|
+
* @author Chris Nasr <chris@ouroboroscoding.com>
|
|
7
|
+
* @copyright Ouroboros Coding Inc.
|
|
8
|
+
* @created 2024-09-24
|
|
9
|
+
*/
|
|
10
|
+
// Ouroboros modules
|
|
11
|
+
import events from '@ouroboros/events';
|
|
12
|
+
import { afindi, isObject } from '@ouroboros/tools';
|
|
13
|
+
// NPM modules
|
|
14
|
+
import PropTypes from 'prop-types';
|
|
15
|
+
import React, { useState, useEffect } from 'react';
|
|
16
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
17
|
+
// Constants
|
|
18
|
+
const EVENT_NAME = 'oc_error_add';
|
|
19
|
+
/**
|
|
20
|
+
* Add Error
|
|
21
|
+
*
|
|
22
|
+
* Sends an error message to the Error component
|
|
23
|
+
*
|
|
24
|
+
* @name addError
|
|
25
|
+
* @access public
|
|
26
|
+
* @param error The string or object to display
|
|
27
|
+
* @returns void
|
|
28
|
+
*/
|
|
29
|
+
export function addError(error) {
|
|
30
|
+
// Send the error to the component
|
|
31
|
+
events.get(EVENT_NAME).trigger(error);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Errors
|
|
35
|
+
*
|
|
36
|
+
* @name Errors
|
|
37
|
+
* @access public
|
|
38
|
+
* @param Object props Properties passed to the component
|
|
39
|
+
* @returns React.Component
|
|
40
|
+
*/
|
|
41
|
+
export default function Errors({ position }) {
|
|
42
|
+
// State
|
|
43
|
+
const [errors, errorsSet] = useState([]);
|
|
44
|
+
// Load effect
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
// Subscribe to error messages
|
|
47
|
+
const oErrorEvent = events.get(EVENT_NAME).subscribe(msg => {
|
|
48
|
+
// Generate the new error ID
|
|
49
|
+
const o = { _id: uuidv4() };
|
|
50
|
+
// If it's an object, add it as a preformatted message
|
|
51
|
+
if (isObject(msg)) {
|
|
52
|
+
o.content = React.createElement("pre", null, JSON.stringify(msg, null, 4));
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
o.content = msg.split('\n').map((s, i) => React.createElement("p", { key: i }, s));
|
|
56
|
+
}
|
|
57
|
+
// Add it to the end of the errors list
|
|
58
|
+
errorsSet(l => {
|
|
59
|
+
const lNew = [...l];
|
|
60
|
+
lNew.push(o);
|
|
61
|
+
return lNew;
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
// Unsubscribe
|
|
65
|
+
return () => {
|
|
66
|
+
oErrorEvent.unsubscribe();
|
|
67
|
+
};
|
|
68
|
+
}, []);
|
|
69
|
+
// Remove an error by ID
|
|
70
|
+
function remove(_id) {
|
|
71
|
+
// Get the latest errors
|
|
72
|
+
errorsSet(l => {
|
|
73
|
+
// Find the index
|
|
74
|
+
const i = afindi(l, '_id', _id);
|
|
75
|
+
// If it exists
|
|
76
|
+
if (i > -1) {
|
|
77
|
+
const lNew = [...l];
|
|
78
|
+
lNew.splice(i, 1);
|
|
79
|
+
return lNew;
|
|
80
|
+
}
|
|
81
|
+
// Else, return existing
|
|
82
|
+
return l;
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
// If we have no errors, do nothing
|
|
86
|
+
if (errors.length === 0) {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
// Get the style from the position
|
|
90
|
+
const oStyle = (position === 'bottom') ? { bottom: '0' } : { top: '0' };
|
|
91
|
+
// Render all the current error messages
|
|
92
|
+
return (React.createElement("div", { id: "oc_errors", style: oStyle }, errors.map(o => React.createElement("div", { key: o._id, className: "error flexColumns" },
|
|
93
|
+
React.createElement("div", { className: "text" }, o.content),
|
|
94
|
+
React.createElement("div", { className: "close" },
|
|
95
|
+
React.createElement("button", { onClick: () => remove(o._id) }, "X"))))));
|
|
96
|
+
}
|
|
97
|
+
// Valid props
|
|
98
|
+
Errors.propTypes = {
|
|
99
|
+
position: PropTypes.oneOf(['bottom', 'top'])
|
|
100
|
+
};
|
|
101
|
+
// Default props
|
|
102
|
+
Errors.defaultProps = {
|
|
103
|
+
position: 'bottom'
|
|
104
|
+
};
|
package/index.tsx
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Errors
|
|
3
|
+
*
|
|
4
|
+
* Shows error messages
|
|
5
|
+
*
|
|
6
|
+
* @author Chris Nasr <chris@ouroboroscoding.com>
|
|
7
|
+
* @copyright Ouroboros Coding Inc.
|
|
8
|
+
* @created 2024-09-24
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
// Ouroboros modules
|
|
12
|
+
import events from '@ouroboros/events';
|
|
13
|
+
import { afindi, isObject } from '@ouroboros/tools';
|
|
14
|
+
|
|
15
|
+
// NPM modules
|
|
16
|
+
import PropTypes from 'prop-types';
|
|
17
|
+
import React, { useState, useEffect } from 'react';
|
|
18
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
19
|
+
|
|
20
|
+
// Constants
|
|
21
|
+
const EVENT_NAME = 'oc_error_add';
|
|
22
|
+
|
|
23
|
+
// Types
|
|
24
|
+
export type ErrorsPositionType = 'bottom' | 'top';
|
|
25
|
+
export type ErrorsProps = {
|
|
26
|
+
position: ErrorsPositionType
|
|
27
|
+
}
|
|
28
|
+
export type ErrorMessageType = {
|
|
29
|
+
_id: string,
|
|
30
|
+
content?: React.JSX.Element | React.JSX.Element[]
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Add Error
|
|
35
|
+
*
|
|
36
|
+
* Sends an error message to the Error component
|
|
37
|
+
*
|
|
38
|
+
* @name addError
|
|
39
|
+
* @access public
|
|
40
|
+
* @param error The string or object to display
|
|
41
|
+
* @returns void
|
|
42
|
+
*/
|
|
43
|
+
export function addError(error: string | object): void {
|
|
44
|
+
|
|
45
|
+
// Send the error to the component
|
|
46
|
+
events.get(EVENT_NAME).trigger(error);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Errors
|
|
51
|
+
*
|
|
52
|
+
* @name Errors
|
|
53
|
+
* @access public
|
|
54
|
+
* @param Object props Properties passed to the component
|
|
55
|
+
* @returns React.Component
|
|
56
|
+
*/
|
|
57
|
+
export default function Errors({
|
|
58
|
+
position
|
|
59
|
+
}: ErrorsProps) {
|
|
60
|
+
|
|
61
|
+
// State
|
|
62
|
+
const [ errors, errorsSet ] = useState<ErrorMessageType[]>([]);
|
|
63
|
+
|
|
64
|
+
// Load effect
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
|
|
67
|
+
// Subscribe to error messages
|
|
68
|
+
const oErrorEvent = events.get(EVENT_NAME).subscribe(msg => {
|
|
69
|
+
|
|
70
|
+
// Generate the new error ID
|
|
71
|
+
const o: ErrorMessageType = { _id: uuidv4() }
|
|
72
|
+
|
|
73
|
+
// If it's an object, add it as a preformatted message
|
|
74
|
+
if(isObject(msg)) {
|
|
75
|
+
o.content = <pre>{JSON.stringify(msg, null, 4)}</pre>
|
|
76
|
+
} else {
|
|
77
|
+
o.content = (msg as string).split('\n').map((s,i) => <p key={i}>{s}</p>);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Add it to the end of the errors list
|
|
81
|
+
errorsSet(l => {
|
|
82
|
+
const lNew = [ ...l ];
|
|
83
|
+
lNew.push(o);
|
|
84
|
+
return lNew;
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// Unsubscribe
|
|
89
|
+
return () => {
|
|
90
|
+
oErrorEvent.unsubscribe();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
}, []);
|
|
94
|
+
|
|
95
|
+
// Remove an error by ID
|
|
96
|
+
function remove(_id: string) {
|
|
97
|
+
|
|
98
|
+
// Get the latest errors
|
|
99
|
+
errorsSet(l => {
|
|
100
|
+
|
|
101
|
+
// Find the index
|
|
102
|
+
const i = afindi(l, '_id', _id);
|
|
103
|
+
|
|
104
|
+
// If it exists
|
|
105
|
+
if(i > -1) {
|
|
106
|
+
const lNew = [ ...l ];
|
|
107
|
+
lNew.splice(i, 1);
|
|
108
|
+
return lNew;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Else, return existing
|
|
112
|
+
return l;
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// If we have no errors, do nothing
|
|
117
|
+
if(errors.length === 0) {
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Get the style from the position
|
|
122
|
+
const oStyle = (position === 'bottom') ? { bottom: '0' } : { top: '0' }
|
|
123
|
+
|
|
124
|
+
// Render all the current error messages
|
|
125
|
+
return (
|
|
126
|
+
<div id="oc_errors" style={oStyle}>
|
|
127
|
+
{errors.map(o =>
|
|
128
|
+
<div key={o._id} className="error flexColumns">
|
|
129
|
+
<div className="text">
|
|
130
|
+
{o.content}
|
|
131
|
+
</div>
|
|
132
|
+
<div className="close">
|
|
133
|
+
<button onClick={() => remove(o._id)}>X</button>
|
|
134
|
+
</div>
|
|
135
|
+
</div>
|
|
136
|
+
)}
|
|
137
|
+
</div>
|
|
138
|
+
)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Valid props
|
|
142
|
+
Errors.propTypes = {
|
|
143
|
+
position: PropTypes.oneOf([ 'bottom', 'top' ])
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Default props
|
|
147
|
+
Errors.defaultProps = {
|
|
148
|
+
position: 'bottom'
|
|
149
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ouroboros/react-errors",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Errors Component. Shows error messages sent to it.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tslint -p tsconfig.json && tsc",
|
|
9
|
+
"clean": "rm index.js index.d.ts",
|
|
10
|
+
"for_local": "npm install; tslint -p tsconfig.json && tsc; rm -Rf node_modules/@types/react node_modules/@emotion/react node_modules/@emotion/styled node_modules/react node_modules/react-dom",
|
|
11
|
+
"test": "echo \"Error: no test specified.\" && exit 1"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/ouroboroscoding/react-errors.git"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"@ouroboros/react-errors",
|
|
19
|
+
"react"
|
|
20
|
+
],
|
|
21
|
+
"author": "Chris Nasr <chris@ouroboroscoding.com>",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/ouroboroscoding/react-errors/issues"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/node": "^18.14.0",
|
|
28
|
+
"@types/react": "^18.0.28",
|
|
29
|
+
"@types/uuid": "^10.0.0",
|
|
30
|
+
"tslint": "^6.1.3",
|
|
31
|
+
"tslint-config-prettier": "^1.18.0",
|
|
32
|
+
"typescript": "^4.8.3"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@ouroboros/events": "^1.0.1",
|
|
36
|
+
"@ouroboros/tools": "^0.5.4",
|
|
37
|
+
"prop-types": "^15.8.1",
|
|
38
|
+
"uuid": "^10.0.0"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"@emotion/react": "^11.10.6",
|
|
42
|
+
"@emotion/styled": "^11.10.6",
|
|
43
|
+
"react": "^17.0.0 || ^18.0.0",
|
|
44
|
+
"react-dom": "^17.0.0 || ^18.0.0"
|
|
45
|
+
}
|
|
46
|
+
}
|
package/style.css
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
#oc_errors {
|
|
2
|
+
left: 0;
|
|
3
|
+
padding: 5px;
|
|
4
|
+
position: absolute;
|
|
5
|
+
right: 0;
|
|
6
|
+
z-index: 100;
|
|
7
|
+
|
|
8
|
+
div.error {
|
|
9
|
+
background: #f00;
|
|
10
|
+
border-radius: 10px;
|
|
11
|
+
box-shadow: 5px 5px 8px #868686;
|
|
12
|
+
display: flex;
|
|
13
|
+
flex-direction: row;
|
|
14
|
+
margin: 10px 5px;
|
|
15
|
+
|
|
16
|
+
div.text {
|
|
17
|
+
color: #fff;
|
|
18
|
+
flex: 1 1 0;
|
|
19
|
+
font-weight: bold;
|
|
20
|
+
overflow: auto;
|
|
21
|
+
padding: 10px;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
div.close {
|
|
25
|
+
flex: 0 0 auto;
|
|
26
|
+
padding: 5px;
|
|
27
|
+
|
|
28
|
+
button {
|
|
29
|
+
border-radius: 10px;
|
|
30
|
+
height: 20px;
|
|
31
|
+
padding: 0;
|
|
32
|
+
text-align: center;
|
|
33
|
+
width: 20px;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"allowJs": true,
|
|
4
|
+
"allowSyntheticDefaultImports": true,
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"esModuleInterop": true,
|
|
7
|
+
"isolatedModules": true,
|
|
8
|
+
"jsx": "react",
|
|
9
|
+
"lib": ["es2017", "dom"],
|
|
10
|
+
"types": ["react", "node"],
|
|
11
|
+
"moduleResolution": "node",
|
|
12
|
+
"noEmit": false,
|
|
13
|
+
"resolveJsonModule": true,
|
|
14
|
+
"strict": true,
|
|
15
|
+
"strictPropertyInitialization": false,
|
|
16
|
+
"target": "esnext"
|
|
17
|
+
},
|
|
18
|
+
"include": ["./"],
|
|
19
|
+
"exclude": []
|
|
20
|
+
}
|