-
Notifications
You must be signed in to change notification settings - Fork 583
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove next index ref, fix string filter is matching default (#3249)
- Loading branch information
1 parent
9ea35d8
commit d918c6a
Showing
3 changed files
with
82 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,69 @@ | ||
import { Get } from "@fiftyone/flashlight/src/state"; | ||
import { zoomAspectRatio } from "@fiftyone/looker"; | ||
import { Lookers, LookerStore, SampleData } from "@fiftyone/state"; | ||
import { Lookers, LookerStore, ModalSampleData } from "@fiftyone/state"; | ||
import { getFetchFunction } from "@fiftyone/utilities"; | ||
import { MutableRefObject, useRef } from "react"; | ||
import { useErrorHandler } from "react-error-boundary"; | ||
import { useRecoilCallback } from "recoil"; | ||
import { pageParameters } from "./recoil"; | ||
|
||
const usePage = ( | ||
modal: boolean, | ||
store: LookerStore<Lookers> | ||
): [MutableRefObject<number>, Get<number>] => { | ||
const handleError = useErrorHandler(); | ||
const next = useRef(0); | ||
return [ | ||
next, | ||
useRecoilCallback( | ||
({ snapshot }) => | ||
async (page: number) => { | ||
try { | ||
const { zoom, ...params } = await snapshot.getPromise( | ||
pageParameters(modal) | ||
); | ||
const { results, more } = await getFetchFunction()( | ||
"POST", | ||
"/samples", | ||
{ | ||
...params, | ||
page, | ||
} | ||
); | ||
|
||
const itemData: SampleData[] = results.map((result) => { | ||
const data: SampleData = { | ||
sample: result.sample, | ||
aspectRatio: result.aspect_ratio, | ||
frameRate: result.frame_rate, | ||
frameNumber: result.sample.frame_number, | ||
urls: Object.fromEntries( | ||
result.urls.map(({ field, url }) => [field, url]) | ||
), | ||
}; | ||
const PAGE_SIZE = 20; | ||
|
||
store.samples.set(result.sample._id, data); | ||
store.indices.set(next.current, result.sample._id); | ||
next.current++; | ||
const usePage = (modal: boolean, store: LookerStore<Lookers>): Get<number> => { | ||
const handleError = useErrorHandler(); | ||
return useRecoilCallback( | ||
({ snapshot }) => | ||
async (page: number) => { | ||
try { | ||
const { zoom, ...params } = await snapshot.getPromise( | ||
pageParameters(modal) | ||
); | ||
const { results, more } = await getFetchFunction()( | ||
"POST", | ||
"/samples", | ||
{ | ||
...params, | ||
page, | ||
page_length: PAGE_SIZE, | ||
} | ||
); | ||
const offset = (page - 1) * PAGE_SIZE; | ||
const itemData: ModalSampleData[] = results.map((result, i) => { | ||
const data = { | ||
sample: result.sample, | ||
aspectRatio: result.aspect_ratio, | ||
frameRate: result.frame_rate, | ||
frameNumber: result.sample.frame_number, | ||
urls: Object.fromEntries( | ||
result.urls.map(({ field, url }) => [field, url]) | ||
), | ||
}; | ||
|
||
return data; | ||
}); | ||
store.samples.set(result.sample._id, data); | ||
store.indices.set(offset + i, result.sample._id); | ||
|
||
const items = itemData.map(({ sample, aspectRatio }) => { | ||
return { | ||
id: sample._id, | ||
aspectRatio: zoom | ||
? zoomAspectRatio(sample, aspectRatio) | ||
: aspectRatio, | ||
}; | ||
}); | ||
return data; | ||
}); | ||
|
||
const items = itemData.map(({ sample, aspectRatio }) => { | ||
return { | ||
items, | ||
nextRequestKey: more ? page + 1 : null, | ||
id: sample._id, | ||
aspectRatio: zoom | ||
? zoomAspectRatio(sample, aspectRatio) | ||
: aspectRatio, | ||
}; | ||
} catch (error) { | ||
handleError(error); | ||
throw error; | ||
} | ||
}, | ||
[modal] | ||
), | ||
]; | ||
}); | ||
|
||
return { | ||
items, | ||
nextRequestKey: more ? page + 1 : null, | ||
}; | ||
} catch (error) { | ||
handleError(error); | ||
throw error; | ||
} | ||
}, | ||
[modal] | ||
); | ||
}; | ||
|
||
export default usePage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters