semaphor 0.0.11 → 0.0.12

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.
Files changed (2) hide show
  1. package/README.md +125 -0
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -0,0 +1,125 @@
1
+ Integrating dashboard into your React app is a straightforward three-step process.
2
+
3
+ ### **1) Install Semaphor Package**
4
+
5
+ Open your terminal in your project directory and run the following command. This command installs the `semaphor` package and adds it to your project dependencies.
6
+
7
+ ```markdown copy
8
+ npm i semaphor
9
+ ```
10
+
11
+ ### **2) Get Auth Token**
12
+
13
+ Before rendering the `Dashboard`, first acquire the `AuthToken`. This token enhances the security of your dashboard by restricting access to only users with the token. Make a POST fetch call as shown below.
14
+
15
+ ```jsx copy {1-3}
16
+ const DASHBOARD_ID = 'd_cf007a8b-19bc-46ad-8787-2915445b7b86'; // Replace with your actual dashboard ID
17
+ const DASHBOARD_SECRET = 'ds_f32f0b30-b7e1-40f9-ba6a-9804a5b9d635'; // Replace with your actual dashboard secret
18
+ const TOKEN_URL = 'https://semaphor.cloud/api/v1/token';
19
+
20
+ async function fetchToken() {
21
+ try {
22
+ const response = await fetch(TOKEN_URL, {
23
+ method: 'POST',
24
+ headers: {
25
+ 'Content-Type': 'application/json',
26
+ },
27
+ body: JSON.stringify({
28
+ dashboardId: DASHBOARD_ID,
29
+ dashboardSecret: DASHBOARD_SECRET,
30
+ }),
31
+ });
32
+ if (!response.ok) {
33
+ throw new Error(`HTTP error! status: ${response.status}`);
34
+ }
35
+
36
+ const token = await response.json();
37
+ if (token?.accessToken) {
38
+ setAuthToken(token);
39
+ }
40
+ } catch (error) {
41
+ console.error('There was an error!', error);
42
+ }
43
+ }
44
+ ```
45
+
46
+ ### **3) Initialize Dashboard**
47
+
48
+ Here is a simple example of a React component that demonstrates how to use the `Dashboard` component from the `semaphor` package. Make sure you import style.css either at the beginning of your component or in your global CSS file. This step is necessary for correctly loading your dashboard styles.
49
+
50
+ ```tsx copy {3, 11}
51
+ import { useEffect, useState } from 'react';
52
+ import { AuthToken, Dashboard } from 'semaphor';
53
+ import '../node_modules/semaphor/dist/style.css'; // IMPORTANT! Include the CSS file. This is the default style, you can customize it.
54
+
55
+ function App() {
56
+ const [authToken, setAuthToken] = useState<AuthToken>();
57
+
58
+ return (
59
+ <div>
60
+ <h2>My cool dashboard</h2>
61
+ <Dashboard authToken={authToken} id={DASHBOARD_ID} />
62
+ </div>
63
+ );
64
+ }
65
+ ```
66
+
67
+ ### **Full Code**
68
+
69
+ Here is the complete React code that uses the above steps. You can copy and paste this example into your own application.
70
+
71
+ ```tsx {3-7, 31, 43} copy filename="App.tsx"
72
+ import { useEffect, useState } from 'react';
73
+ import { AuthToken, Dashboard } from 'semaphor';
74
+ import '../node_modules/semaphor/dist/style.css'; // IMPORTANT! Include the CSS file. This is the default style, you can customize it.
75
+
76
+ const DASHBOARD_ID = 'd_cf007a8b-19bc-46ad-8787-2915445b7b86'; // Replace with your actual dashboard ID
77
+ const DASHBOARD_SECRET = 'ds_f32f0b30-b7e1-40f9-ba6a-9804a5b9d635'; // Replace with your actual dashboard secret
78
+ const TOKEN_URL = 'https://semaphor.cloud/api/v1/token';
79
+
80
+ function App() {
81
+ const [authToken, setAuthToken] = useState<AuthToken>();
82
+
83
+ useEffect(() => {
84
+ async function fetchToken() {
85
+ try {
86
+ const response = await fetch(TOKEN_URL, {
87
+ method: 'POST',
88
+ headers: {
89
+ 'Content-Type': 'application/json',
90
+ },
91
+ body: JSON.stringify({
92
+ dashboardId: DASHBOARD_ID,
93
+ dashboardSecret: DASHBOARD_SECRET,
94
+ }),
95
+ });
96
+ if (!response.ok) {
97
+ throw new Error(`HTTP error! status: ${response.status}`);
98
+ }
99
+
100
+ const token = await response.json();
101
+ if (token?.accessToken) {
102
+ setAuthToken(token);
103
+ }
104
+ } catch (error) {
105
+ console.error('There was an error!', error);
106
+ }
107
+ }
108
+ fetchToken();
109
+ }, []);
110
+
111
+ return (
112
+ <div>
113
+ <h2>My cool dashboard</h2>
114
+ <Dashboard authToken={authToken} id={DASHBOARD_ID} />
115
+ </div>
116
+ );
117
+ }
118
+
119
+ export default App;
120
+ ```
121
+
122
+ ### **Key Considerations**
123
+
124
+ - **Security Note:** Keep the `DASHBOARD_SECRET` secure and do not expose it in client-side code in production. This example is for demonstration purposes only. When deploying in production, obtain the authentication token from a secure, server-side environment.
125
+ - **Fetching the AuthToken:** Before the dashboard can be displayed, an authentication token must be fetched. The `useEffect` hook is used to perform this action when the component mounts.
package/package.json CHANGED
@@ -5,8 +5,8 @@
5
5
  "email": "support@semaphor.cloud"
6
6
  },
7
7
  "license": "MIT",
8
- "version": "0.0.11",
9
- "description": "Fully interactive and customizable dashboards for your apps. Embed analytics and insights into your app with ease.",
8
+ "version": "0.0.12",
9
+ "description": "Fully interactive and customizable dashboards for your apps.",
10
10
  "keywords": [
11
11
  "react",
12
12
  "semaphor",