-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathList.jsx
118 lines (106 loc) · 3.31 KB
/
List.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { useContext, useEffect, useState, useCallback } from "react";
import { SyncContext } from "./Context/Sync";
import Input from "./Input";
import { STORE_DATA_KEY } from "./constants";
import DeleteIcon from "./icons/delete.jsx";
import "./list.scss";
const Card = ({ info, store }) => {
const { updatedListItem } = useContext(SyncContext);
const posts = Object.keys(store?.[info] ?? {}) ?? [];
if (posts.length === 0) return null;
return (
<div className="card">
<div className=" card__header">
<div>{info || "Not available"}</div>
</div>
<div className="card__body">
<ul
className="card__body-elements"
data-editor={"post-editor"}
onClick={updatedListItem}
>
{posts.map((post) => {
const { title, content } = store[info][post];
return (
<li
key={title}
data-url={info}
data-post={post}
className="d-flex align-items-center justify-content-space-between"
>
<span className="card__body-specific-title">{title} </span>
<span data-action="delete">
<DeleteIcon className="cp" />
</span>
</li>
);
})}
</ul>
</div>
</div>
);
};
const List = () => {
const [store, setStore] = useState({});
const [filteredStore, setFilteredStore] = useState({});
useEffect(() => {
try {
chrome.storage.local.get("store", async (items) => {
if (chrome.runtime.lastError) {
return chrome.runtime.lastError;
}
setStore(items?.store ?? {});
setFilteredStore(items?.store ?? {});
});
chrome.storage.onChanged.addListener(async (changes, namespace) => {
if (chrome.runtime.lastError) {
return chrome.runtime.lastError;
}
const { newValue, oldValue } = changes[STORE_DATA_KEY];
setStore(newValue ?? {});
setFilteredStore(newValue ?? {});
});
} catch (e) {}
}, []);
//Filter the data based on keyword.
//@TODO: update this logic with better search algorithm
const onChange = useCallback(
(e) => {
const inputValue = e.target.value.toLocaleLowerCase();
let finalStore = {};
for (const [key, value] of Object.entries(store)) {
if (key.toLocaleLowerCase().includes(inputValue)) {
finalStore = { ...finalStore, [key]: value };
} else {
for (const [nestedKey, nestedValue] of Object.entries(value)) {
const urlNotes = nestedValue?.content?.filter(
(data) => data?.insert
);
if (
urlNotes.includes(inputValue) ||
nestedValue?.title?.toLocaleLowerCase()?.includes(inputValue)
) {
finalStore = { ...finalStore, [key]: value };
}
}
}
setFilteredStore(finalStore);
}
},
[store]
);
const availableData = Object.keys(filteredStore);
return (
<>
<Input onChange={onChange} />
{availableData.length === 0 ? (
<h1>No Post found</h1>
) : (
availableData.map((info) => {
return <Card key={info} info={info} store={store} />;
})
)}
</>
);
};
export default List;