Skip to content

Commit 0cbe200

Browse files
committed
Add todolist demo
1 parent 6874f93 commit 0cbe200

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

demos/react-supabase-todolist-sync-streams/src/app/views/todo-lists/edit/page.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ const TodoEditSection = () => {
5858
created_at DESC,
5959
id
6060
`,
61-
[listID]
61+
[listID],
62+
{ streams: [{ name: 'todos', parameters: { list: listID } }] }
6263
);
6364

6465
const [showPrompt, setShowPrompt] = React.useState(false);
@@ -96,11 +97,13 @@ const TodoEditSection = () => {
9697
<AddIcon />
9798
</S.FloatingActionButton>
9899
<Box>
99-
<List dense={false}>
100-
{todos.map((r) => (
101-
<TodoItemWidget key={r.id} id={r.id} description={r.description} isComplete={r.completed == 1} />
102-
))}
103-
</List>
100+
{todos && (
101+
<List dense={false}>
102+
{todos.map((r) => (
103+
<TodoItemWidget key={r.id} id={r.id} description={r.description} isComplete={r.completed == 1} />
104+
))}
105+
</List>
106+
)}
104107
</Box>
105108
{/* TODO use a dialog service in future, this is just a simple example app */}
106109
<Dialog

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import { AppSchema, ListRecord, LISTS_TABLE, TODOS_TABLE } from '@/library/power
33
import { SupabaseConnector } from '@/library/powersync/SupabaseConnector';
44
import { CircularProgress } from '@mui/material';
55
import { PowerSyncContext } from '@powersync/react';
6-
import { createBaseLogger, DifferentialWatchedQuery, LogLevel, PowerSyncDatabase } from '@powersync/web';
6+
import {
7+
createBaseLogger,
8+
DifferentialWatchedQuery,
9+
LogLevel,
10+
PowerSyncDatabase,
11+
SyncClientImplementation
12+
} from '@powersync/web';
713
import React, { Suspense } from 'react';
814
import { NavigationPanelContextProvider } from '../navigation/NavigationPanelContext';
915

@@ -68,7 +74,7 @@ export const SystemProvider = ({ children }: { children: React.ReactNode }) => {
6874
const l = connector.registerListener({
6975
initialized: () => {},
7076
sessionStarted: () => {
71-
powerSync.connect(connector);
77+
powerSync.connect(connector, { clientImplementation: SyncClientImplementation.RUST });
7278
}
7379
});
7480

demos/react-supabase-todolist-sync-streams/src/components/widgets/TodoListsWidget.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { List } from '@mui/material';
2-
import { useWatchedQuerySubscription } from '@powersync/react';
2+
import { usePowerSync, useStatus, useWatchedQuerySubscription } from '@powersync/react';
33
import { useQueryStore } from '../providers/SystemProvider';
44
import { ListItemWidget } from './ListItemWidget';
55

@@ -12,6 +12,8 @@ const description = (total: number, completed: number = 0) => {
1212
};
1313

1414
export function TodoListsWidget(props: TodoListsWidgetProps) {
15+
const db = usePowerSync();
16+
const status = useStatus();
1517
const queries = useQueryStore();
1618
const { data: listRecords, isLoading } = useWatchedQuerySubscription(queries!.lists);
1719

@@ -22,12 +24,22 @@ export function TodoListsWidget(props: TodoListsWidgetProps) {
2224
return (
2325
<List dense={false}>
2426
{listRecords.map((r) => {
27+
const listStatus = status.forStream({ name: 'todos', parameters: { list: r.id } });
28+
let listDescription = '';
29+
if (listStatus == null || !listStatus.subscription.active) {
30+
listDescription = 'Items in this list not loaded - open list for details.';
31+
} else if (!listStatus.subscription.hasSynced) {
32+
listDescription = 'Loading items in this list...';
33+
} else {
34+
listDescription = description(r.total_tasks, r.completed_tasks);
35+
}
36+
2537
return (
2638
<ListItemWidget
2739
key={r.id}
2840
id={r.id}
2941
title={r.name ?? ''}
30-
description={description(r.total_tasks, r.completed_tasks)}
42+
description={listDescription}
3143
selected={r.id == props.selectedId}
3244
/>
3345
);

0 commit comments

Comments
 (0)