-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample.tsx
More file actions
61 lines (57 loc) · 1.66 KB
/
example.tsx
File metadata and controls
61 lines (57 loc) · 1.66 KB
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
import { useState } from 'react';
import { Spinner } from './components/ui.spinner';
import { Modal } from './components/ui.modal';
import { useQuery } from '@tanstack/react-query';
import { getServerUrl } from './config';
import { hc } from 'hono/client';
import { useMonitor } from './services/monitor.use-monitor';
import type { ServerApi } from 'types.shared';
const serverUrl = getServerUrl();
const { $get } = hc<ServerApi>(serverUrl)['index'];
const useServerOkQuery = () => {
const { captureException } = useMonitor();
return useQuery({
queryKey: ['hello-from-server'],
queryFn: async () => {
return $get()
.then(async (res) => {
if (!res.ok) {
throw res;
}
return res.text();
})
.catch((error) => {
captureException(error);
throw error;
});
},
});
};
export const Example = () => {
const [isModalOpen, setIsModalOpen] = useState(false);
const serverOkQuery = useServerOkQuery();
return (
<div className="flex flex-col gap-4">
<h1>Hello from React!</h1>
<div>
<p>Server connection: {serverOkQuery.isSuccess ? '✅' : '❌'}</p>
</div>
<div>
<p>
Spinner: <Spinner isSpinning={true}>spinner example</Spinner>
</p>
</div>
<div>
<button
className="text-white bg-blue-700 p-2 rounded-md"
onClick={() => setIsModalOpen(true)}
>
show modal
</button>
<Modal isOpen={isModalOpen} onClose={() => setIsModalOpen(false)}>
<h2 className="bg-black text-white p-4">I am a modal!</h2>
</Modal>
</div>
</div>
);
};