@rivi-software/radiotools-sdk-react 0.0.0-stage → 0.2.2
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/README.md +34 -2
- package/package.json +34 -4
- package/src/hooks.generated.ts +74 -0
- package/src/index.ts +1 -0
package/README.md
CHANGED
|
@@ -1,3 +1,35 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @rivi-software/radiotools-sdk-react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
React hooks for the RadioTools listener API, wrapping [`@rivi-software/radiotools-sdk`](../sdk-typescript). Generated from `@rivi-software/radiotools-contract`; do not edit `src/hooks.generated.ts` by hand.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @rivi-software/radiotools-sdk-react @rivi-software/radiotools-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Requires React 18 or newer.
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```tsx
|
|
16
|
+
import { createStationClient } from "@rivi-software/radiotools-sdk";
|
|
17
|
+
import { useGetNowPlaying } from "@rivi-software/radiotools-sdk-react";
|
|
18
|
+
|
|
19
|
+
const client = createStationClient({ baseUrl, stationSlug, apiToken });
|
|
20
|
+
|
|
21
|
+
export function NowPlaying() {
|
|
22
|
+
const { data, error, loading, refresh } = useGetNowPlaying(client);
|
|
23
|
+
if (loading) return <p>Loading…</p>;
|
|
24
|
+
if (error) return <p>{error.message}</p>;
|
|
25
|
+
return <p>{data?.track?.title}</p>;
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Create the client once, outside components or in a memo, so the hooks keep a stable reference.
|
|
30
|
+
|
|
31
|
+
## Hooks
|
|
32
|
+
|
|
33
|
+
`useGetStation`, `useGetCurrentBroadcast`, `useGetNowPlaying`, `useGetWeather`, `useGetTraffic`, `useRecommendedPollInterval`. Each returns `{ data, error, loading, refresh }`.
|
|
34
|
+
|
|
35
|
+
For anything else, call the client directly.
|
package/package.json
CHANGED
|
@@ -1,6 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rivi-software/radiotools-sdk-react",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.2",
|
|
4
|
+
"description": "React hooks for @rivi-software/radiotools-sdk (generated from @rivi-software/radiotools-contract)",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.ts"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"src"
|
|
12
|
+
],
|
|
13
|
+
"sideEffects": false,
|
|
14
|
+
"scripts": {
|
|
15
|
+
"check": "tsc --noEmit"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@rivi-software/radiotools-sdk": "workspace:*"
|
|
19
|
+
},
|
|
20
|
+
"peerDependencies": {
|
|
21
|
+
"react": ">=18"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@types/react": "19.3.0",
|
|
25
|
+
"react": "19.3.0",
|
|
26
|
+
"typescript": "5.9.3"
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "https://github.com/rivi-software/radiotools-sdk.git",
|
|
34
|
+
"directory": "packages/sdk-react"
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/** Generated by @rivi-software/radiotools-sdk-generator — do not edit by hand. */
|
|
2
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
3
|
+
import type { StationClient } from "@rivi-software/radiotools-sdk";
|
|
4
|
+
|
|
5
|
+
type QueryState<T> = {
|
|
6
|
+
data: T | undefined;
|
|
7
|
+
error: Error | undefined;
|
|
8
|
+
loading: boolean;
|
|
9
|
+
refresh: () => void;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
function useStationQuery<T>(
|
|
13
|
+
client: StationClient,
|
|
14
|
+
_method: string,
|
|
15
|
+
read: () => Promise<T>,
|
|
16
|
+
): QueryState<T> {
|
|
17
|
+
const [data, setData] = useState<T | undefined>(undefined);
|
|
18
|
+
const [error, setError] = useState<Error | undefined>(undefined);
|
|
19
|
+
const [loading, setLoading] = useState(true);
|
|
20
|
+
const mounted = useRef(true);
|
|
21
|
+
|
|
22
|
+
const refresh = useCallback(() => {
|
|
23
|
+
setLoading(true);
|
|
24
|
+
void read()
|
|
25
|
+
.then((value) => {
|
|
26
|
+
if (!mounted.current) return;
|
|
27
|
+
setData(value);
|
|
28
|
+
setError(undefined);
|
|
29
|
+
})
|
|
30
|
+
.catch((cause: unknown) => {
|
|
31
|
+
if (!mounted.current) return;
|
|
32
|
+
setError(cause instanceof Error ? cause : new Error(String(cause)));
|
|
33
|
+
})
|
|
34
|
+
.finally(() => {
|
|
35
|
+
if (mounted.current) setLoading(false);
|
|
36
|
+
});
|
|
37
|
+
}, [read]);
|
|
38
|
+
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
mounted.current = true;
|
|
41
|
+
refresh();
|
|
42
|
+
return () => {
|
|
43
|
+
mounted.current = false;
|
|
44
|
+
};
|
|
45
|
+
}, [refresh]);
|
|
46
|
+
|
|
47
|
+
return { data, error, loading, refresh };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function useRecommendedPollInterval(client: StationClient): QueryState<number> {
|
|
51
|
+
return useStationQuery(client, "recommendedPollIntervalMs", () =>
|
|
52
|
+
client.recommendedPollIntervalMs(),
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function useGetStation(client: StationClient) {
|
|
57
|
+
return useStationQuery(client, "getStation", () => client.getStation());
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function useGetCurrentBroadcast(client: StationClient) {
|
|
61
|
+
return useStationQuery(client, "getCurrentBroadcast", () => client.getCurrentBroadcast());
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function useGetNowPlaying(client: StationClient) {
|
|
65
|
+
return useStationQuery(client, "getNowPlaying", () => client.getNowPlaying());
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function useGetWeather(client: StationClient) {
|
|
69
|
+
return useStationQuery(client, "getWeather", () => client.getWeather());
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function useGetTraffic(client: StationClient) {
|
|
73
|
+
return useStationQuery(client, "getTraffic", () => client.getTraffic());
|
|
74
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./hooks.generated";
|