Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

refactor: make a single component for trash/favorites list

thecodrr97074ae05819
diff --git a/apps/web/src/components/favorite/index.js b/apps/web/src/components/favorite/index.js
deleted file mode 100644
index 168e8984c..000000000
--- a/apps/web/src/components/favorite/index.js
+++ /dev/null
@@ -1,41 +0,0 @@
-import React from "react";
-import { Flex, Text } from "rebass";
-import TimeAgo from "timeago-react";
-import ListItem from "../list-item";
-
-const dropdownRefs = [];
-const menuItems = [
- { title: "Unfavorite" },
- {
- title: "Delete",
- color: "red"
- }
-];
-
-const Favourite = ({ item, index }) => {
- const trashItem = item;
- return (
- <ListItem
- title={trashItem.title}
- body={trashItem.headline}
- index={index}
- onClick={() => {}} //TODO
- info={
- <Flex justifyContent="center" alignItems="center">
- <TimeAgo datetime={trashItem.dateDeleted} />
- <Text as="span" mx={1}>
- •
- </Text>
- <Text color="primary">
- {trashItem.type[0].toUpperCase() + trashItem.type.substring(1)}
- </Text>
- </Flex>
- }
- menuData={trashItem}
- menuItems={menuItems}
- dropdownRefs={dropdownRefs}
- />
- );
-};
-
-export default Favourite;
\ No newline at end of file
diff --git a/apps/web/src/components/listview/index.js b/apps/web/src/components/listview/index.js
new file mode 100644
index 000000000..fa90cbfc2
--- /dev/null
+++ b/apps/web/src/components/listview/index.js
@@ -0,0 +1,91 @@
+import React, { useState, useEffect } from "react";
+import { Box, Flex, Text } from "rebass";
+import Button from "../button";
+import { db, ev } from "../common";
+import ListItem from "../list-item";
+import TimeAgo from "timeago-react";
+import Search from "../search";
+import * as Icon from "react-feather";
+import { Virtuoso as List } from "react-virtuoso";
+
+function ListView({
+ type,
+ getItems,
+ menu: { menuItems, dropdownRefs },
+ button: { callToAction, icon, onClick }
+}) {
+ const [items, setItems] = useState([]);
+
+ useEffect(() => {
+ function onRefreshItems() {
+ setItems(getItems());
+ }
+ onRefreshItems();
+ ev.addListener(`refresh${type}`, onRefreshItems);
+ return () => {
+ ev.removeListener(`refresh${type}`, onRefreshItems);
+ };
+ }, []);
+
+ return (
+ <Flex flexDirection="column" flex="1 1 auto">
+ {items.length > 0 ? (
+ <Flex flexDirection="column" flex="1 1 auto">
+ <Search placeholder="Search" />
+ <List
+ style={{
+ width: "100%",
+ flex: "1 1 auto",
+ height: "auto",
+ overflowX: "hidden"
+ }}
+ totalCount={items.length}
+ item={index => {
+ const item = items[index];
+ return (
+ <ListItem
+ title={item.title}
+ body={item.headline}
+ index={index}
+ onClick={() => {}} //TODO
+ info={
+ <Flex justifyContent="center" alignItems="center">
+ <TimeAgo
+ datetime={item.dateDeleted || item.dateCreated}
+ />
+ <Text as="span" mx={1}>
+ •
+ </Text>
+ <Text color="primary">
+ {item.type[0].toUpperCase() + item.type.substring(1)}
+ </Text>
+ </Flex>
+ }
+ menuData={item}
+ menuItems={menuItems}
+ dropdownRefs={dropdownRefs}
+ />
+ );
+ }}
+ />
+ {button && (
+ <Button Icon={icon} content={callToAction} onClick={onClick} />
+ )}
+ </Flex>
+ ) : (
+ <Flex
+ flex="1 1 auto"
+ alignItems="center"
+ justifyContent="center"
+ color="#9b9b9b"
+ flexDirection="column"
+ >
+ <Icon.Star size={72} strokeWidth={1.5} />
+ <Text variant="title">You have no {type}.</Text>
+ </Flex>
+ )}
+ </Flex>
+ );
+}
+
+export default ListView;
diff --git a/apps/web/src/components/trash/index.js b/apps/web/src/components/trash/index.js
deleted file mode 100644
index 905a733d3..000000000
--- a/apps/web/src/components/trash/index.js
+++ /dev/null
@@ -1,41 +0,0 @@
-import React from "react";
-import { Flex, Text } from "rebass";
-import TimeAgo from "timeago-react";
-import ListItem from "../list-item";
-
-const dropdownRefs = [];
-const menuItems = [
- { title: "Restore" },
- {
- title: "Delete",
- color: "red"
- }
-];
-
-const Trash = ({ item, index }) => {
- const trashItem = item;
- return (
- <ListItem
- title={trashItem.title}
- body={trashItem.headline}
- index={index}
- onClick={() => {}} //TODO
- info={
- <Flex justifyContent="center" alignItems="center">
- <TimeAgo datetime={trashItem.dateDeleted} />
- <Text as="span" mx={1}>
- •
- </Text>
- <Text color="primary">
- {trashItem.type[0].toUpperCase() + trashItem.type.substring(1)}
- </Text>
- </Flex>
- }
- menuData={trashItem}
- menuItems={menuItems}
- dropdownRefs={dropdownRefs}
- />
- );
-};
-
-export default Trash;
diff --git a/apps/web/src/navigation/index.js b/apps/web/src/navigation/index.js
index 5a9bad69b..f4f715d2f 100644
--- a/apps/web/src/navigation/index.js
+++ b/apps/web/src/navigation/index.js
@@ -8,7 +8,7 @@ import { ThemeProvider } from "emotion-theming";
import { Box, Flex, Heading } from "rebass";
import * as Icon from "react-feather";
import theme from "../theme";
-import Favorite from "../views/Favorite";
+import Favorites from "../views/Favorites";
export const routes = {
home: {
@@ -26,7 +26,7 @@ export const routes = {
favorites: {
key: "favorites",
title: "Favorites",
- component: Favorite,
+ component: Favorites,
icon: Icon.Star
},
trash: {
diff --git a/apps/web/src/views/Favorite.js b/apps/web/src/views/Favorite.js
deleted file mode 100644
index be4443901..000000000
--- a/apps/web/src/views/Favorite.js
+++ /dev/null
@@ -1,61 +0,0 @@
-import React, { useState, useEffect } from "react";
-import { Box, Flex, Text } from "rebass";
-import Button from "../components/button";
-import { db, ev } from "../common";
-import FavoriteItem from "../components/favorite";
-import Search from "../components/search";
-import * as Icon from "react-feather";
-import { Virtuoso as List } from "react-virtuoso";
-
-function Favorite() {
- const [favorites, setFavorites] = useState([]);
-
- useEffect(() => {
- function onRefreshFavorite() {
- setFavorites(db.getFavorites());
- }
- onRefreshFavorite();
- ev.addListener("refreshFavorite", onRefreshFavorite);
- return () => {
- ev.removeListener("refreshFavorite", onRefreshFavorite);
- };
- }, []);
-
- return (
- <Flex flexDirection="column" flex="1 1 auto">
- {favorites.length > 0 ? (
- <Flex flexDirection="column" flex="1 1 auto">
- <Search placeholder="Search" />
- <List
- style={{
- width: "100%",
- flex: "1 1 auto",
- height: "auto",
- overflowX: "hidden"
- }}
- totalCount={favorites.length}
- item={index => <FavoriteItem index={index} item={favorites[index]} />}
- />
- <Button
- Icon={Icon.Star}
- content={"Clear Favorite"}
- //onClick={}
- />
- </Flex>
- ) : (
- <Flex
- flex="1 1 auto"
- alignItems="center"
- justifyContent="center"
- color="#9b9b9b"
- flexDirection="column"
- >
- <Icon.Star size={72} strokeWidth={1.5} />
- <Text variant="title">You have no Favorites.</Text>
- </Flex>
- )}
- </Flex>
- );
-}
-
-export default Favorite;
\ No newline at end of file
diff --git a/apps/web/src/views/Favorites.js b/apps/web/src/views/Favorites.js
new file mode 100644
index 000000000..9584dcbe1
--- /dev/null
+++ b/apps/web/src/views/Favorites.js
@@ -0,0 +1,25 @@
+import React from "react";
+import { db } from "../common";
+import ListItem from "../components/listview";
+
+const dropdownRefs = [];
+const menuItems = [
+ { title: "Unfavorite" },
+ {
+ title: "Delete",
+ color: "red"
+ }
+];
+
+function Favorites() {
+ return (
+ <ListItem
+ type="Favorites"
+ getItems={db.getFavorites}
+ menu={{ menuItems, dropdownRefs }}
+ button={undefined}
+ />
+ );
+}
+
+export default Favorites;
diff --git a/apps/web/src/views/Trash.js b/apps/web/src/views/Trash.js
index 76f14c3db..e27458e44 100644
--- a/apps/web/src/views/Trash.js
+++ b/apps/web/src/views/Trash.js
@@ -1,60 +1,29 @@
-import React, { useState, useEffect } from "react";
-import { Box, Flex, Text } from "rebass";
-import Button from "../components/button";
-import { db, ev } from "../common";
-import TrashItem from "../components/trash";
-import Search from "../components/search";
+import React from "react";
+import { db } from "../common";
import * as Icon from "react-feather";
-import { Virtuoso as List } from "react-virtuoso";
+import ListItem from "../components/listview";
-function Trash() {
- const [trash, setTrash] = useState([]);
-
- useEffect(() => {
- function onRefreshTrash() {
- setTrash(db.getTrash());
- }
- onRefreshTrash();
- ev.addListener("refreshTrash", onRefreshTrash);
- return () => {
- ev.removeListener("refreshTrash", onRefreshTrash);
- };
- }, []);
+const dropdownRefs = [];
+const menuItems = [
+ { title: "Restore" },
+ {
+ title: "Delete",
+ color: "red"
+ }
+];
+function Trash() {
return (
- <Flex flexDirection="column" flex="1 1 auto">
- {trash.length > 0 ? (
- <Flex flexDirection="column" flex="1 1 auto">
- <Search placeholder="Search" />
- <List
- style={{
- width: "100%",
- flex: "1 1 auto",
- height: "auto",
- overflowX: "hidden"
- }}
- totalCount={trash.length}
- item={index => <TrashItem index={index} item={trash[index]} />}
- />
- <Button
- Icon={Icon.Trash2}
- content={"Clear Trash"}
- onClick={() => db.clearTrash()}
- />
- </Flex>
- ) : (
- <Flex
- flex="1 1 auto"
- alignItems="center"
- justifyContent="center"
- color="#9b9b9b"
- flexDirection="column"
- >
- <Icon.Trash size={72} strokeWidth={1.5} />
- <Text variant="title">You have no trash.</Text>
- </Flex>
- )}
- </Flex>
+ <ListItem
+ type="Trash"
+ getItems={db.getTrash}
+ menu={{ menuItems, dropdownRefs }}
+ button={{
+ callToAction: "Clear Trash",
+ icon: Icon.Trash2,
+ onClick: () => db.clearTrash()
+ }}
+ />
);
}