-
Notifications
You must be signed in to change notification settings - Fork 0
feat: adds hook to sort presets by default order #129
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
Conversation
…n a new hook to return the presets sorted by default order in project settings.
…ion to support sorting presets. Updates docs, test, and exports.
test/lib/presets.test.ts
Outdated
| function makePreset( | ||
| docId: string, | ||
| name: string, | ||
| geometry: Array<'point' | 'line'>, | ||
| ): PresetDoc { | ||
| return { docId, name, geometry } as unknown as PresetDoc | ||
| } |
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.
think it could be preferable to use @mapeo/mock-data for this e.g.
function makePreset(overrides: Partial<Preset>) {
const preset = generate('preset', { count: 1 })[0]!
return {
...preset,
...overrides,
}
}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.
OK. Done. af6e133
src/lib/presets.ts
Outdated
| const presetMap = new Map(presets.map((p) => [p.docId, p])) | ||
| const orderedPresets = orderedPresetIds | ||
| .map((id) => presetMap.get(id)) | ||
| .filter((p): p is PresetDoc => Boolean(p)) |
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.
Think this can be reduced to something like this:
| const presetMap = new Map(presets.map((p) => [p.docId, p])) | |
| const orderedPresets = orderedPresetIds | |
| .map((id) => presetMap.get(id)) | |
| .filter((p): p is PresetDoc => Boolean(p)) | |
| const orderedPresets: Array<Preset> = [] | |
| for (const presetId of orderedPresetIds) { | |
| const matches = presets.filter((p) => p.docId === presetId) | |
| orderedPresets.push(...matches) | |
| } |
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.
I took Gregor's function (pretty much as is...) af6e133
|
Hi folks, sorry I had not been keeping up with this work being done, in parallel to the improvements on categories. I think the new naming for "defaultPresets" will help with understanding and implementation: Category Selection. This defines what categories (presets) should be presented to the user when they are selecting a category for a given datatype, including the order that they should be displayed in. The category selection could be a subset of all the categories that are defined, because a project maintainer might want to keep a category defined for matching existing data, but not allow users to select it for new data. Until we change the language everywhere, we have to work with the "defaultPresets" language for now. I think adding this as a hook could help with naming and understanding: function Example() {
const presets = usePresetsSelection({ projectId, dataType: 'observation' })
return (<CategoryScreen>
{presets.map(preset => <CategoryItem title={preset.name} icon={preset.icon} />}
</CategoryScreen>)
}const dataTypeToGeom = {
observation: 'point',
track: 'line'
} as const
/**
* Returns an ordered list of presets to be shown to the user when
* selecting the category for the given data type (observation or track)
* If the project does not have a presets selection defined, then all presets
* are returned, sorted by name.
*/
function usePresetsSelection(dataType: 'observation' | 'track') {
const { data: projectSettings } = useProjectSettings({ projectId })
const { data: presets } = useManyDocs({ projectId, docType: 'preset', lang })
const defaults = projectSettings.defaultPresets[dataTypeToGeom[dataType]]
if (!defaults.length) {
return sortByName(presets)
}
const presetsSelection: Preset[] = []
for (const presetDocId of defaults) {
const preset = presets.find(preset = preset.docId = presetDocId)
if (preset) {
presetsSelection.push(preset)
} else {
// TODO: log error to Sentry
}
}
if (!presetsSelection.length) {
return sortByName(presets)
}
return presetsSelection
}This would might benefit from being memoized in the future, which is why it's good to have it as a hook so that optimization can be an implementation detail of core-react. We could start a gradual rollout of the new language here with a hook name like |
|
@achou11 and/ or @gmaclennan
For Andrew's other comments (using @mapeo/mock-data, default config test, type improvements) - should I wait until we settle on the architecture first? Happy to go either direction, just want to make sure we're all aligned on expectations. |
@gmaclennan thanks for elaborating. These I haven't fully processed everything but these 2 points are definitely clarifying things a bit for me.
Yeah i think for now let's keep the same language and then make a single push for updating the language as a separate piece of work. @cimigree apologies for the confusion, but given the reasons outlined by Gregor, let's opt for exporting a hook implementation. |
Yes to both 👍
No, no need to export that function as it becomes an internal implementation detail.
Yeah let's stick with the existing naming conventions for now. It'll be easier to a do a bigger renaming of things as a singular piece of work later. |
Think it'd still be helpful to have these in place if they're still relevant after working on the implementation. I'd imagine that they will be but can revisit when you're ready to re-review the implementation |
|
I'm in agreement with what Andrew says here. Sorry about any confusion, I know that categories and related things like "default presets" are poorly defined and I'm working towards clarifying that, but it's currently in flux as I put together documentation. |
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
|
Ok, @achou11 I had to add some packages for the testing. Hope that is ok. I think I understood what was wanted and hope we are getting close to the desired implementation. |
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.
Looks great! Some minor non-blocking comments but otherwise feel free to merge when ready
EDIT: Might be worth updating the PR title to mention that a new hook has been added.
Closes digidem/comapeo-mobile#1389
Adds utility to sort presets.
Adds test file for that.
Uses utility in a new hook to return the presets sorted by default order in project settings.
I originally did this in comapeo-mobile so I hope it translates ok! https://github.com/digidem/comapeo-mobile/pull/1420/files