Skip to content

Commit

Permalink
fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeparticle committed Sep 24, 2023
1 parent fc9b687 commit eff9150
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 87 deletions.
42 changes: 42 additions & 0 deletions ui/src/lib/utils/hooks/useParamsValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { useCallback, useEffect, useRef } from "react";
import { useSearchParams } from "react-router-dom";

interface Params {
[key: string]: string;
}

const useParamsValue = (initialParams: Params) => {
const count = useRef(0);

const [searchParams, setSearchParams] = useSearchParams(initialParams);

const updateParamsValue = useCallback(
(key: string, value: string) => {
console.log(key, value);
setSearchParams(
(prev) => {
prev.set(key, value);
return prev;
},
{ replace: true }
);
},
[setSearchParams]
);

useEffect(() => {
if (count.current === 0) {
for (const key in initialParams) {
if (initialParams[key]) {
const element = initialParams[key];
updateParamsValue(key, element);
}
}
count.current = 1;
}
}, [updateParamsValue, initialParams]);

return { searchParams, setSearchParams, updateParamsValue };
};

export default useParamsValue;
39 changes: 0 additions & 39 deletions ui/src/lib/utils/hooks/useUrlParams.ts

This file was deleted.

36 changes: 9 additions & 27 deletions ui/src/pages/Colors/ColorPicker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,26 @@ import ColorFormatTags from "./components/ColorFormatTags";
import Clipboard from "components/RenderProps/Clipboard";
import ClipboardButton from "components/General/ClipboardButton";
import DisplayColors from "./components/DisplayColors";
import { FormatType } from "./utils/types";
import { calculateColors, determineFormat } from "./utils/helper";
import { calculateColors } from "./utils/helper";
import CopyInput from "components/Layouts/CopyInput";
import { ResponsiveInputWithLabel } from "components/General/FormComponents";
import { useSearchParams } from "react-router-dom";
import useParamsValue from "lib/utils/hooks/useParamsValue";
import { FormatType } from "./utils/types";

const ColorPicker: React.FC = () => {
const [searchParams, setSearchParams] = useSearchParams({
const { searchParams, updateParamsValue } = useParamsValue({
color: INITIAL_COLOR,
format: INITIAL_FORMAT,
});

const color = String(searchParams.get("color"));
const format = String(searchParams.get("format")) as FormatType;

console.log(color, format);

const colors = useMemo(() => calculateColors(color), [color]);
console.log(colors);

const setColor = (color: string) => {
setSearchParams(
(prev) => {
prev.set("color", color);
prev.set("format", determineFormat(color));
return prev;
},
{ replace: true }
);
};

const onInputChange = (e: ChangeEvent<HTMLInputElement>) => {
const input = e.target.value.trim();
setColor(input);
updateParamsValue("color", input);
};

return (
Expand All @@ -64,21 +50,17 @@ const ColorPicker: React.FC = () => {
<ColorFormatTags
currentFormat={format}
onSelect={(format) =>
setSearchParams(
(prev) => {
prev.set("format", format);
return prev;
},
{ replace: true }
)
updateParamsValue("format", format)
}
/>
</Form.Item>

<CP
format={format}
value={color}
onChange={setColor}
onChange={(value) =>
updateParamsValue("color", value)
}
size="xl"
/>
</Space>
Expand Down
41 changes: 20 additions & 21 deletions ui/src/pages/Colors/ShadesAndTints/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,54 @@ import { SelectOption } from "./utils/types";
import PageGrid from "components/Layouts/PageGrid";
import Colors from "./components/Colors";
import ColorInputs from "./components/ColorInputs";
import useUrlParams from "lib/utils/hooks/useUrlParams";
import useParamsValue from "lib/utils/hooks/useParamsValue";

const ShadesAndTints: React.FC = () => {
const [params, updateUrlParam, searchParams] = useUrlParams({
const { searchParams, updateParamsValue } = useParamsValue({
color: DEFAULT_COLOR,
percentage: DEFAULT_NUM_SHADES,
percentage: DEFAULT_NUM_SHADES.toString(),
});

const [color, setColor] = useState(
searchParams.get("color") || (params.color as string)
);
const color = String(searchParams.get("color"));
const percentage = String(searchParams.get("percentage"));

const [shades, setShades] = useState<string[]>([]);
const [tints, setTints] = useState<string[]>([]);
const [percentage, setPercentage] = useState(
Number(searchParams.get("percentage")) || Number(params.percentage)
);

const [option, setOption] = useState<SelectOption>(OUTPUT_FORMAT[0]);
const [isPending, startTransition] = useTransition();

const resetInputs = () => {
setColor(DEFAULT_COLOR);
setPercentage(DEFAULT_NUM_SHADES);
updateParamsValue("color", DEFAULT_COLOR);
updateParamsValue("percentage", DEFAULT_NUM_SHADES.toString());
};

useCombinedKeyPress(resetInputs, ["ControlLeft", "KeyE"]);
useCombinedKeyPress(resetInputs, ["ControlLeft", "KeyR"]);

useEffect(() => {
startTransition(() => {
const { shades, tints } = generateShadesForColor(color, percentage);
const { shades, tints } = generateShadesForColor(
color,
Number(percentage)
);
setShades(shades);
setTints(tints);
});
}, [color, percentage]);

useEffect(() => {
updateUrlParam("color", color);
updateUrlParam("percentage", String(percentage));
}, [color, percentage]);

return (
<div className={styles.st}>
<ColorInputs
color={color}
handleColorChange={(e) => setColor(e.target.value)}
handlePercentageChange={(num) => num && setPercentage(num)}
setColor={setColor}
percentage={percentage}
handleColorChange={(e) =>
updateParamsValue("color", e.target.value)
}
handlePercentageChange={(num) =>
num && updateParamsValue("percentage", num.toString())
}
setColor={(value) => updateParamsValue("color", value)}
percentage={Number(percentage)}
handleOutputFormatChange={setOption}
option={option}
shades={shades}
Expand Down

0 comments on commit eff9150

Please sign in to comment.