working search functionality

This commit is contained in:
erik-sn committed 2020-09-27 11:24:07 -04:00
1 parent 660f534176
commit 7955fa3017
12 files changed
+149 -51

No files matched your search

@@ -25,7 +25,7 @@ const useStyles = makeStyles((theme) => ({
display: "flex",
flexFlow: "column nowrap",
position: "absolute",
top: "35px",
top: "80px",
},
button: {
margin: 5,
+29 -26
View File
@@ -1,12 +1,12 @@
import React from "react";
import { useTranslation } from "react-i18next";
import { makeStyles } from "@material-ui/core/styles";
import ListItemText from "@material-ui/core/ListItemText";
import ListItemAvatar from "@material-ui/core/ListItemAvatar";
import MuiAvatar from "@material-ui/core/Avatar";
import Button from "@material-ui/core/Button";
import Typography from "@material-ui/core/Typography";
import FavoriteBorderIcon from "@material-ui/icons/FavoriteBorder";
import MoreIcon from "@material-ui/icons/MoreVert";
import { ListItem } from "../../../ui/components/ListItem";
import { secondsToHumanReadable } from "../utils/time";
@@ -30,8 +30,14 @@ const useStyles = makeStyles(() => ({
flexFlow: "column nowrap",
width: "100%",
},
primary: {
display: "flex",
flexFlow: "row nowrap",
width: "100%",
alignItems: "flex-end",
},
profile: {
fontSize: "24px",
fontSize: "22px",
fontWeight: "bold",
},
date: {
@@ -40,6 +46,7 @@ const useStyles = makeStyles(() => ({
},
message: {
fontSize: "20px",
color: "rgba(255, 255, 255, 0.7)",
},
buttonContainer: {
display: "flex",
@@ -47,6 +54,7 @@ const useStyles = makeStyles(() => ({
justifyContent: "space-between",
width: "100%",
marginLeft: "-20px", // easier to do this here than override the MUI styles
marginTop: "3px",
},
}));
@@ -61,32 +69,9 @@ export const Tweet = (tweet) => {
const classes = useStyles();
const { t } = useTranslation();
const Primary = () => (
<>
<span className={classes.profile}>{`@${profile_name}`}</span>
<Typography
className={classes.date}
component="span"
variant="body2"
color="textSecondary"
>
{secondsToHumanReadable(t, seconds_since_tweet)}
</Typography>
</>
);
// this is a workaround to transfer string new lines and returns that
// are stored into the database into html for the UI to render
const formattedMessage = message.replace(/\n\r?/g, "<br />");
const Message = () => (
<>
<div
className={classes.message}
dangerouslySetInnerHTML={{ __html: formattedMessage }}
/>
<ImageDisplay visible images={images} small />
</>
);
return (
<ListItem className={classes.root} divider>
@@ -96,13 +81,31 @@ export const Tweet = (tweet) => {
</MuiAvatar>
</ListItemAvatar>
<div className={classes.content}>
<ListItemText primary={<Primary />} secondary={<Message />} />
<div className={classes.primary}>
<div className={classes.profile}>{`@${profile_name}`}</div>
<Typography
className={classes.date}
component="div"
variant="body2"
color="textSecondary"
>
{secondsToHumanReadable(t, seconds_since_tweet)}
</Typography>
</div>
<div
className={classes.message}
dangerouslySetInnerHTML={{ __html: formattedMessage }}
/>
<ImageDisplay visible images={images} small />
<div className={classes.buttonContainer}>
<ReplyButton profile_name={profile_name} />
<Button>
<FavoriteBorderIcon />
</Button>
<QuoteButton message={message} />
<Button>
<MoreIcon />
</Button>
</div>
</div>
</ListItem>
@@ -2,9 +2,11 @@ import React from "react";
import { useTweets } from "../hooks/useTweets";
import TweetList from "./TweetList";
import TweetSkeletonList from "./TweetSkeletonList";
export function TweetListContainer() {
const { tweets } = useTweets();
if (1 || !tweets) return <TweetSkeletonList />;
return <TweetList tweets={tweets} />;
}
@@ -0,0 +1,12 @@
import React from "react";
import Skeleton from "@material-ui/lab/Skeleton";
export default function TweetSkeleton() {
return (
<div>
<Skeleton variant="text" />
<Skeleton variant="circle" width={40} height={40} />
<Skeleton variant="rect" width={210} height={118} />
</div>
);
}
@@ -0,0 +1,16 @@
import React from "react";
import { List } from "../../../ui/components/List";
import TweetSkeleton from "./TweetSkeleton";
export function TweetSkeletonList() {
return (
<List>
{new Array(10).map(() => (
<TweetSkeleton />
))}
</List>
);
}
export default TweetSkeletonList;
@@ -39,7 +39,7 @@ const PAGE_COMPONENT_MAPPING = {
1: <TwitterSearch />,
2: <TwitterProfile />,
};
const MINIMUM_LOAD_TIME = 750;
const MINIMUM_LOAD_TIME = 500;
const TWEETS_REFRESH_RATE = 50000000; // TODO move this to twitter config
export const TwitterApp = () => {
@@ -48,8 +48,6 @@ export const TwitterApp = () => {
const [minimumLoadPassed, setMimimumLoadPassed] = useState(false);
const [activePage, setActivePage] = useState(0);
const { profile } = useProfile();
const { tweets } = useTweets();
useEffect(() => {
Nui.send("phone:getOrCreateTwitterProfile", {});
Nui.send("phone:fetchTweets", {});
@@ -80,11 +78,9 @@ export const TwitterApp = () => {
// we add a minimum (but short) load time here so that
// there isn't a quick flash of loading and immediately
// another flash to the tweets screen.
const hasLoaded = tweets && profile && minimumLoadPassed;
const hasLoaded = profile && minimumLoadPassed;
const showTweetButton = hasLoaded && activePage === 0;
if (!hasLoaded) return <TwitterLoader />;
return (
<AppWrapper id="twitter-app">
<AddTweetModal />
@@ -1,11 +1,13 @@
import React, { useState } from "react";
import React, { useState, useEffect } from "react";
import { useTranslation } from "react-i18next";
import { makeStyles } from "@material-ui/core/styles";
import { TextField } from "@material-ui/core";
import TweetList from "./TweetList";
import Nui from "../../../os/nui-events/utils/Nui";
import SearchButton from "./SearchButton";
import { useTweets } from "../hooks/useTweets";
import { useFilteredTweets } from "../hooks/useFilteredTweets";
const useStyles = makeStyles({
root: {
@@ -22,23 +24,20 @@ const useStyles = makeStyles({
function TwitterSearch() {
const classes = useStyles();
const { t } = useTranslation();
const { tweets } = useTweets();
const [searchValue, setSearchValue] = useState("");
const [filteredTweets, setFilteredTweets] = useState(tweets);
const { tweets, setTweets } = useFilteredTweets();
useEffect(() => {
setTweets(null); // clear the filtered tweets on app load
});
const handleChange = (e) => setSearchValue(e.target.value);
const handleSubmit = () => {
if (!searchValue.trim()) return;
const cleanedSearchValue = searchValue.trim();
if (!cleanedSearchValue) return;
setFilteredTweets(
tweets.filter((tweet) => {
const v = searchValue.toLowerCase();
return (
tweet.profile_name.toLowerCase().includes(v) ||
tweet.message.toLowerCase().includes(v)
);
})
);
Nui.send("phone:fetchTweetsFiltered", cleanedSearchValue);
};
return (
@@ -55,7 +54,7 @@ function TwitterSearch() {
inputRef={(input) => input && input.focus()}
/>
</div>
<TweetList tweets={filteredTweets} />
<TweetList tweets={tweets || []} />
</div>
<SearchButton handleClick={handleSubmit} />
</>