forked from viamrobotics/viam-typescript-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
93 lines (84 loc) · 2.06 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
*/
import React, {PropsWithoutRef} from 'react';
import {useState} from 'react';
import {
Button,
FlatList,
SafeAreaView,
StatusBar,
StyleSheet,
Text,
View,
} from 'react-native';
import * as VIAM from '@viamrobotics/sdk';
type ResourceNameViewProps = PropsWithoutRef<{
resourceName: VIAM.ResourceName;
}>;
function ResourceNameView({
resourceName,
}: ResourceNameViewProps): React.JSX.Element {
return (
<View style={[styles.listItem]}>
<Text style={[styles.listItemTitle]}>{resourceName.name}</Text>
<Text style={[styles.listItemSubtitle]}>
{resourceName.namespace}/{resourceName.type}/{resourceName.subtype}
</Text>
</View>
);
}
function App(): React.JSX.Element {
const [connected, setConnected] = useState<boolean>(false);
const [resourceNames, setResourceNames] = useState<VIAM.ResourceName[]>([]);
async function connect() {
const host = '<HOST>';
const client = await VIAM.createRobotClient({
host,
credential: {
type: 'api-key',
payload: '<API_KEY>',
},
authEntity: '<API_KEY_ID>',
signalingAddress: 'https://app.viam.com:443',
});
setConnected(true);
const rns = await client.resourceNames();
setResourceNames(rns.sort((a, b) => (a.name < b.name ? -1 : 1)));
}
return (
<SafeAreaView>
<StatusBar />
<Button
title={connected ? 'Connected' : 'Connect'}
onPress={connect}
disabled={connected}
/>
<FlatList
data={resourceNames}
renderItem={({item}) => <ResourceNameView resourceName={item} />}
keyExtractor={item =>
`${item.namespace}/${item.type}/${item.subtype}:${item.name}`
}
/>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
listItem: {
paddingVertical: 8,
paddingHorizontal: 24,
},
listItemTitle: {
fontSize: 16,
fontWeight: '600',
},
listItemSubtitle: {
fontSize: 12,
fontWeight: '400',
},
});
export default App;