Skip to content

Commit 0d0a542

Browse files
committed
Poc for completing logout loop.
1 parent 9f3cffd commit 0d0a542

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
import React from 'react';
2525
import { usePowerSync, useStatus } from '@powersync/react';
2626
import { useNavigate } from 'react-router-dom';
27-
import { useSupabase } from '@/components/providers/SystemProvider';
27+
import { useReset, useSupabase } from '@/components/providers/SystemProvider';
2828
import { useNavigationPanel } from '@/components/navigation/NavigationPanelContext';
2929
import { DEFAULT_ENTRY_ROUTE, LOGIN_ROUTE, SQL_CONSOLE_ROUTE, TODO_LISTS_ROUTE } from '@/app/router';
3030
import { setSyncEnabled } from '@/library/powersync/SyncMode';
@@ -35,6 +35,7 @@ export default function ViewsLayout({ children }: { children: React.ReactNode })
3535
const status = useStatus();
3636
const supabase = useSupabase();
3737
const navigate = useNavigate();
38+
const reset = useReset();
3839
const [authText, setAuthText] = React.useState(supabase?.currentSession ? 'Sign Out' : 'Sign In');
3940

4041
const [openDrawer, setOpenDrawer] = React.useState(false);
@@ -63,6 +64,8 @@ export default function ViewsLayout({ children }: { children: React.ReactNode })
6364
setSyncEnabled(powerSync.database.name, false);
6465

6566
await switchToLocalSchema(powerSync);
67+
68+
reset?.();
6669
navigate(DEFAULT_ENTRY_ROUTE);
6770
return true;
6871
}

demos/react-supabase-todolist-optional-sync/src/components/providers/SystemProvider.tsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import { CircularProgress } from '@mui/material';
44
import { PowerSyncContext } from '@powersync/react';
55
import { PowerSyncDatabase } from '@powersync/web';
66
import Logger from 'js-logger';
7-
import React, { Suspense } from 'react';
7+
import React, { Suspense, useEffect } from 'react';
88
import { NavigationPanelContextProvider } from '../navigation/NavigationPanelContext';
99
import { getSyncEnabled } from '@/library/powersync/SyncMode';
1010

1111
const SupabaseContext = React.createContext<SupabaseConnector | null>(null);
1212
export const useSupabase = () => React.useContext(SupabaseContext);
1313

14+
const ResetContext = React.createContext<(() => void) | null>(null);
15+
export const useReset = () => React.useContext(ResetContext);
16+
1417
const dbName = 'example.db';
1518
const syncEnabled = getSyncEnabled(dbName);
1619

@@ -23,7 +26,7 @@ const db = new PowerSyncDatabase({
2326

2427
export const SystemProvider = ({ children }: { children: React.ReactNode }) => {
2528
const [connector] = React.useState(new SupabaseConnector());
26-
const [powerSync] = React.useState(db);
29+
const [powerSync, setPowersync] = React.useState(db);
2730

2831
React.useEffect(() => {
2932
// Linting thinks this is a hook due to it's name
@@ -53,11 +56,16 @@ export const SystemProvider = ({ children }: { children: React.ReactNode }) => {
5356

5457
return (
5558
<Suspense fallback={<CircularProgress />}>
56-
<PowerSyncContext.Provider value={powerSync}>
57-
<SupabaseContext.Provider value={connector}>
58-
<NavigationPanelContextProvider>{children}</NavigationPanelContextProvider>
59-
</SupabaseContext.Provider>
60-
</PowerSyncContext.Provider>
59+
<ResetContext.Provider
60+
value={() => {
61+
setPowersync(new PowerSyncDatabase({ schema: makeSchema(syncEnabled), database: { dbFilename: dbName } }));
62+
}}>
63+
<PowerSyncContext.Provider value={powerSync}>
64+
<SupabaseContext.Provider value={connector}>
65+
<NavigationPanelContextProvider>{children}</NavigationPanelContextProvider>
66+
</SupabaseContext.Provider>
67+
</PowerSyncContext.Provider>
68+
</ResetContext.Provider>
6169
</Suspense>
6270
);
6371
};

0 commit comments

Comments
 (0)