refactor: clarify frontend module ownership
This commit is contained in:
@@ -8,3 +8,4 @@ export {
|
||||
formatSnapshotTime,
|
||||
instrumentLogoUrl,
|
||||
} from "./display";
|
||||
export { useUrlPeriod } from "./useUrlPeriod";
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
import { useEffect } from "react";
|
||||
import { useSearchParams } from "react-router";
|
||||
|
||||
import { isValidPeriod } from "./calendar";
|
||||
|
||||
function useUrlPeriod() {
|
||||
const [parameters, setParameters] = useSearchParams();
|
||||
const from = parameters.get("from");
|
||||
const to = parameters.get("to");
|
||||
const isValid = isValidPeriod(from, to);
|
||||
useEffect(() => {
|
||||
if (isValid || (!parameters.has("from") && !parameters.has("to"))) return;
|
||||
const normalized = new URLSearchParams(parameters);
|
||||
normalized.delete("from");
|
||||
normalized.delete("to");
|
||||
setParameters(normalized, { replace: true });
|
||||
}, [isValid, parameters, setParameters]);
|
||||
|
||||
return {
|
||||
from: isValid ? from : null,
|
||||
parameters,
|
||||
setParameters,
|
||||
to: isValid ? to : null,
|
||||
};
|
||||
}
|
||||
|
||||
export { useUrlPeriod };
|
||||
Reference in New Issue
Block a user