Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
0b186bf
chore: rename useTab to useSearchParamsKey and add test
Parkreiner Feb 19, 2024
a7a0944
chore: mark old renderHookWithAuth as deprecated (temp)
Parkreiner Feb 19, 2024
ec3ba4f
fix: update imports for useResourcesNav
Parkreiner Feb 19, 2024
6d7ef9f
Merge branch 'main' into mes/hook-test-revamps-4
Parkreiner Mar 3, 2024
afffb26
refactor: change API for useSearchParamsKey
Parkreiner Mar 3, 2024
590f02b
chore: let user pass in their own URLSearchParams value
Parkreiner Mar 3, 2024
9e00ea6
refactor: clean up comments for clarity
Parkreiner Mar 3, 2024
d7110c5
fix: update import
Parkreiner Mar 3, 2024
2cc63d7
wip: commit progress on useWorkspaceDuplication revamp
Parkreiner Mar 3, 2024
26089e3
chore: migrate duplication test to new helper
Parkreiner Mar 3, 2024
8057397
refactor: update code for clarity
Parkreiner Mar 3, 2024
f8f87a3
refactor: reorder test cases for clarity
Parkreiner Mar 3, 2024
dcc89dc
refactor: split off hook helper into separate file
Parkreiner Mar 3, 2024
1ab6f03
refactor: remove reliance on internal React Router state property
Parkreiner Mar 4, 2024
b32603f
refactor: move variables around for more clarity
Parkreiner Mar 4, 2024
b0fabde
refactor: more updates for clarity
Parkreiner Mar 4, 2024
b94decc
refactor: reorganize test cases for clarity
Parkreiner Mar 4, 2024
3e41877
refactor: clean up test cases for useWorkspaceDupe
Parkreiner Mar 8, 2024
1fcf9e2
refactor: clean up test cases for useWorkspaceDupe
Parkreiner Mar 8, 2024
13f5e53
Merge branch 'main' into mes/hook-test-revamps-4
Parkreiner Mar 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: change API for useSearchParamsKey
  • Loading branch information
Parkreiner committed Mar 3, 2024
commit afffb269cdebed820bf2f9cb8d46d7448226965d
21 changes: 11 additions & 10 deletions site/src/hooks/useSearchParamsKey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { act, waitFor } from "@testing-library/react";
describe(useSearchParamsKey.name, () => {
it("Returns out a default value of an empty string if the key does not exist in URL", async () => {
const { result } = await renderHookWithAuth(
() => useSearchParamsKey("blah"),
() => useSearchParamsKey({ key: "blah" }),
{ routingOptions: { route: `/` } },
);

Expand All @@ -23,7 +23,7 @@ describe(useSearchParamsKey.name, () => {
it("Uses the 'defaultValue' config override if provided", async () => {
const defaultValue = "dogs";
const { result } = await renderHookWithAuth(
() => useSearchParamsKey("blah", { defaultValue }),
() => useSearchParamsKey({ key: "blah", defaultValue }),
{ routingOptions: { route: `/` } },
);

Expand All @@ -34,11 +34,12 @@ describe(useSearchParamsKey.name, () => {
const key = "blah";
const value = "cats";

const { result } = await renderHookWithAuth(() => useSearchParamsKey(key), {
routingOptions: {
route: `/?${key}=${value}`,
const { result } = await renderHookWithAuth(
() => useSearchParamsKey({ key }),
{
routingOptions: { route: `/?${key}=${value}` },
},
});
);

expect(result.current.value).toEqual(value);
});
Expand All @@ -48,7 +49,7 @@ describe(useSearchParamsKey.name, () => {
const initialValue = "cats";

const { result, getLocationSnapshot } = await renderHookWithAuth(
() => useSearchParamsKey(key),
() => useSearchParamsKey({ key }),
{
routingOptions: {
route: `/?${key}=${initialValue}`,
Expand All @@ -69,7 +70,7 @@ describe(useSearchParamsKey.name, () => {
const initialValue = "cats";

const { result, getLocationSnapshot } = await renderHookWithAuth(
() => useSearchParamsKey(key),
() => useSearchParamsKey({ key }),
{
routingOptions: {
route: `/?${key}=${initialValue}`,
Expand All @@ -91,7 +92,7 @@ describe(useSearchParamsKey.name, () => {
const initialMutableValue = "mutable";

const { result, rerender, getLocationSnapshot } = await renderHookWithAuth(
({ key }) => useSearchParamsKey(key),
({ key }) => useSearchParamsKey({ key }),
{
routingOptions: {
route: `/?${readonlyKey}=${initialReadonlyValue}&${mutableKey}=${initialMutableValue}`,
Expand All @@ -105,7 +106,7 @@ describe(useSearchParamsKey.name, () => {

const swapValue = "dogs";
rerender({ key: mutableKey });
act(() => result.current.onValueChange(swapValue));
void act(() => result.current.onValueChange(swapValue));
await waitFor(() => expect(result.current.value).toEqual(swapValue));

const { search } = getLocationSnapshot();
Expand Down
53 changes: 21 additions & 32 deletions site/src/hooks/useSearchParamsKey.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useCallback } from "react";
import { useSearchParams } from "react-router-dom";
import { useEffectEvent } from "./hookPolyfills";

export type UseSearchParamKeyConfig = Readonly<{
export type UseSearchParamsKeyConfig = Readonly<{
key: string;
searchParams?: URLSearchParams;
defaultValue?: string;
replace?: boolean;
}>;
Expand All @@ -14,39 +14,28 @@ export type UseSearchParamKeyResult = Readonly<{
}>;

export const useSearchParamsKey = (
key: string,
config: UseSearchParamKeyConfig = {},
config: UseSearchParamsKeyConfig,
): UseSearchParamKeyResult => {
const { defaultValue = "", replace = true } = config;
const [searchParams, setSearchParams] = useSearchParams();
const stableSetSearchParams = useEffectEvent(setSearchParams);
// Cannot use function update form for setSearchParams, because by default, it
// will always be linked to innerSearchParams, ignoring the config's params
const [innerSearchParams, setSearchParams] = useSearchParams();

const onValueChange = useCallback(
(newValue: string) => {
stableSetSearchParams(
(currentParams) => {
currentParams.set(key, newValue);
return currentParams;
},
{ replace },
);
},
[stableSetSearchParams, key, replace],
);

const removeValue = useCallback(() => {
stableSetSearchParams(
(currentParams) => {
currentParams.delete(key);
return currentParams;
},
{ replace },
);
}, [stableSetSearchParams, key, replace]);
const {
key,
searchParams = innerSearchParams,
defaultValue = "",
replace = true,
} = config;

return {
value: searchParams.get(key) ?? defaultValue,
onValueChange,
removeValue,
onValueChange: (newValue) => {
searchParams.set(key, newValue);
setSearchParams(searchParams, { replace });
},
removeValue: () => {
searchParams.delete(key);
setSearchParams(searchParams, { replace: true });
},
};
};
2 changes: 1 addition & 1 deletion site/src/pages/WorkspacePage/Workspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export const Workspace: FC<WorkspaceProps> = ({
const transitionStats =
template !== undefined ? ActiveTransition(template, workspace) : undefined;

const sidebarOption = useSearchParamsKey("sidebar");
const sidebarOption = useSearchParamsKey({ key: "sidebar" });
const setSidebarOption = (newOption: string) => {
if (sidebarOption.value === newOption) {
sidebarOption.removeValue();
Expand Down
2 changes: 1 addition & 1 deletion site/src/pages/WorkspacePage/useResourcesNav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const resourceOptionValue = (resource: WorkspaceResource) => {
// refactoring. Consider revisiting this solution in the future for a more
// robust implementation.
export const useResourcesNav = (resources: WorkspaceResource[]) => {
const resourcesNav = useSearchParamsKey("resources");
const resourcesNav = useSearchParamsKey({ key: "resources" });
const isSelected = useCallback(
(resource: WorkspaceResource) => {
return resourceOptionValue(resource) === resourcesNav.value;
Expand Down