-
Notifications
You must be signed in to change notification settings - Fork 39
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
✨ table-controls: factor out duplicated code via new usePersistentState hook + misc cleanup + add initial documentation (DOCS.md
and JSDoc comments)
#1355
Conversation
Signed-off-by: Mike Turley <[email protected]>
Signed-off-by: Mike Turley <[email protected]>
Signed-off-by: Mike Turley <[email protected]>
Signed-off-by: Mike Turley <[email protected]>
Signed-off-by: Mike Turley <[email protected]>
3f0af01
to
b1e43db
Compare
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## main #1355 +/- ##
==========================================
- Coverage 40.72% 40.31% -0.41%
==========================================
Files 139 143 +4
Lines 4469 4524 +55
Branches 1069 1108 +39
==========================================
+ Hits 1820 1824 +4
- Misses 2554 2603 +49
- Partials 95 97 +2
Flags with carried forward coverage won't be shown. Click here to find out more.
☔ View full report in Codecov by Sentry. |
Signed-off-by: Mike Turley <[email protected]>
00cb21e
to
bd08667
Compare
Signed-off-by: Mike Turley <[email protected]>
8d1e616
to
66a5fe8
Compare
Signed-off-by: Mike Turley <[email protected]>
66a5fe8
to
af606ad
Compare
7eff52e
to
2c56571
Compare
Signed-off-by: Mike Turley <[email protected]>
3816352
to
22f783e
Compare
…ered markdown Signed-off-by: Mike Turley <[email protected]>
4373a57
to
4f34a0f
Compare
Signed-off-by: Mike Turley <[email protected]>
Signed-off-by: Mike Turley <[email protected]>
Signed-off-by: Mike Turley <[email protected]>
Signed-off-by: Mike Turley <[email protected]>
…able to use it (#1392) When using the table-control hooks for a server-paginated table, you can use either `useTableControlState` or `useTableControlUrlParams` to provide state, then you take the returned object from that and use its properties to fetch API data, then pass the object with additional API-dependent properties to `useTableControlProps`. For a client-paginated table, there is no need to split this into two steps because the API request is not dependent on the table state (there's no need to take pagination/sort/filter state and include it in the API request), so the shorthand `useLocalTableControls` hook exists which is simply `useTableControlProps(useLocalTableControlState(args))` (`useLocalTableControlState` is similar to `useTableControlState` but performs client-side logic with the `getLocal[Feature]DerivedState` helpers in between calling each feature hook). However, `useLocalTableControls` only allows the use of React state as the source of truth rather than URL params. This PR adds an equivalent `useLocalTableControlsWithUrlParams` shorthand hook that is a drop-in replacement for `useLocalTableControls` which uses URL params instead of React state. The only additional argument available when switching to this new hook is `urlParamKeyPrefix`, which is optional and is used to distinguish the params for multiple tables on the same page. Even though the archetypes table is the only table on its page, I used it to be consistent in case we do add additional tables in modals or drawers, etc. like we did for Issues / Affected apps. With the archetypes table now using URL params for its filter state, we should now easily be able to navigate to the archetypes table filtered by tags (as discussed with @ibolton336) by using an approach similar to `getAffectedAppsUrl` on the issues page ([usage here](https://github.com/konveyor/tackle2-ui/blob/main/client/src/app/pages/issues/affected-apps-link.tsx#L22C1-L28), [implementation here](https://github.com/konveyor/tackle2-ui/blob/main/client/src/app/pages/issues/helpers.ts#L100)), which uses the `trimAndStringifyUrlParams` and `serializeFilterUrlParams` helpers. This implementation might look something like: ```ts export const getArchetypesUrlFilteredByTags = (tags: string[]) => { const baseUrl = Paths.archetypes; return `${baseUrl}?${trimAndStringifyUrlParams({ newPrefixedSerializedParams: { [`${TableURLParamKeyPrefix.archetypes}:filters`]: serializeFilterUrlParams({ tags }).filters, }, })}`; }; ``` Note (see the code comment in this diff): Because of the way we implemented `useUrlParams`, there is no way to conditionally use URL params or React state within the same hook based on an argument. This is why all the separate `use[Feature]State` and `use[Feature]UrlParams` hooks exist, and it also results in a moderate amount of duplication of code in this PR because of the logic required to feed the values returned from these feature hooks into each other. In the future we should refactor `useUrlParams` into something like `useStateOrUrlParams` to eliminate this issue and the duplication it causes (I also mentioned this in the initial docs in #1355). That will come in a future PR. Signed-off-by: Mike Turley <[email protected]> Co-authored-by: Ian Bolton <[email protected]>
Signed-off-by: Mike Turley <[email protected]>
ad3e9f9
to
ae96577
Compare
…ers"" This reverts commit 4f93796. Signed-off-by: Mike Turley <[email protected]>
Signed-off-by: Mike Turley <[email protected]>
This reverts commit ba126fa. Signed-off-by: Mike Turley <[email protected]>
…rop helpers""" This reverts commit bff8e32. Signed-off-by: Mike Turley <[email protected]>
…e-item JSDocs Signed-off-by: Mike Turley <[email protected]>
Signed-off-by: Mike Turley <[email protected]>
Signed-off-by: Mike Turley <[email protected]>
3af33c3
to
cb983bf
Compare
Signed-off-by: Mike Turley <[email protected]>
…k example Signed-off-by: Mike Turley <[email protected]>
Signed-off-by: Mike Turley <[email protected]>
27a5d46
to
9e0146f
Compare
DOCS.md and JSDoc comments
)
DOCS.md and JSDoc comments
)DOCS.md
and JSDoc comments)
9e0146f
to
06546cb
Compare
DOCS.md
and JSDoc comments)DOCS.md
and JSDoc comments)
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.
Looking great @mturley. Thanks for all of the hard work on this. The docs are going to be a lifesaver.
Before this is merged, the
DOCS.md
file is easier to read by directly viewing the markdown-rendered file on my fork.This PR got larger than I anticipated... my goal was to write thorough JSDoc comments and combine the state providers with
usePersistentState
, but as I documented things I wanted to change I decided to just go ahead and change them. My sincere apologies for my self-indulgence and the size of the PR. When you pull on one thread, there's always another. and another...DOCS.md
.@deprecated
JSDoc comments to the olduseLegacyFilterState
,useLegacySortState
anduseLegacyPaginationState
hooks with explanations.main
currently, each of theuse[Feature]State
hooks is separated into two almost identical hooksuse[Feature]State
anduse[Feature]UrlParams
which calluseState
oruseUrlParams
directly. This is a lot of duplicated code, including some duplicated business logic. This PR creates theusePersistentState
hook which is a replacement forReact.useState
that can conditionally persist the state to URL params, localStorage or sessionStorage. This allows us to combine each feature's two state hooks into oneuse[Feature]State
hook which takes apersistTo
argument for configuring the state storage location.useTableControlProps
hook was not fully separated along feature concerns. Some feature-specific code that was in there is moved to ause[Feature]PropHelpers
function, and other olderget[Feature]Props
oruse[Feature]Props
functions are refactored to share this naming convention.is[Feature]Enabled
arguments passed to eitheruseTableControlState
oruseLocalTableControls
. Using the utility typeDiscriminatedArgs
and the power of TypeScript discriminated unions, any feature-specific args for the hooks are only required by TypeScript if thatis[Feature]Enabled
arg istrue
. This ensures the consumer doesn't forget anything when enabling and configuring features (for example, enabling the sort feature and forgetting to specifysortableColumns
used to cause sorting to silently fail and now raises a type error).