|
20 | 20 |
|
21 | 21 | import { useCallback, useEffect, useMemo, useState } from "react"; |
22 | 22 | import { FormattedMessage } from "react-intl"; |
23 | | -import { graphql, usePaginationFragment } from "react-relay/hooks"; |
| 23 | +import { |
| 24 | + graphql, |
| 25 | + usePaginationFragment, |
| 26 | + useSubscription, |
| 27 | +} from "react-relay/hooks"; |
24 | 28 | import _ from "lodash"; |
25 | 29 |
|
| 30 | +import { ConnectionHandler } from "relay-runtime"; |
| 31 | + |
26 | 32 | import type { DeviceGroupsTable_PaginationQuery } from "@/api/__generated__/DeviceGroupsTable_PaginationQuery.graphql"; |
27 | 33 | import type { |
28 | 34 | DeviceGroupsTable_DeviceGroupFragment$data, |
@@ -54,6 +60,40 @@ const DEVICE_GROUPS_TABLE_FRAGMENT = graphql` |
54 | 60 | } |
55 | 61 | `; |
56 | 62 |
|
| 63 | +const DEVICE_GROUP_CREATED_SUBSCRIPTION = graphql` |
| 64 | + subscription DeviceGroupsTable_deviceGroupEvent_created_Subscription { |
| 65 | + deviceGroupEvent { |
| 66 | + created { |
| 67 | + id |
| 68 | + name |
| 69 | + handle |
| 70 | + selector |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +`; |
| 75 | + |
| 76 | +const DEVICE_GROUP_UPDATED_SUBSCRIPTION = graphql` |
| 77 | + subscription DeviceGroupsTable_deviceGroupEvent_updated_Subscription { |
| 78 | + deviceGroupEvent { |
| 79 | + updated { |
| 80 | + id |
| 81 | + name |
| 82 | + handle |
| 83 | + selector |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +`; |
| 88 | + |
| 89 | +const DEVICE_GROUP_DESTROYED_SUBSCRIPTION = graphql` |
| 90 | + subscription DeviceGroupsTable_deviceGroupEvent_destroyed_Subscription { |
| 91 | + deviceGroupEvent { |
| 92 | + destroyed |
| 93 | + } |
| 94 | + } |
| 95 | +`; |
| 96 | + |
57 | 97 | type TableRecord = NonNullable< |
58 | 98 | NonNullable< |
59 | 99 | DeviceGroupsTable_DeviceGroupFragment$data["deviceGroups"] |
@@ -122,6 +162,119 @@ const DeviceGroupsTable = ({ |
122 | 162 | >(DEVICE_GROUPS_TABLE_FRAGMENT, deviceGroupsRef); |
123 | 163 | const [searchText, setSearchText] = useState<string | null>(null); |
124 | 164 |
|
| 165 | + const normalizedSearchText = useMemo( |
| 166 | + () => (searchText ?? "").trim(), |
| 167 | + [searchText], |
| 168 | + ); |
| 169 | + |
| 170 | + const currentFilter = useMemo(() => { |
| 171 | + if (normalizedSearchText === "") return {}; |
| 172 | + |
| 173 | + return { |
| 174 | + or: [ |
| 175 | + { name: { ilike: `%${normalizedSearchText}%` } }, |
| 176 | + { handle: { ilike: `%${normalizedSearchText}%` } }, |
| 177 | + { selector: { ilike: `%${normalizedSearchText}%` } }, |
| 178 | + ], |
| 179 | + }; |
| 180 | + }, [normalizedSearchText]); |
| 181 | + |
| 182 | + const connectionFilter = useMemo( |
| 183 | + () => (normalizedSearchText === "" ? undefined : currentFilter), |
| 184 | + [currentFilter, normalizedSearchText], |
| 185 | + ); |
| 186 | + |
| 187 | + useSubscription( |
| 188 | + useMemo( |
| 189 | + () => ({ |
| 190 | + subscription: DEVICE_GROUP_CREATED_SUBSCRIPTION, |
| 191 | + variables: {}, |
| 192 | + updater: (store) => { |
| 193 | + const groupEvent = store.getRootField("deviceGroupEvent"); |
| 194 | + const newGroup = groupEvent?.getLinkedRecord("created"); |
| 195 | + if (!newGroup) return; |
| 196 | + |
| 197 | + if (normalizedSearchText !== "") { |
| 198 | + const search = normalizedSearchText.toLowerCase(); |
| 199 | + const name = String(newGroup.getValue("name") ?? "").toLowerCase(); |
| 200 | + const handle = String( |
| 201 | + newGroup.getValue("handle") ?? "", |
| 202 | + ).toLowerCase(); |
| 203 | + const selector = String( |
| 204 | + newGroup.getValue("selector") ?? "", |
| 205 | + ).toLowerCase(); |
| 206 | + |
| 207 | + if ( |
| 208 | + !name.includes(search) && |
| 209 | + !handle.includes(search) && |
| 210 | + !selector.includes(search) |
| 211 | + ) { |
| 212 | + return; |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + const connection = ConnectionHandler.getConnection( |
| 217 | + store.getRoot(), |
| 218 | + "DeviceGroupsTable_deviceGroups", |
| 219 | + connectionFilter ? { filter: connectionFilter } : undefined, |
| 220 | + ); |
| 221 | + if (!connection) return; |
| 222 | + |
| 223 | + const newGroupId = newGroup.getDataID(); |
| 224 | + const edges = connection.getLinkedRecords("edges") ?? []; |
| 225 | + const alreadyPresent = edges.some( |
| 226 | + (edge) => edge.getLinkedRecord("node")?.getDataID() === newGroupId, |
| 227 | + ); |
| 228 | + if (alreadyPresent) return; |
| 229 | + |
| 230 | + const edge = ConnectionHandler.createEdge( |
| 231 | + store, |
| 232 | + connection, |
| 233 | + newGroup, |
| 234 | + "DeviceGroupEdge", |
| 235 | + ); |
| 236 | + |
| 237 | + ConnectionHandler.insertEdgeBefore(connection, edge); |
| 238 | + }, |
| 239 | + }), |
| 240 | + [connectionFilter, currentFilter, normalizedSearchText], |
| 241 | + ), |
| 242 | + ); |
| 243 | + |
| 244 | + useSubscription( |
| 245 | + useMemo( |
| 246 | + () => ({ |
| 247 | + subscription: DEVICE_GROUP_UPDATED_SUBSCRIPTION, |
| 248 | + variables: {}, |
| 249 | + }), |
| 250 | + [], |
| 251 | + ), |
| 252 | + ); |
| 253 | + |
| 254 | + useSubscription( |
| 255 | + useMemo( |
| 256 | + () => ({ |
| 257 | + subscription: DEVICE_GROUP_DESTROYED_SUBSCRIPTION, |
| 258 | + variables: {}, |
| 259 | + updater: (store) => { |
| 260 | + const groupEvent = store.getRootField("deviceGroupEvent"); |
| 261 | + const destroyedId = groupEvent?.getValue("destroyed"); |
| 262 | + if (!destroyedId || typeof destroyedId !== "string") return; |
| 263 | + |
| 264 | + const connection = ConnectionHandler.getConnection( |
| 265 | + store.getRoot(), |
| 266 | + "DeviceGroupsTable_deviceGroups", |
| 267 | + connectionFilter ? { filter: connectionFilter } : undefined, |
| 268 | + ); |
| 269 | + if (!connection) return; |
| 270 | + |
| 271 | + ConnectionHandler.deleteNode(connection, destroyedId); |
| 272 | + }, |
| 273 | + }), |
| 274 | + [connectionFilter], |
| 275 | + ), |
| 276 | + ); |
| 277 | + |
125 | 278 | const debounceRefetch = useMemo( |
126 | 279 | () => |
127 | 280 | _.debounce((text: string) => { |
@@ -153,9 +306,9 @@ const DeviceGroupsTable = ({ |
153 | 306 |
|
154 | 307 | useEffect(() => { |
155 | 308 | if (searchText !== null) { |
156 | | - debounceRefetch(searchText); |
| 309 | + debounceRefetch(normalizedSearchText); |
157 | 310 | } |
158 | | - }, [debounceRefetch, searchText]); |
| 311 | + }, [debounceRefetch, normalizedSearchText, searchText]); |
159 | 312 |
|
160 | 313 | const loadNextDeviceGroups = useCallback(() => { |
161 | 314 | if (hasNext && !isLoadingNext) { |
|
0 commit comments