using cookies to remember where we are

This commit is contained in:
ari committed 2022-06-06 11:02:06 -04:00
1 parent 359958aada
commit c934a4afcb
6 files changed
+110 -15

No files matched your search

@@ -3,6 +3,7 @@ import { useState } from "react";
import { AutomationListBox } from "./AutomationListBox";
import { createMockAuto } from "utils/mocks";
import { Page } from "components/Page";
import { CookiesProvider } from "react-cookie";
export default {
title: 'App/AutomationList/Box',
@@ -16,10 +17,12 @@ export default {
const Template: ComponentStory<typeof AutomationListBox> = args => {
const [selected, setSelected] = useState(args.selected)
return <Page>
<AutomationListBox {...args} automations={args.automations} selected={selected} onSelectAutomation={(i) => {
setSelected(i)
args.onSelectAutomation(i)
}} />
<CookiesProvider>
<AutomationListBox {...args} automations={args.automations} selected={selected} onSelectAutomation={(i) => {
setSelected(i)
args.onSelectAutomation(i)
}} />
</CookiesProvider>
</Page>
}
@@ -6,6 +6,7 @@ import InputText from "components/Inputs/InputText";
import { Button } from "components/Inputs/Button";
import { makeGrouping } from "./automationGrouper";
import { AutomationListBoxGroup } from "./AutomationListBoxGroup";
import { useCookies } from "react-cookie";
export type Props = {
@@ -16,6 +17,33 @@ export type Props = {
onAdd: () => void;
}
const useAutomationListBoxState = () => {
const [cookies, setCookies, _] = useCookies(['albSearchText', 'albSelected']);
let initialSelected = [];
try {
initialSelected = JSON.parse(cookies.albSelected)
if (!Array.isArray(initialSelected)) {
initialSelected = [initialSelected]
}
initialSelected = initialSelected.map(Number)
} catch (_) { }
const [searchText, setSearchText] = useState(cookies.albSearchText ?? "");
const [selectedTagIdx, setSelectedTagIdx] = useState<number[]>(initialSelected);
return {
searchText,
setSearchText(v: string) {
setSearchText(v)
setCookies('albSearchText', v)
},
setSelectedTagIdx(n: number[]) {
setSelectedTagIdx(n)
setCookies('albSelected', JSON.stringify(n))
},
selectedTagIdx,
}
}
export const AutomationListBox: FC<Props> = ({
automations,
selected,
@@ -25,9 +53,13 @@ export const AutomationListBox: FC<Props> = ({
}) => {
const tags = getTagList(automations);
const [searchText, setSearchText] = useState('');
const [selectedTagIdx, setSelectedTagIdx] = useState<number[]>([]);
const filteredAutomations = filterAutomations(automations, searchText, selectedTagIdx, tags);
const {
searchText,
setSearchText,
selectedTagIdx,
setSelectedTagIdx,
} = useAutomationListBoxState();
const filteredAutomations = filterAutomations(automations, searchText.toLowerCase(), selectedTagIdx, tags);
const grouping = makeGrouping(
filteredAutomations.map(([a, _]) => a),
selectedTagIdx.map(i => tags[i]),
+23 -2
View File
@@ -13,6 +13,7 @@ import useWindowSize from "utils/useWindowSize";
import { makeTagDB } from "./TagDB";
import LinearProgress from "@mui/material/LinearProgress";
import CircularProgress from "@mui/material/CircularProgress";
import { useCookies } from 'react-cookie';
interface AutomationListParams {
@@ -24,6 +25,27 @@ interface AutomationListParams {
children?: ReactNode;
}
export const useAutomationListState = () => {
const [cookies, setCookies, _] = useCookies(['alCurrent']);
let initialCurrent = 0;
try {
initialCurrent = Number(cookies.alCurrent ?? '0');
} catch (_) { }
const [hideList, setHideList] = useState(false);
const [current, setCurrent] = useState(initialCurrent);
return {
hideList,
setHideList,
current,
setCurrent(i: number) {
setCurrent(i)
setCookies('alCurrent', String(i))
}
}
}
export const AutomationList: FC<AutomationListParams> = ({
automations,
onUpdate,
@@ -33,8 +55,7 @@ export const AutomationList: FC<AutomationListParams> = ({
children,
}) => {
// state
const [hideList, setHideList] = useState(false);
const [current, setCurrent] = useState(0);
const { hideList, setHideList, current, setCurrent } = useAutomationListState();
const { isMobile } = useWindowSize();
// alias
const currentAuto = automations.length > 0 ? automations[current] : null;