Feature/floating notifications calling and twitter (#112)

* Add floating notifications :O

* Add phone notifications and refactor calling ui

* This is unsafe

* Fix controls null check

* Remove go to alert

* Fix phone stuck after call

* Remove lazy mount for Phone

* Fix open/close cl_main

* Use effect for notification visibility

* Remove all alerts when phone closes

* Avoid re-render useNuiEvent

* It works now :c
This commit is contained in:
J Francisco Rader authored and GitHub committed 2021-02-23 13:19:10 -05:00
1 parent 4633146d40
commit f6d7a3dbe1
42 files changed
+869 -679

No files matched your search

+100 -94
View File
@@ -21,100 +21,6 @@ import { useProfile } from '../hooks/useProfile';
import ProfilePrompt from './profile/ProfilePrompt';
import InjectDebugData from '../../../os/debug/InjectDebugData';
InjectDebugData([
{
app: 'TWITTER',
method: 'getOrCreateTwitterProfile',
data: {
profile_name: 'Angular_Dev',
bio: 'Angular FTW',
job: 'Googler @ Google',
location: '1600 Amphitheatre Parkway, CA, USA',
avatar_url: 'https://i.tasoagc.dev/i9Ig',
},
},
{
app: 'TWITTER',
method: 'createTweetBroadcast',
data: {
id: 111,
profile_id: 111,
profile_name: 'Kidz',
isMine: false,
isReported: false,
message: 'I have tweeted this because Im badass'
}
},
{
app: 'TWITTER',
method: 'createTweetBroadcast',
data: {
id: 111,
profile_id: 111,
profile_name: 'ItsChip',
isMine: false,
isReported: false,
message: 'I have tweeted this because Im even more badass'
}
},
{
app: 'TWITTER',
method: 'fetchTweets',
data: [
{
id: 59,
message: 'XSS\n<script src="test.js">Test</script>\nBro',
avatar_url: 'http://i.tasoagc.dev/jtKL',
seconds_since_tweet: 500,
isMine: false,
updateAt: '2021-02-08 14:37:46',
visible: 1,
likes: 50000,
profile_name: 'jQuery Main',
images: 'http://i.tasoagc.dev/0xdp',
},
{
id: 2,
message: 'GCPhone will never die',
avatar_url: 'http://i.tasoagc.dev/jtKL',
seconds_since_tweet: 500,
isMine: false,
updateAt: '2021-02-08 14:37:46',
visible: 1,
likes: 50000,
profile_name: 'GCPhone_v9999',
images: 'http://i.tasoagc.dev/0xdp',
},
{
id: 1,
message: 'I love angular',
avatar_url: 'https://i.tasoagc.dev/i9Ig',
seconds_since_tweet: 800,
isMine: true,
updateAt: '2021-02-08 14:37:46',
visible: 1,
likes: 50000,
profile_name: 'Angular_Dev',
images: 'http://i.tasoagc.dev/i9Ig',
},
{
id: 3,
message:
'Anyways uhm... I bought a whole bunch of shungite, rocks, ' +
'do you know what shungite is? Anybody know what shungite is. No, no ' +
'Suge Knight, I think hes locked up in prison.',
avatar_url: 'http://i.tasoagc.dev/0v64',
seconds_since_tweet: 800,
isMine: true,
updateAt: '2021-02-08 14:37:46',
visible: 1,
likes: 50000,
profile_name: 'The_Doc',
},
],
},
]);
const useStyles = makeStyles(() => ({
backgroundModal: {
background: 'black',
@@ -188,3 +94,103 @@ export const TwitterApp = () => {
</AppWrapper>
);
};
InjectDebugData([
{
app: 'TWITTER',
method: 'getOrCreateTwitterProfile',
data: {
profile_name: 'Angular_Dev',
bio: 'Angular FTW',
job: 'Googler @ Google',
location: '1600 Amphitheatre Parkway, CA, USA',
avatar_url: 'https://i.tasoagc.dev/i9Ig',
},
},
{
app: 'TWITTER',
method: 'fetchTweets',
data: [
{
id: 59,
message: 'XSS\n<script src="test.js">Test</script>\nBro',
avatar_url: 'http://i.tasoagc.dev/jtKL',
seconds_since_tweet: 500,
isMine: false,
updateAt: '2021-02-08 14:37:46',
visible: 1,
likes: 50000,
profile_name: 'jQuery Main',
images: 'http://i.tasoagc.dev/0xdp',
},
{
id: 2,
message: 'GCPhone will never die',
avatar_url: 'http://i.tasoagc.dev/jtKL',
seconds_since_tweet: 500,
isMine: false,
updateAt: '2021-02-08 14:37:46',
visible: 1,
likes: 50000,
profile_name: 'GCPhone_v9999',
images: 'http://i.tasoagc.dev/0xdp',
},
{
id: 1,
message: 'I love angular',
avatar_url: 'https://i.tasoagc.dev/i9Ig',
seconds_since_tweet: 800,
isMine: true,
updateAt: '2021-02-08 14:37:46',
visible: 1,
likes: 50000,
profile_name: 'Angular_Dev',
images: 'http://i.tasoagc.dev/i9Ig',
},
{
id: 3,
message:
'Anyways uhm... I bought a whole bunch of shungite, rocks, ' +
'do you know what shungite is? Anybody know what shungite is. No, no ' +
'Suge Knight, I think hes locked up in prison.',
avatar_url: 'http://i.tasoagc.dev/0v64',
seconds_since_tweet: 800,
isMine: true,
updateAt: '2021-02-08 14:37:46',
visible: 1,
likes: 50000,
profile_name: 'The_Doc',
},
],
},
]);
InjectDebugData(
[
{
app: 'TWITTER',
method: 'createTweetBroadcast',
data: {
id: 111,
profile_id: 111,
profile_name: 'Kidz',
isMine: false,
isReported: false,
message: 'I have tweeted this because Im badass',
},
},
{
app: 'TWITTER',
method: 'createTweetBroadcast',
data: {
id: 111,
profile_id: 111,
profile_name: 'ItsChip',
isMine: false,
isReported: false,
message: 'I have tweeted this because Im even more badass',
},
},
],
2000
);
@@ -1,47 +0,0 @@
import React from 'react';
import { shallow } from 'enzyme';
import TwitterNotification from './TwitterNotification';
jest.mock('react-i18next', () => ({
useTranslation: () => ({ t: (key) => key }),
}));
jest.mock('../../../../os/phone/hooks/usePhone', () => ({
usePhone: jest.fn().mockReturnValue({
config: {
twitter: {
showNotifications: true,
},
},
}),
}));
jest.mock('../../hooks/useTwitterNotification', () => ({
useTwitterNotification: jest.fn().mockReturnValue({
notification: {
profile_name: 'test',
message: 'test message',
},
}),
}));
describe('TwitterNotification', () => {
let wrapper;
beforeEach(() => {
wrapper = shallow(<TwitterNotification />);
});
it('renders', () => {
expect(wrapper).toBeDefined();
});
it('renders profile name', () => {
expect(wrapper.find('.makeStyles-profileName-5').text()).toEqual('@test');
});
it('renders message', () => {
expect(wrapper.find('.makeStyles-message-7').text()).toEqual(
'test message'
);
});
});
@@ -1,88 +0,0 @@
import React, { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { makeStyles, createStyles, Theme } from '@material-ui/core/styles';
import TwitterIcon from '@material-ui/icons/Twitter';
import { useTwitterNotification } from '../../hooks/useTwitterNotification';
import Notification from '../../../../ui/components/Notification';
import { usePhone } from '../../../../os/phone/hooks/usePhone';
const useStyles = makeStyles((theme: Theme) =>
createStyles({
content: {
padding: theme.spacing(2),
overflowY: 'hidden',
},
title: {
display: 'flex',
flexFlow: 'row nowrap',
justifyContent: 'space-between',
alignItems: 'center',
},
titleLeft: {
display: 'flex',
flexFlow: 'row nowrap',
justifyContent: 'flex-start',
alignItems: 'center',
},
icon: {
marginTop: '3px',
marginRight: '5px',
},
profileName: {
marginBottom: '3px',
},
justNow: {
fontSize: '18px',
fontStyle: 'italic',
paddingRight: '3px',
},
message: {
fontSize: '16px',
overflow: 'hidden',
display: '-webkit-box',
WebkitLineClamp: 2,
WebkitBoxOrient: 'vertical',
},
})
);
function TwitterNotification() {
const classes = useStyles();
const { t } = useTranslation();
const [visible, setVisible] = useState(false);
const { notification } = useTwitterNotification();
const { config } = usePhone();
useEffect(() => {
if (notification && notification.id) {
setVisible(true);
}
}, [notification]);
if (!config?.twitter.showNotifications || !notification) return null;
const profileName = `@${notification.profile_name?.trim()}`;
return (
<Notification
key={notification.id}
open={visible}
handleClose={() => setVisible(false)}
>
<div className={classes.content}>
<div className={classes.title}>
<div className={classes.titleLeft}>
<div className={classes.icon}>
<TwitterIcon />
</div>
<div className={classes.profileName}>{profileName}</div>
</div>
<div className={classes.justNow}>{t('APPS_TWITTER_JUST_NOW')}</div>
</div>
<div className={classes.message}>{notification.message}</div>
</div>
</Notification>
);
}
export default TwitterNotification;
@@ -2,15 +2,23 @@ import React, { useEffect } from 'react';
import Nui from '../../../../os/nui-events/utils/Nui';
import { useTweets } from '../../hooks/useTweets';
import { useTwitterNotifications } from '../../hooks/useTwitterNotifications';
import TweetList from './TweetList';
export function TweetListContainer() {
const { tweets } = useTweets();
const { setUnreadCount } = useTwitterNotifications();
useEffect(() => {
Nui.send('phone:fetchTweets', {});
}, []);
useEffect(() => {
if (tweets?.length) {
setUnreadCount(0);
}
}, [setUnreadCount, tweets]);
return <TweetList tweets={tweets} />;
}
+3 -3
View File
@@ -37,8 +37,8 @@ export const twitterState = {
key: 'updateProfileSuccess',
default: null,
}),
notification: atom({
key: 'notification',
default: null,
unreadTweetsCount: atom({
key: 'unreadTweetsCount',
default: 0,
}),
};
@@ -1,13 +0,0 @@
import { useRecoilValue } from 'recoil';
import { Tweet } from '../../../common/typings/twitter';
import { twitterState } from './state';
interface ITwitterNotification {
notification: Tweet | null;
}
export const useTwitterNotification = (): ITwitterNotification => {
const notification = useRecoilValue<Tweet | null>(twitterState.notification);
return { notification };
};
@@ -0,0 +1,43 @@
import { useCallback } from "react";
import { useTranslation } from "react-i18next";
import { useHistory } from "react-router-dom";
import { useRecoilState } from "recoil";
import { useApp } from "../../../os/apps/hooks/useApps";
import { useNotifications } from "../../../os/notifications/hooks/useNotifications";
import { twitterState } from "./state";
export const useTwitterNotifications = () => {
const { t } = useTranslation();
const history = useHistory();
const { addNotificationAlert } = useNotifications();
const { icon, notificationIcon } = useApp('TWITTER');
const [unreadCount, setUnreadCount] = useRecoilState(twitterState.unreadTweetsCount);
const setNotification = useCallback(
({ profile_name, message }) => {
setUnreadCount((curr) => {
const unread = curr + 1;
addNotificationAlert(
{
app: 'TWITTER',
title: t('APPS_TWITTER_NEW_BROADCAST', { profile_name }),
onClick: () => history.push('/twitter'),
content: message,
icon,
notificationIcon,
},
true,
{
title: t('APPS_TWITTER_UNREAD_MESSAGES', {
count: unread,
}),
content: null,
}
);
return unread;
});
},
[setUnreadCount, addNotificationAlert, t, icon, notificationIcon, history]
);
return { setNotification, setUnreadCount, unreadCount }
};
@@ -5,10 +5,7 @@ import { useNuiEvent } from '../../../os/nui-events/hooks/useNuiEvent';
import { IMAGE_DELIMITER } from '../utils/images';
import { APP_TWITTER } from '../utils/constants';
import { twitterState } from './state';
import { useNotifications } from '../../../os/notifications/hooks/useNotifications';
import { useApp } from '../../../os/apps/hooks/useApps';
import { useTranslation } from 'react-i18next';
import { useCallback } from 'react';
import { useTwitterNotifications } from './useTwitterNotifications';
/**
* Perform all necessary processing/transforms from the raw database
@@ -32,22 +29,7 @@ function processTweet(tweet) {
* there and have moved that logic here instead.
*/
export const useTwitterService = () => {
const { t } = useTranslation();
const { addNotification } = useNotifications();
const { icon, notificationIcon } = useApp('TWITTER');
const setNotification = useCallback(
({ profile_name, message }) =>
addNotification({
app: 'TWITTER',
title: t('APPS_TWITTER_NEW_BROADCAST', { profile_name }),
href: '/twitter',
content: message,
icon,
notificationIcon,
}),
[addNotification, icon, notificationIcon, t]
);
const { setNotification } = useTwitterNotifications();
const setProfile = useSetRecoilState(twitterState.profile);
const setUpdateProfileLoading = useSetRecoilState(