Skip to content
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

new-log-viewer: Add UrlContextProvider to provide URL parameters and use them in the StateContextProvider. #48

Merged
merged 79 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
b8bf425
add UrlContextProvider
Henry8192 Jul 6, 2024
de2b31c
adjust styling
Henry8192 Jul 6, 2024
14cc8ec
adjust UrlContextProvider's field location
Henry8192 Jul 9, 2024
0860c4d
apply patch from junhao
Henry8192 Aug 1, 2024
44a36a8
set searchParams as a global variable
Henry8192 Aug 2, 2024
ee6b9ea
add docs & small fixes
Henry8192 Aug 2, 2024
1c4bf53
move typings to url.ts
Henry8192 Aug 2, 2024
128f60b
change searchParams type from string pairs to actual type pairs
Henry8192 Aug 4, 2024
a3e1054
Move "Set logEventNum to 3" button to Layout component.
junhaoliao Aug 5, 2024
fcdde69
Update URL hash parameters conditionally and dispatch event.
junhaoliao Aug 5, 2024
ea69df2
Simplify state management by using logEventNum from UrlContext directly.
junhaoliao Aug 5, 2024
334ea05
Update functions to return strings instead of URLSearchParams objects.
junhaoliao Aug 6, 2024
009905f
Add 'Copy link to last log' button.
junhaoliao Aug 6, 2024
7980ca4
Fix lint issues.
junhaoliao Aug 6, 2024
f9bdc9d
Fix out-of-bounds error in LogFileManager event indexing.
junhaoliao Aug 7, 2024
e5b5d61
Support loading with initial logEventNum.
junhaoliao Aug 7, 2024
d1b4979
Support page switching via logEventNum in the URL.
junhaoliao Aug 7, 2024
f02b094
Clamp logEventNum; refactor code.
junhaoliao Aug 7, 2024
19031b4
Refactor log event number handling and clean up worker typings.
junhaoliao Aug 7, 2024
c61e766
Refactor code.
junhaoliao Aug 7, 2024
6988f29
Add missing throws in docstring.
junhaoliao Aug 7, 2024
084d1a0
Refactor type usage to employ Nullable utility type.
junhaoliao Aug 7, 2024
97cfc50
Add getAbsoluteUrl utility function to resolve relative `filePath`s.
junhaoliao Aug 7, 2024
559dd0e
Change the conditional check from string to undefined for filePath fo…
junhaoliao Aug 8, 2024
a0f65a8
Docs - Apply suggestions from code review
junhaoliao Aug 8, 2024
f6fc924
Change `clamp` into an arrow function.
junhaoliao Aug 8, 2024
aa9bbc2
Set default log event number to 1 in debug input.
junhaoliao Aug 8, 2024
55423ba
Docs - Apply suggestions from code review
junhaoliao Aug 8, 2024
6dab65c
Rename updateLogEventNum -> updateLogEventNumInUrl.
junhaoliao Aug 8, 2024
77b6995
Refactor page calculation logic; addd `getPageNumFromLogEventNum` uti…
junhaoliao Aug 8, 2024
e8e5b0b
Docs - Apply suggestions from code review
junhaoliao Aug 8, 2024
2871740
Add docs - why we need to dispatch "hashchange" event manually.
junhaoliao Aug 8, 2024
0dd4a46
Early return in `updateWindowHashParams` if no change is required.
junhaoliao Aug 8, 2024
9d2a65c
Rename `updateWindowSearchParams` -> `updateWindowUrlSearchParams`, `…
junhaoliao Aug 8, 2024
8ba9c7a
Lint.
junhaoliao Aug 8, 2024
318607e
Rename `getAllWindowSearchParams` -> `getWindowSearchParams`, `getAll…
junhaoliao Aug 8, 2024
b04f179
Use `Number.isNaN()` instead of `isNaN()` - "Because coercion inside …
junhaoliao Aug 8, 2024
f5705bc
Docs - Apply suggestions from code review
junhaoliao Aug 8, 2024
ec6d346
Move `copyUrlToClipboard` to two methods above; rename the function a…
junhaoliao Aug 8, 2024
92655f2
Docs - Apply suggestions from code review
junhaoliao Aug 8, 2024
41e0a10
Change the @throws tag to include curly braces around {Error} in LogF…
junhaoliao Aug 8, 2024
4ada12a
Rename SEARCH_PARAM_NAME and HASH_PARAM_NAME enums to SEARCH_PARAM_NA…
junhaoliao Aug 8, 2024
1536bd9
Docs & Prompts - Apply suggestions from code review
junhaoliao Aug 8, 2024
f06c3ce
Correct initial beginLogEventIdx `1` -> `0` - Apply suggestions from …
junhaoliao Aug 8, 2024
11ddbd9
Add docs for non-trivial useEffect() hooks.
junhaoliao Aug 8, 2024
683ac29
Rename `copyWindowUrlToClipboard` -> `copyPermalinkToClipboard`.
junhaoliao Aug 8, 2024
013566d
Update StateContextProvider doc for UrlContextProvider dependency.
junhaoliao Aug 8, 2024
8a08cff
Ensure `filePath` is updated last to maintain order.
junhaoliao Aug 9, 2024
c3c730e
Assume `filePath` to be the last search parameter in URL search param…
junhaoliao Aug 9, 2024
913b2bf
Assume `filePath` to be the last search parameter in URL search param…
junhaoliao Aug 9, 2024
4b95e43
Remove redundant line in `filePath` handling.
junhaoliao Aug 9, 2024
2936d4e
add config utilities
Henry8192 Aug 9, 2024
e1e3315
Revert "Remove redundant line in `filePath` handling."
Henry8192 Aug 9, 2024
bed1917
Remove config utilities
Henry8192 Aug 9, 2024
5265f53
Reapply "Remove redundant line in `filePath` handling."
Henry8192 Aug 9, 2024
8f55ee5
Revert "add config utilities"
Henry8192 Aug 9, 2024
97a2615
Docs - Apply suggestions from code review
junhaoliao Aug 9, 2024
6d94ca6
Docs - Apply suggestions from code review
junhaoliao Aug 9, 2024
a840999
Rename `getPageNumFromLogEventNum` -> `getChunkNum` - Apply suggestio…
junhaoliao Aug 9, 2024
86de109
Fix references - rename `getPageNumFromLogEventNum` -> `getChunkNum`.
junhaoliao Aug 9, 2024
d3cbae9
Rename `numPage` -> `newPageNum`.
junhaoliao Aug 9, 2024
bd25e09
Fix getChunkNum to ensure minimum of 1.
junhaoliao Aug 9, 2024
84d9509
Use concise arrow function for useMemo callback.
junhaoliao Aug 9, 2024
3431616
Refactor ambiguous URL characters check in UrlContextProvider.
junhaoliao Aug 9, 2024
dfb4d16
Rename `getWindowSearchParams` & `getWindowSearchParams` -> `getWindo…
junhaoliao Aug 9, 2024
1a52be6
Fix beginLogEventIdx calculation off by 1.
junhaoliao Aug 9, 2024
a047b1a
Simplify the dependency array of the page-switching useEffect hook to…
junhaoliao Aug 9, 2024
11f3876
Remove debug console.log.
junhaoliao Aug 9, 2024
a7cb2e8
Refine state synchronization logic.
junhaoliao Aug 9, 2024
79e896a
Remove redundant `else` after early return.
junhaoliao Aug 9, 2024
4022cf2
Replace literals with constants and update docs.
junhaoliao Aug 11, 2024
72178a5
Update docs.
junhaoliao Aug 11, 2024
97c8f35
Add documentation on using state references in React hooks.
junhaoliao Aug 11, 2024
39008d5
Update docs.
junhaoliao Aug 11, 2024
89a4bbf
Refactor docs.
kirkrodrigues Aug 12, 2024
12ad883
Unwrap line.
kirkrodrigues Aug 12, 2024
51d68f7
Refactor some comments and add a newline.
kirkrodrigues Aug 12, 2024
e50379e
Split `numPagesRef` updates from the `logEventNum` update useEffect h…
junhaoliao Aug 12, 2024
ce42600
Restore PAGE_SIZE to 10_000.
junhaoliao Aug 12, 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
5 changes: 4 additions & 1 deletion new-log-viewer/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Layout from "./components/Layout";
import StateContextProvider from "./contexts/StateContextProvider";
import UrlContextProvider from "./contexts/UrlContextProvider";


/**
Expand All @@ -11,7 +12,9 @@ const App = () => {
return (
<>
<StateContextProvider>
<Layout/>
<UrlContextProvider>
<Layout/>
</UrlContextProvider>
</StateContextProvider>
Henry8192 marked this conversation as resolved.
Show resolved Hide resolved
</>
);
Expand Down
7 changes: 7 additions & 0 deletions new-log-viewer/src/components/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
PAGE_SIZE,
StateContext,
} from "../contexts/StateContextProvider";
import {UrlContext} from "../contexts/UrlContextProvider";


/**
Expand All @@ -21,6 +22,12 @@ const Layout = () => {
logEventNum,
} = useContext(StateContext);

const {
setSearchParamSet,
setHashParamSet,
copyToClipboard,
} = useContext(UrlContext);

useEffect(() => {
Henry8192 marked this conversation as resolved.
Show resolved Hide resolved
const urlSearchParams = new URLSearchParams(window.location.search);
const filePath = urlSearchParams.get("filePath");
Expand Down
109 changes: 109 additions & 0 deletions new-log-viewer/src/contexts/UrlContextProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import {
createContext,
useEffect,
useState,
} from "react";


interface UrlContextType {
setSearchParamSet: (searchParamSet: Record<string, string | null>) => void;
setHashParamSet: (hashParamSet: Record<string, string | null>) => void;
copyToClipboard: (
searchParamSet: Record<string, string | null>,
hashParamSet: Record<string, string | null>
) => void;
Henry8192 marked this conversation as resolved.
Show resolved Hide resolved
}

const UrlContext = createContext <UrlContextType>({} as UrlContextType);


interface UrlContextProviderProps {
children: React.ReactNode
}

/**
* Provides a context for managing URL parameters and hash values,
* including utilities for setting search and hash parameters,
* and copying the current URL with these parameters to the clipboard.
junhaoliao marked this conversation as resolved.
Show resolved Hide resolved
*
* @param children.children
* @param children The child components that will have access to the context.
*/
const UrlContextProvider = ({children}: UrlContextProviderProps) => {
const [hashParam, setHashParam] = useState<string>(window.location.hash.substring(1));
useEffect(() => {
setHashParam(window.location.hash.substring(1));
}, [window.location.hash]);
Henry8192 marked this conversation as resolved.
Show resolved Hide resolved

const setSearchParamSetHelper = (searchParamSet: Record<string, string | null>) => {
Henry8192 marked this conversation as resolved.
Show resolved Hide resolved
const newSearchParam = new URLSearchParams(window.location.search.substring(1));
const {filePath} = searchParamSet;
delete searchParamSet.filePath;

for (const [key, value] of Object.entries(searchParamSet)) {
if (null === value) {
newSearchParam.delete(key);
} else {
newSearchParam.set(key, value);
}
}
if (filePath) {
newSearchParam.set("filePath", filePath);
}

return newSearchParam;
};

const setSearchParamSet = (searchParamSet: Record<string, string | null>) => {
Henry8192 marked this conversation as resolved.
Show resolved Hide resolved
const newUrl = new URL(window.location.href);
newUrl.search = setSearchParamSetHelper(searchParamSet).toString();
if (!(/%23|%26/).test(newUrl.search)) {
newUrl.search = decodeURIComponent(newUrl.search);
}
window.history.pushState({}, "", newUrl.toString());
};

const setHashParamSetHelper = (hashParamSet: Record<string, string | null>) => {
Henry8192 marked this conversation as resolved.
Show resolved Hide resolved
const newHashParam = new URLSearchParams(hashParam);
for (const [key, value] of Object.entries(hashParamSet)) {
if (null === value) {
newHashParam.delete(key);
} else {
newHashParam.set(key, value);
}
}

return newHashParam;
};

const setHashParamSet = (hashParamSet: Record<string, string | null>) => {
const newUrl = new URL(window.location.href);
newUrl.hash = setHashParamSetHelper(hashParamSet).toString();
window.history.pushState({}, "", newUrl.toString());
};

const copyToClipboard = (
searchParamSet: Record<string, string | null>,
hashParamSet: Record<string, string | null>
) => {
const newUrl = new URL(window.location.href);
newUrl.search = setSearchParamSetHelper(searchParamSet).toString();
newUrl.hash = setHashParamSetHelper(hashParamSet).toString();
navigator.clipboard.writeText(newUrl.toString())
.then(() => {
console.log("URL copied to clipboard.");
})
.catch((error: unknown) => {
console.error("Failed to copy URL to clipboard:", error);
});
};

return (
<UrlContext.Provider value={{setSearchParamSet, setHashParamSet, copyToClipboard}}>
{children}
</UrlContext.Provider>
);
};

export default UrlContextProvider;
export {UrlContext};