1
- import { useConnectivitySignal } from "@solid-primitives/connectivity"
2
1
import {
3
2
DragDropProvider ,
4
3
DragDropSensors ,
@@ -7,87 +6,28 @@ import {
7
6
SortableProvider ,
8
7
closestCenter ,
9
8
} from "@thisbeyond/solid-dnd"
10
- import { Client , EventQueue , ShoppingList , type ShoppingListEvent , type ShoppingListItem , trimAndUppercase } from "lib"
9
+ import { type ShoppingListItem , trimAndUppercase } from "lib"
11
10
import { animate } from "motion/mini"
12
- import { For , type JSX , Show , createEffect , createSignal , onCleanup , onMount } from "solid-js"
13
- import { createStore , reconcile } from "solid-js/store"
11
+ import { For , type JSX , Show , createSignal , onCleanup , onMount } from "solid-js"
14
12
import { Motion , Presence } from "solid-motionone"
15
13
import { TransitionGroup } from "solid-transition-group"
16
- import { ConnectionWarning } from "~/components/ConnectionWarning"
17
14
import { ItemRow } from "~/components/ItemRow"
18
- import { BrowserServerConnection } from "~/lib/browser-server-connection "
15
+ import { isConnected , myShoppingList , shopping } from "~/lib/shopping-list "
19
16
import { isInputField } from "~/lib/type-guards"
20
17
import IconCaretRight from "~icons/radix-icons/caret-right"
21
-
22
- function createClient ( ) {
23
- const [ connectionStatus , setConnectionStatus ] = createSignal ( { authenticated : true , connected : true } )
24
-
25
- const serverConnection = new BrowserServerConnection ( ( value ) => setConnectionStatus ( value ) )
26
-
27
- const initialShoppingListString = localStorage . getItem ( "main-shopping-list" )
28
- const initialShoppingList = initialShoppingListString
29
- ? ( JSON . parse ( initialShoppingListString ) as ShoppingListItem [ ] )
30
- : [ ]
31
-
32
- const [ list , setStore ] = createStore ( { items : initialShoppingList } )
33
-
34
- const shoppingList = new ShoppingList ( structuredClone ( initialShoppingList ) , ( newList ) => {
35
- setStore ( "items" , reconcile ( structuredClone ( newList ) ) )
36
- localStorage . setItem ( "main-shopping-list" , JSON . stringify ( newList ) )
37
- } )
38
-
39
- const storedRemoteShoppingListCopyString = localStorage . getItem ( "remote-shopping-list" )
40
- const storedRemoteShoppingListCopy = storedRemoteShoppingListCopyString
41
- ? ( JSON . parse ( storedRemoteShoppingListCopyString ) as ShoppingListItem [ ] )
42
- : [ ]
43
- const remoteShoppingListCopy = new ShoppingList ( storedRemoteShoppingListCopy , ( newList ) => {
44
- localStorage . setItem ( "remote-shopping-list" , JSON . stringify ( newList ) )
45
- } )
46
-
47
- const storedEventQueueString = localStorage . getItem ( "event-queue" )
48
- const storedEventQueue = storedEventQueueString ? ( JSON . parse ( storedEventQueueString ) as ShoppingListEvent [ ] ) : [ ]
49
- const eventQueue = new EventQueue < ShoppingListEvent > ( storedEventQueue , ( events ) => {
50
- localStorage . setItem ( "event-queue" , JSON . stringify ( events ) )
51
- } )
52
-
53
- const client = new Client ( {
54
- shoppingList,
55
- remoteShoppingListCopy,
56
- serverConnection,
57
- eventQueue,
58
- } )
59
-
60
- const isOnline = useConnectivitySignal ( )
61
-
62
- createEffect ( ( ) => {
63
- if ( isOnline ( ) ) client . connect ( )
64
- else serverConnection . disconnect ( )
65
- } )
66
-
67
- return { connectionStatus, client, items : list . items }
68
- }
18
+ import { ConnectionWarning } from "./ConnectionWarning"
69
19
70
20
const ITEM_HEIGHT = 40
71
21
const ITEM_HEIGHT_PX = `${ ITEM_HEIGHT } px`
72
22
73
23
export function Home ( props : { softwareKeyboardShown : boolean } ) {
74
- const { connectionStatus, client, items } = createClient ( )
75
-
76
- const sortedList = ( ) => {
77
- return [ ...items ] . sort ( ( a , b ) => a . position - b . position )
78
- }
24
+ const sortedList = ( ) => myShoppingList . toSorted ( ( a , b ) => a . position - b . position )
79
25
80
26
const activeList = ( ) => sortedList ( ) . filter ( ( item ) => ! item . checked )
81
27
const checkedList = ( ) => sortedList ( ) . filter ( ( item ) => item . checked )
82
28
83
29
const [ showChecked , setShowChecked ] = createSignal ( false )
84
30
85
- const actions = {
86
- deleteItem : client . deleteItem . bind ( client ) ,
87
- setChecked : client . setItemChecked . bind ( client ) ,
88
- renameItem : client . renameItem . bind ( client ) ,
89
- }
90
-
91
31
const [ activeItem , setActiveItem ] = createSignal < ShoppingListItem | null > ( null )
92
32
93
33
const onDragStart = ( { draggable } : DragEvent ) => {
@@ -105,16 +45,16 @@ export function Home(props: { softwareKeyboardShown: boolean }) {
105
45
const fromIndex = currentIds . indexOf ( draggable . id as string )
106
46
const toIndex = currentIds . indexOf ( droppable . id as string )
107
47
108
- const fromPosition = activeList ( ) [ fromIndex ] . position ?? fromIndex
109
- const toPosition = activeList ( ) [ toIndex ] . position ?? toIndex
48
+ const fromPosition = activeList ( ) [ fromIndex ] . position
49
+ const toPosition = activeList ( ) [ toIndex ] . position
110
50
111
51
if ( fromIndex !== toIndex ) {
112
- client . moveItem ( draggable . id as string , { fromPosition, toPosition } )
52
+ shopping . moveItem ( draggable . id as string , { fromPosition, toPosition } )
113
53
}
114
54
}
115
55
}
116
56
117
- const ids = ( ) => activeList ( ) . map ( ( { id } ) => id )
57
+ const ids = ( ) => activeList ( ) . map ( ( item ) => item . id )
118
58
119
59
const [ scrollRef , setScrollRef ] = createSignal < HTMLElement | null > ( null )
120
60
@@ -138,10 +78,7 @@ export function Home(props: { softwareKeyboardShown: boolean }) {
138
78
return (
139
79
< >
140
80
< div style = { props . softwareKeyboardShown ? { display : "none" } : { } } >
141
- < ConnectionWarning
142
- isAuthenticated = { connectionStatus ( ) . authenticated }
143
- isConnected = { connectionStatus ( ) . connected }
144
- />
81
+ < ConnectionWarning isConnected = { isConnected ( ) } />
145
82
</ div >
146
83
147
84
< div ref = { setScrollRef } class = "text-lg flex-1 overflow-auto" >
@@ -151,7 +88,7 @@ export function Home(props: { softwareKeyboardShown: boolean }) {
151
88
< ul class = "flex flex-col" >
152
89
< SortableProvider ids = { ids ( ) } >
153
90
< RowAnimator >
154
- < For each = { activeList ( ) } > { ( item ) => < ItemRow item = { item } actions = { actions } /> } </ For >
91
+ < For each = { activeList ( ) } > { ( item ) => < ItemRow item = { item } /> } </ For >
155
92
</ RowAnimator >
156
93
</ SortableProvider >
157
94
</ ul >
@@ -187,7 +124,7 @@ export function Home(props: { softwareKeyboardShown: boolean }) {
187
124
</ h2 >
188
125
</ button >
189
126
190
- < button type = "button" class = "px-3 py-1" onClick = { ( ) => void client . clearCheckedItems ( ) } >
127
+ < button type = "button" class = "px-3 py-1" onClick = { ( ) => shopping . clearCheckedItems ( ) } >
191
128
Clear all
192
129
</ button >
193
130
</ div >
@@ -201,7 +138,7 @@ export function Home(props: { softwareKeyboardShown: boolean }) {
201
138
exit = { { opacity : 0 , transition : { duration : 0.2 } } }
202
139
>
203
140
< RowAnimator >
204
- < For each = { checkedList ( ) } > { ( item ) => < ItemRow item = { item } actions = { actions } /> } </ For >
141
+ < For each = { checkedList ( ) } > { ( item ) => < ItemRow item = { item } /> } </ For >
205
142
</ RowAnimator >
206
143
</ Motion . ul >
207
144
</ Show >
@@ -211,7 +148,7 @@ export function Home(props: { softwareKeyboardShown: boolean }) {
211
148
</ Presence >
212
149
</ div >
213
150
214
- < NewItem onCreate = { ( name ) => void client . addItem ( name ) } />
151
+ < NewItem onCreate = { ( name ) => shopping . addItem ( name ) } />
215
152
</ >
216
153
)
217
154
}
0 commit comments