Skip to content

Commit 36234eb

Browse files
Demo connect buttong
1 parent b013f54 commit 36234eb

File tree

1 file changed

+43
-9
lines changed
  • demos/react-supabase-todolist/src/app/views

1 file changed

+43
-9
lines changed

demos/react-supabase-todolist/src/app/views/layout.tsx

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { LOGIN_ROUTE, SQL_CONSOLE_ROUTE, TODO_LISTS_ROUTE } from '@/app/router';
2+
import { useNavigationPanel } from '@/components/navigation/NavigationPanelContext';
3+
import { useSupabase } from '@/components/providers/SystemProvider';
14
import ChecklistRtlIcon from '@mui/icons-material/ChecklistRtl';
25
import ExitToAppIcon from '@mui/icons-material/ExitToApp';
36
import MenuIcon from '@mui/icons-material/Menu';
@@ -17,16 +20,15 @@ import {
1720
ListItemButton,
1821
ListItemIcon,
1922
ListItemText,
23+
Menu,
24+
MenuItem,
2025
Toolbar,
2126
Typography,
2227
styled
2328
} from '@mui/material';
24-
import React from 'react';
2529
import { usePowerSync, useStatus } from '@powersync/react';
30+
import React from 'react';
2631
import { useNavigate } from 'react-router-dom';
27-
import { useSupabase } from '@/components/providers/SystemProvider';
28-
import { useNavigationPanel } from '@/components/navigation/NavigationPanelContext';
29-
import { LOGIN_ROUTE, SQL_CONSOLE_ROUTE, TODO_LISTS_ROUTE } from '@/app/router';
3032

3133
export default function ViewsLayout({ children }: { children: React.ReactNode }) {
3234
const powerSync = usePowerSync();
@@ -37,6 +39,8 @@ export default function ViewsLayout({ children }: { children: React.ReactNode })
3739
const [openDrawer, setOpenDrawer] = React.useState(false);
3840
const { title } = useNavigationPanel();
3941

42+
const [connectionAnchor, setConnectionAnchor] = React.useState<null | HTMLElement>(null);
43+
4044
const NAVIGATION_ITEMS = React.useMemo(
4145
() => [
4246
{
@@ -72,16 +76,47 @@ export default function ViewsLayout({ children }: { children: React.ReactNode })
7276
color="inherit"
7377
aria-label="menu"
7478
sx={{ mr: 2 }}
75-
onClick={() => setOpenDrawer(!openDrawer)}
76-
>
79+
onClick={() => setOpenDrawer(!openDrawer)}>
7780
<MenuIcon />
7881
</IconButton>
7982
<Box sx={{ flexGrow: 1 }}>
8083
<Typography>{title}</Typography>
8184
</Box>
8285
<NorthIcon sx={{ marginRight: '-10px' }} color={status?.dataFlowStatus.uploading ? 'primary' : 'inherit'} />
8386
<SouthIcon color={status?.dataFlowStatus.downloading ? 'primary' : 'inherit'} />
84-
{status?.connected ? <WifiIcon /> : <SignalWifiOffIcon />}
87+
<Box
88+
sx={{ cursor: 'pointer' }}
89+
onClick={(event) => {
90+
setConnectionAnchor(event.currentTarget);
91+
}}>
92+
{status?.connected ? <WifiIcon /> : <SignalWifiOffIcon />}
93+
{/* Allows for manual connection and disconnect for testing purposes */}
94+
<Menu
95+
id="connection-menu"
96+
anchorEl={connectionAnchor}
97+
open={Boolean(connectionAnchor)}
98+
onClose={() => setConnectionAnchor(null)}>
99+
{status?.connected ? (
100+
<MenuItem
101+
onClick={(event) => {
102+
event.stopPropagation();
103+
setConnectionAnchor(null);
104+
powerSync.disconnect();
105+
}}>
106+
Disconnect
107+
</MenuItem>
108+
) : supabase ? (
109+
<MenuItem
110+
onClick={(event) => {
111+
event.stopPropagation();
112+
setConnectionAnchor(null);
113+
powerSync.connect(supabase);
114+
}}>
115+
Connect
116+
</MenuItem>
117+
) : null}
118+
</Menu>
119+
</Box>
85120
</Toolbar>
86121
</S.TopBar>
87122
<Drawer anchor={'left'} open={openDrawer} onClose={() => setOpenDrawer(false)}>
@@ -95,8 +130,7 @@ export default function ViewsLayout({ children }: { children: React.ReactNode })
95130
await item.beforeNavigate?.();
96131
navigate(item.path);
97132
setOpenDrawer(false);
98-
}}
99-
>
133+
}}>
100134
<ListItemIcon>{item.icon()}</ListItemIcon>
101135
<ListItemText primary={item.title} />
102136
</ListItemButton>

0 commit comments

Comments
 (0)