-
Notifications
You must be signed in to change notification settings - Fork 74
fix(web): properly reload available devices after reprobing #2235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,7 @@ | |
| */ | ||
|
|
||
| import { useMutation, useQuery, useQueryClient, useSuspenseQuery } from "@tanstack/react-query"; | ||
| import React from "react"; | ||
| import React, { useMemo } from "react"; | ||
| import { | ||
| fetchConfig, | ||
| setConfig, | ||
|
|
@@ -65,6 +65,12 @@ const devicesQuery = (scope: "result" | "system") => ({ | |
| staleTime: Infinity, | ||
| }); | ||
|
|
||
| const usableDevicesQuery = () => ({ | ||
| queryKey: ["storage", "usableDevices"], | ||
| queryFn: fetchUsableDevices, | ||
| staleTime: Infinity, | ||
| }); | ||
|
|
||
| const productParamsQuery = { | ||
| queryKey: ["storage", "productParams"], | ||
| queryFn: fetchProductParams, | ||
|
|
@@ -145,34 +151,25 @@ const useDevices = ( | |
| return data; | ||
| }; | ||
|
|
||
| const availableDevices = async (devices: StorageDevice[]): Promise<StorageDevice[]> => { | ||
| const findDevice = (devices: StorageDevice[], sid: number): StorageDevice | undefined => { | ||
| const device = devices.find((d) => d.sid === sid); | ||
| if (device === undefined) console.warn("Device not found:", sid); | ||
|
|
||
| return device; | ||
| }; | ||
|
|
||
| const availableDevices = await fetchUsableDevices(); | ||
|
|
||
| return availableDevices | ||
| .map((sid: number) => findDevice(devices, sid)) | ||
| .filter((d: StorageDevice | undefined) => d); | ||
| }; | ||
|
|
||
| const availableDevicesQuery = (devices: StorageDevice[]) => ({ | ||
| queryKey: ["storage", "availableDevices"], | ||
| queryFn: () => availableDevices(devices), | ||
| staleTime: Infinity, | ||
| }); | ||
|
|
||
| /** | ||
| * Hook that returns the list of available devices for installation. | ||
| */ | ||
| const useAvailableDevices = (): StorageDevice[] => { | ||
| const devices = useDevices("system", { suspense: true }); | ||
| const { data } = useSuspenseQuery(availableDevicesQuery(devices)); | ||
| return data; | ||
| const { data: usableDevices } = useSuspenseQuery(usableDevicesQuery()); | ||
|
|
||
| const availableDevices = useMemo(() => { | ||
| /** @todo Extract this function to some shared place. */ | ||
| const findDevice = (devices: StorageDevice[], sid: number): StorageDevice | undefined => { | ||
| const device = devices.find((d) => d.sid === sid); | ||
| if (device === undefined) console.warn("Device not found:", sid); | ||
|
|
||
| return device; | ||
| }; | ||
|
Comment on lines
+163
to
+168
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NP: If possible (and when possible) I'd extract that function from here. Looks like it can be defined somewhere and consumed by this (and potentially others) method instead of be (re)defined/hidden here.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am going to add a TODO in order to keep the change as minimal as possible because it goes to beta3 branch. |
||
| return usableDevices.map((sid: number) => findDevice(devices, sid)).filter((d) => d); | ||
| }, [devices, usableDevices]); | ||
|
|
||
| return availableDevices; | ||
| }; | ||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍