-
Notifications
You must be signed in to change notification settings - Fork 158
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
Feat 1065 filter persistance on asset index #1211
Merged
DonKoko
merged 16 commits into
Shelf-nu:main
from
rajeshj11:feat-1065-filter-persistance-on-asset-index
Jul 30, 2024
Merged
Changes from 5 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d5dd212
feat:add the filter persistnace logic ofr asset index.
rajeshj11 0ec9b3e
chore:code format and clean up
rajeshj11 61cb2e7
Merge remote-tracking branch 'origin/main' into feat-1065-filter-pers…
rajeshj11 e9b914f
chore:code clean up. remove unwnated files
rajeshj11 b9b479b
Merge branch 'main' into feat-1065-filter-persistance-on-asset-index
rajeshj11 8632bc9
refactor:add function annotations and used predeifned function
rajeshj11 e6c275f
Merge branch 'feat-1065-filter-persistance-on-asset-index' of github.…
rajeshj11 5721064
Merge branch 'main' into feat-1065-filter-persistance-on-asset-index
rajeshj11 3ae0cac
chore:formatted
rajeshj11 42b5093
chore:code clean up
rajeshj11 2854331
Merge branch 'feat-1065-filter-persistance-on-asset-index' of github.…
rajeshj11 eb09845
add useSearchParams abstraction and update file structure
DonKoko 17a0a79
add rule that doesnt allow imports of useSearchParams from @remix-run…
DonKoko d2018cf
chore:removed the useDestroy at component level and replace with cust…
rajeshj11 266d8eb
replaced all imports of useSearchParams & adjusted how types are inhe…
DonKoko 616bd52
Update status-filter.tsx
DonKoko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,35 +1,103 @@ | ||
import { useMemo } from "react"; | ||
import { useSearchParams } from "@remix-run/react"; | ||
import { useLoaderData, useLocation, useSearchParams } from "@remix-run/react"; | ||
import Cookies from "js-cookie"; | ||
import type { loader } from "~/routes/_layout+/assets._index"; | ||
|
||
type SetSearchParams = ( | ||
setter: (prev: URLSearchParams) => URLSearchParams | ||
) => void; | ||
|
||
export function useAssetIndexMeta() { | ||
rajeshj11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const location = useLocation(); | ||
const { filters, organizationId } = useLoaderData<typeof loader>(); | ||
const isAssetIndexPage = location.pathname === "/assets"; | ||
const cookieSearchParams = new URLSearchParams(filters); | ||
|
||
return { filters, isAssetIndexPage, cookieSearchParams, organizationId }; | ||
} | ||
|
||
export function checkValueInCookie( | ||
keys: string[], | ||
cookieSearchParams: URLSearchParams | ||
): boolean { | ||
return keys.map((key) => cookieSearchParams.has(key)).some(Boolean); | ||
} | ||
|
||
/** | ||
* Returns a Boolean indicating if any values exists for any key passed | ||
*/ | ||
export function useSearchParamHasValue(...keys: string[]) { | ||
const [searchParams] = useSearchParams(); | ||
|
||
const { isAssetIndexPage, cookieSearchParams } = useAssetIndexMeta(); | ||
const hasValue = useMemo( | ||
() => keys.map((key) => searchParams.has(key)).some(Boolean), | ||
[keys, searchParams] | ||
); | ||
|
||
return hasValue; | ||
const hasValueInCookie = | ||
isAssetIndexPage && checkValueInCookie(keys, cookieSearchParams); | ||
|
||
return hasValue || hasValueInCookie; | ||
} | ||
|
||
/** | ||
* Returns a handler which can use used to clear all the values | ||
* for specific keys passed as params | ||
*/ | ||
|
||
export function deleteKeysInSearchParams( | ||
keys: string[], | ||
setSearchParams: SetSearchParams | ||
) { | ||
keys.forEach((key) => { | ||
setSearchParams((prev) => { | ||
prev.delete(key); | ||
return prev; | ||
}); | ||
}); | ||
} | ||
|
||
export function destoryCookieValues( | ||
rajeshj11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
organizationId: string, | ||
keys: string[], | ||
cookieSearchParams: URLSearchParams | ||
) { | ||
keys.forEach((key) => { | ||
cookieSearchParams.delete(key); | ||
}); | ||
Cookies.set(`${organizationId}_assetFilter`, cookieSearchParams.toString(), { | ||
path: "/", | ||
sameSite: "lax", | ||
secure: process.env.NODE_ENV === "production", | ||
expires: 365, // 1 year | ||
}); | ||
} | ||
|
||
export function useClearValueFromParams(...keys: string[]) { | ||
const [, setSearchParams] = useSearchParams(); | ||
const { isAssetIndexPage, organizationId, cookieSearchParams } = | ||
useAssetIndexMeta(); | ||
|
||
function clearValuesFromParams() { | ||
keys.forEach((key) => { | ||
setSearchParams((prev) => { | ||
prev.delete(key); | ||
return prev; | ||
}); | ||
}); | ||
if (isAssetIndexPage) { | ||
destoryCookieValues(organizationId, keys, cookieSearchParams); | ||
deleteKeysInSearchParams(keys, setSearchParams); | ||
return; | ||
} | ||
deleteKeysInSearchParams(keys, setSearchParams); | ||
} | ||
|
||
return clearValuesFromParams; | ||
} | ||
|
||
export function useCookieDestory() { | ||
const { isAssetIndexPage, cookieSearchParams, organizationId } = | ||
useAssetIndexMeta(); | ||
|
||
function _destoryCookieValues(keys: string[]) { | ||
if (isAssetIndexPage) { | ||
destoryCookieValues(organizationId, keys, cookieSearchParams); | ||
} | ||
} | ||
return { destoryCookieValues: _destoryCookieValues }; | ||
} |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 am a bit concerned about this approach, because that requires us to never forget to add this when having functions that clear filters and so on.
A suggestion, what if we create our own abstraction of
useSearchParams()
that handles this and makes sure that every time a value is removed from the search params, we also remove it from the cookie.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 could not able to figure out a way to do it due to some scenarios.
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.
If the user has search params saved in the cookie, they wont be empty as they come with the server response and useSearchParams checks on the client so I am not sure what you mean here.