summaryrefslogtreecommitdiff
path: root/extension/react-app/src/hooks/useArrayState.ts
diff options
context:
space:
mode:
authorNate Sesti <sestinj@gmail.com>2023-05-23 23:45:12 -0400
committerNate Sesti <sestinj@gmail.com>2023-05-23 23:45:12 -0400
commitf53768612b1e2268697b5444e502032ef9f3fb3c (patch)
tree4ed49b73e6bd3c2f8fceffa9643973033f87af95 /extension/react-app/src/hooks/useArrayState.ts
downloadsncontinue-f53768612b1e2268697b5444e502032ef9f3fb3c.tar.gz
sncontinue-f53768612b1e2268697b5444e502032ef9f3fb3c.tar.bz2
sncontinue-f53768612b1e2268697b5444e502032ef9f3fb3c.zip
copying from old repo
Diffstat (limited to 'extension/react-app/src/hooks/useArrayState.ts')
-rw-r--r--extension/react-app/src/hooks/useArrayState.ts29
1 files changed, 29 insertions, 0 deletions
diff --git a/extension/react-app/src/hooks/useArrayState.ts b/extension/react-app/src/hooks/useArrayState.ts
new file mode 100644
index 00000000..d379e720
--- /dev/null
+++ b/extension/react-app/src/hooks/useArrayState.ts
@@ -0,0 +1,29 @@
+import { useState } from "react";
+
+function useArrayState<T>(initialValue: T[]) {
+ const [value, setValue] = useState(initialValue);
+
+ function add(item: any) {
+ setValue((prev) => [...prev, item]);
+ }
+
+ function remove(index: number) {
+ setValue((prev) => prev.filter((_, i) => i !== index));
+ }
+
+ function edit(editFn: (prev: T[]) => T[]) {
+ setValue((prev) => editFn(prev));
+ }
+
+ function replace(atIndex: number, withItem: T) {
+ setValue((prev) => {
+ let updated = [...prev];
+ updated[atIndex] = withItem;
+ return updated;
+ });
+ }
+
+ return { value, add, remove, edit, replace };
+}
+
+export default useArrayState;