pl-fe: remove trends reducers
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
parent
6c61f749fa
commit
7609a7e2a7
13 changed files with 64 additions and 104 deletions
|
@ -113,11 +113,11 @@ const createAppToken = () =>
|
||||||
const app = getState().auth.app!;
|
const app = getState().auth.app!;
|
||||||
|
|
||||||
const params = {
|
const params = {
|
||||||
client_id: app.client_id!,
|
client_id: app.client_id!,
|
||||||
client_secret: app.client_secret!,
|
client_secret: app.client_secret!,
|
||||||
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
|
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
|
||||||
grant_type: 'client_credentials',
|
grant_type: 'client_credentials',
|
||||||
scope: getScopes(getState()),
|
scope: getScopes(getState()),
|
||||||
};
|
};
|
||||||
|
|
||||||
return dispatch(obtainOAuthToken(params)).then((token) =>
|
return dispatch(obtainOAuthToken(params)).then((token) =>
|
||||||
|
@ -130,13 +130,13 @@ const createUserToken = (username: string, password: string) =>
|
||||||
const app = getState().auth.app;
|
const app = getState().auth.app;
|
||||||
|
|
||||||
const params = {
|
const params = {
|
||||||
client_id: app?.client_id!,
|
client_id: app?.client_id!,
|
||||||
client_secret: app?.client_secret!,
|
client_secret: app?.client_secret!,
|
||||||
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
|
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
|
||||||
grant_type: 'password',
|
grant_type: 'password',
|
||||||
username: username,
|
username: username,
|
||||||
password: password,
|
password: password,
|
||||||
scope: getScopes(getState()),
|
scope: getScopes(getState()),
|
||||||
};
|
};
|
||||||
|
|
||||||
return dispatch(obtainOAuthToken(params))
|
return dispatch(obtainOAuthToken(params))
|
||||||
|
|
|
@ -5,6 +5,7 @@ import { getClient } from 'pl-fe/api';
|
||||||
import { isNativeEmoji } from 'pl-fe/features/emoji';
|
import { isNativeEmoji } from 'pl-fe/features/emoji';
|
||||||
import emojiSearch from 'pl-fe/features/emoji/search';
|
import emojiSearch from 'pl-fe/features/emoji/search';
|
||||||
import { Language } from 'pl-fe/features/preferences';
|
import { Language } from 'pl-fe/features/preferences';
|
||||||
|
import { queryClient } from 'pl-fe/queries/client';
|
||||||
import { selectAccount, selectOwnAccount, makeGetAccount } from 'pl-fe/selectors';
|
import { selectAccount, selectOwnAccount, makeGetAccount } from 'pl-fe/selectors';
|
||||||
import { tagHistory } from 'pl-fe/settings';
|
import { tagHistory } from 'pl-fe/settings';
|
||||||
import { useModalsStore } from 'pl-fe/stores/modals';
|
import { useModalsStore } from 'pl-fe/stores/modals';
|
||||||
|
@ -615,7 +616,7 @@ const fetchComposeSuggestionsTags = (dispatch: AppDispatch, getState: () => Root
|
||||||
const { trends } = state.auth.client.features;
|
const { trends } = state.auth.client.features;
|
||||||
|
|
||||||
if (trends) {
|
if (trends) {
|
||||||
const currentTrends = state.trends.items;
|
const currentTrends = queryClient.getQueryData<Array<Tag>>(['trends', 'tags']) || [];
|
||||||
|
|
||||||
return dispatch(updateSuggestionTags(composeId, token, currentTrends));
|
return dispatch(updateSuggestionTags(composeId, token, currentTrends));
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +0,0 @@
|
||||||
import type { Tag } from 'pl-api';
|
|
||||||
|
|
||||||
const TRENDS_FETCH_SUCCESS = 'TRENDS_FETCH_SUCCESS';
|
|
||||||
|
|
||||||
const fetchTrendsSuccess = (tags: Array<Tag>) => ({
|
|
||||||
type: TRENDS_FETCH_SUCCESS,
|
|
||||||
tags,
|
|
||||||
});
|
|
||||||
|
|
||||||
type TrendsAction = ReturnType<typeof fetchTrendsSuccess>;
|
|
||||||
|
|
||||||
export {
|
|
||||||
TRENDS_FETCH_SUCCESS,
|
|
||||||
fetchTrendsSuccess,
|
|
||||||
type TrendsAction,
|
|
||||||
};
|
|
28
packages/pl-fe/src/api/hooks/trends/use-trending-statuses.ts
Normal file
28
packages/pl-fe/src/api/hooks/trends/use-trending-statuses.ts
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
import { useQuery } from '@tanstack/react-query';
|
||||||
|
|
||||||
|
import { importEntities } from 'pl-fe/actions/importer';
|
||||||
|
import { useAppDispatch } from 'pl-fe/hooks/use-app-dispatch';
|
||||||
|
import { useClient } from 'pl-fe/hooks/use-client';
|
||||||
|
import { useFeatures } from 'pl-fe/hooks/use-features';
|
||||||
|
|
||||||
|
const useTrendingStatuses = () => {
|
||||||
|
const client = useClient();
|
||||||
|
const dispatch = useAppDispatch();
|
||||||
|
const features = useFeatures();
|
||||||
|
|
||||||
|
const fetchTrendingStatuses = async () => {
|
||||||
|
const response = await client.trends.getTrendingStatuses();
|
||||||
|
|
||||||
|
dispatch(importEntities({ statuses: response }));
|
||||||
|
|
||||||
|
return response.map(({ id }) => id);
|
||||||
|
};
|
||||||
|
|
||||||
|
return useQuery({
|
||||||
|
queryKey: ['trends', 'statuses'],
|
||||||
|
queryFn: fetchTrendingStatuses,
|
||||||
|
enabled: features.trendingStatuses,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export { useTrendingStatuses };
|
|
@ -57,11 +57,11 @@ const CreateApp: React.FC = () => {
|
||||||
const baseURL = getBaseURL(account!);
|
const baseURL = getBaseURL(account!);
|
||||||
|
|
||||||
const tokenParams = {
|
const tokenParams = {
|
||||||
client_id: app!.client_id,
|
client_id: app!.client_id,
|
||||||
client_secret: app!.client_secret,
|
client_secret: app!.client_secret,
|
||||||
redirect_uri: params.redirect_uris,
|
redirect_uri: params.redirect_uris,
|
||||||
grant_type: 'client_credentials',
|
grant_type: 'client_credentials',
|
||||||
scope: params.scopes,
|
scope: params.scopes,
|
||||||
};
|
};
|
||||||
|
|
||||||
return dispatch(obtainOAuthToken(tokenParams, baseURL))
|
return dispatch(obtainOAuthToken(tokenParams, baseURL))
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
import clsx from 'clsx';
|
import clsx from 'clsx';
|
||||||
import React, { useEffect, useState } from 'react';
|
import React, { useState } from 'react';
|
||||||
import { FormattedMessage, defineMessages, useIntl } from 'react-intl';
|
import { FormattedMessage, defineMessages, useIntl } from 'react-intl';
|
||||||
|
|
||||||
import { expandSearch, setFilter, setSearchAccount } from 'pl-fe/actions/search';
|
import { expandSearch, setFilter, setSearchAccount } from 'pl-fe/actions/search';
|
||||||
import { fetchTrendingStatuses } from 'pl-fe/actions/trending-statuses';
|
|
||||||
import { useAccount } from 'pl-fe/api/hooks/accounts/use-account';
|
import { useAccount } from 'pl-fe/api/hooks/accounts/use-account';
|
||||||
import { useTrendingLinks } from 'pl-fe/api/hooks/trends/use-trending-links';
|
import { useTrendingLinks } from 'pl-fe/api/hooks/trends/use-trending-links';
|
||||||
|
import { useTrendingStatuses } from 'pl-fe/api/hooks/trends/use-trending-statuses';
|
||||||
import Hashtag from 'pl-fe/components/hashtag';
|
import Hashtag from 'pl-fe/components/hashtag';
|
||||||
import IconButton from 'pl-fe/components/icon-button';
|
import IconButton from 'pl-fe/components/icon-button';
|
||||||
import ScrollableList from 'pl-fe/components/scrollable-list';
|
import ScrollableList from 'pl-fe/components/scrollable-list';
|
||||||
|
@ -21,6 +21,7 @@ import PlaceholderStatus from 'pl-fe/features/placeholder/components/placeholder
|
||||||
import { useAppDispatch } from 'pl-fe/hooks/use-app-dispatch';
|
import { useAppDispatch } from 'pl-fe/hooks/use-app-dispatch';
|
||||||
import { useAppSelector } from 'pl-fe/hooks/use-app-selector';
|
import { useAppSelector } from 'pl-fe/hooks/use-app-selector';
|
||||||
import { useFeatures } from 'pl-fe/hooks/use-features';
|
import { useFeatures } from 'pl-fe/hooks/use-features';
|
||||||
|
import useTrends from 'pl-fe/queries/trends';
|
||||||
|
|
||||||
import type { SearchFilter } from 'pl-fe/reducers/search';
|
import type { SearchFilter } from 'pl-fe/reducers/search';
|
||||||
|
|
||||||
|
@ -41,11 +42,11 @@ const SearchResults = () => {
|
||||||
const value = useAppSelector((state) => state.search.submittedValue);
|
const value = useAppSelector((state) => state.search.submittedValue);
|
||||||
const results = useAppSelector((state) => state.search.results);
|
const results = useAppSelector((state) => state.search.results);
|
||||||
const suggestions = useAppSelector((state) => state.suggestions.items);
|
const suggestions = useAppSelector((state) => state.suggestions.items);
|
||||||
const trendingStatuses = useAppSelector((state) => state.trending_statuses.items);
|
|
||||||
const trends = useAppSelector((state) => state.trends.items);
|
|
||||||
const submitted = useAppSelector((state) => state.search.submitted);
|
const submitted = useAppSelector((state) => state.search.submitted);
|
||||||
const selectedFilter = useAppSelector((state) => state.search.filter);
|
const selectedFilter = useAppSelector((state) => state.search.filter);
|
||||||
const filterByAccount = useAppSelector((state) => state.search.accountId || undefined);
|
const filterByAccount = useAppSelector((state) => state.search.accountId || undefined);
|
||||||
|
const { data: trendingTags } = useTrends();
|
||||||
|
const { data: trendingStatuses } = useTrendingStatuses();
|
||||||
const { trendingLinks } = useTrendingLinks();
|
const { trendingLinks } = useTrendingLinks();
|
||||||
const { account } = useAccount(filterByAccount);
|
const { account } = useAccount(filterByAccount);
|
||||||
|
|
||||||
|
@ -107,10 +108,6 @@ const SearchResults = () => {
|
||||||
if (element) element.focus();
|
if (element) element.focus();
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
dispatch(fetchTrendingStatuses());
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
let searchResults;
|
let searchResults;
|
||||||
let hasMore = false;
|
let hasMore = false;
|
||||||
let loaded;
|
let loaded;
|
||||||
|
@ -187,7 +184,7 @@ const SearchResults = () => {
|
||||||
if (results.hashtags && results.hashtags.length > 0) {
|
if (results.hashtags && results.hashtags.length > 0) {
|
||||||
searchResults = results.hashtags.map(hashtag => <Hashtag key={hashtag.name} hashtag={hashtag} />);
|
searchResults = results.hashtags.map(hashtag => <Hashtag key={hashtag.name} hashtag={hashtag} />);
|
||||||
} else if (!submitted && suggestions && suggestions.length !== 0) {
|
} else if (!submitted && suggestions && suggestions.length !== 0) {
|
||||||
searchResults = trends.map(hashtag => <Hashtag key={hashtag.name} hashtag={hashtag} />);
|
searchResults = trendingTags?.map(hashtag => <Hashtag key={hashtag.name} hashtag={hashtag} />);
|
||||||
} else if (loaded) {
|
} else if (loaded) {
|
||||||
noResultsMessage = (
|
noResultsMessage = (
|
||||||
<div className='empty-column-indicator'>
|
<div className='empty-column-indicator'>
|
||||||
|
|
|
@ -1,31 +1,22 @@
|
||||||
import { useQuery } from '@tanstack/react-query';
|
import { useQuery } from '@tanstack/react-query';
|
||||||
|
|
||||||
import { fetchTrendsSuccess } from 'pl-fe/actions/trends';
|
|
||||||
import { useAppDispatch } from 'pl-fe/hooks/use-app-dispatch';
|
|
||||||
import { useClient } from 'pl-fe/hooks/use-client';
|
import { useClient } from 'pl-fe/hooks/use-client';
|
||||||
|
import { useFeatures } from 'pl-fe/hooks/use-features';
|
||||||
import { useLoggedIn } from 'pl-fe/hooks/use-logged-in';
|
import { useLoggedIn } from 'pl-fe/hooks/use-logged-in';
|
||||||
|
|
||||||
import type { Tag } from 'pl-api';
|
import type { Tag } from 'pl-api';
|
||||||
|
|
||||||
const useTrends = () => {
|
const useTrends = () => {
|
||||||
const dispatch = useAppDispatch();
|
|
||||||
const client = useClient();
|
const client = useClient();
|
||||||
|
const features = useFeatures();
|
||||||
const { isLoggedIn } = useLoggedIn();
|
const { isLoggedIn } = useLoggedIn();
|
||||||
|
|
||||||
const getTrends = async() => {
|
|
||||||
const data = await client.trends.getTrendingTags();
|
|
||||||
|
|
||||||
dispatch(fetchTrendsSuccess(data));
|
|
||||||
|
|
||||||
return data;
|
|
||||||
};
|
|
||||||
|
|
||||||
const result = useQuery<ReadonlyArray<Tag>>({
|
const result = useQuery<ReadonlyArray<Tag>>({
|
||||||
queryKey: ['trends'],
|
queryKey: ['trends', 'tags'],
|
||||||
queryFn: getTrends,
|
queryFn: () => client.trends.getTrendingTags(),
|
||||||
placeholderData: [],
|
placeholderData: [],
|
||||||
staleTime: 600000, // 10 minutes
|
staleTime: 600000, // 10 minutes
|
||||||
enabled: isLoggedIn,
|
enabled: isLoggedIn && features.trends,
|
||||||
});
|
});
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|
|
@ -40,7 +40,6 @@ import suggestions from './suggestions';
|
||||||
import tags from './tags';
|
import tags from './tags';
|
||||||
import timelines from './timelines';
|
import timelines from './timelines';
|
||||||
import trending_statuses from './trending-statuses';
|
import trending_statuses from './trending-statuses';
|
||||||
import trends from './trends';
|
|
||||||
import user_lists from './user-lists';
|
import user_lists from './user-lists';
|
||||||
|
|
||||||
const reducers = {
|
const reducers = {
|
||||||
|
@ -81,7 +80,6 @@ const reducers = {
|
||||||
tags,
|
tags,
|
||||||
timelines,
|
timelines,
|
||||||
trending_statuses,
|
trending_statuses,
|
||||||
trends,
|
|
||||||
user_lists,
|
user_lists,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,10 +0,0 @@
|
||||||
import reducer from './trends';
|
|
||||||
|
|
||||||
describe('trends reducer', () => {
|
|
||||||
it('should return the initial state', () => {
|
|
||||||
expect(reducer(undefined, {} as any).toJS()).toEqual({
|
|
||||||
items: [],
|
|
||||||
isLoading: false,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,29 +0,0 @@
|
||||||
import { create } from 'mutative';
|
|
||||||
|
|
||||||
import { TRENDS_FETCH_SUCCESS, type TrendsAction } from '../actions/trends';
|
|
||||||
|
|
||||||
import type { Tag } from 'pl-api';
|
|
||||||
|
|
||||||
interface State {
|
|
||||||
items: Array<Tag>;
|
|
||||||
isLoading: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
const initialState: State = {
|
|
||||||
items: [],
|
|
||||||
isLoading: false,
|
|
||||||
};
|
|
||||||
|
|
||||||
const trendsReducer = (state = initialState, action: TrendsAction) => {
|
|
||||||
switch (action.type) {
|
|
||||||
case TRENDS_FETCH_SUCCESS:
|
|
||||||
return create(state, (draft) => {
|
|
||||||
draft.items = action.tags;
|
|
||||||
draft.isLoading = false;
|
|
||||||
});
|
|
||||||
default:
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export { trendsReducer as default };
|
|
|
@ -155,13 +155,13 @@ const handlePush = (event: PushEvent) => {
|
||||||
event.waitUntil(
|
event.waitUntil(
|
||||||
fetchFromApi(`/api/v1/notifications/${notification_id}`, 'get', access_token).then(notification => {
|
fetchFromApi(`/api/v1/notifications/${notification_id}`, 'get', access_token).then(notification => {
|
||||||
const options: ExtendedNotificationOptions = {
|
const options: ExtendedNotificationOptions = {
|
||||||
title: formatMessage(`notification.${notification.type}`, preferred_locale, { name: notification.account.display_name.length > 0 ? notification.account.display_name : notification.account.username }),
|
title: formatMessage(`notification.${notification.type}`, preferred_locale, { name: notification.account.display_name.length > 0 ? notification.account.display_name : notification.account.username }),
|
||||||
body: notification.status && htmlToPlainText(notification.status.content),
|
body: notification.status && htmlToPlainText(notification.status.content),
|
||||||
icon: notification.account.avatar_static,
|
icon: notification.account.avatar_static,
|
||||||
timestamp: notification.created_at ? Number(new Date(notification.created_at)) : undefined,
|
timestamp: notification.created_at ? Number(new Date(notification.created_at)) : undefined,
|
||||||
tag: notification.id,
|
tag: notification.id,
|
||||||
image: notification.status?.media_attachments[0]?.preview_url,
|
image: notification.status?.media_attachments[0]?.preview_url,
|
||||||
data: { access_token, preferred_locale, id: notification.status ? notification.status.id : notification.account.id, url: notification.status ? `/@${notification.account.acct}/posts/${notification.status.id}` : `/@${notification.account.acct}` },
|
data: { access_token, preferred_locale, id: notification.status ? notification.status.id : notification.account.id, url: notification.status ? `/@${notification.account.acct}/posts/${notification.status.id}` : `/@${notification.account.acct}` },
|
||||||
};
|
};
|
||||||
|
|
||||||
if (notification.status?.spoiler_text || notification.status?.sensitive) {
|
if (notification.status?.spoiler_text || notification.status?.sensitive) {
|
||||||
|
@ -224,7 +224,7 @@ const findBestClient = (clients: readonly WindowClient[]): WindowClient => {
|
||||||
const expandNotification = (notification: Notification) => {
|
const expandNotification = (notification: Notification) => {
|
||||||
const newNotification = cloneNotification(notification);
|
const newNotification = cloneNotification(notification);
|
||||||
|
|
||||||
newNotification.body = newNotification.data.hiddenBody;
|
newNotification.body = newNotification.data.hiddenBody;
|
||||||
newNotification.image = newNotification.data.hiddenImage;
|
newNotification.image = newNotification.data.hiddenImage;
|
||||||
newNotification.actions = [actionReblog(notification.data.preferred_locale), actionFavourite(notification.data.preferred_locale)];
|
newNotification.actions = [actionReblog(notification.data.preferred_locale), actionFavourite(notification.data.preferred_locale)];
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ filenames.forEach(filename => {
|
||||||
if (!filename.match(/\.json$/) || filename.match(/defaultMessages|whitelist/)) return;
|
if (!filename.match(/\.json$/) || filename.match(/defaultMessages|whitelist/)) return;
|
||||||
|
|
||||||
const content = fs.readFileSync(path.resolve(__dirname, `../locales/${filename}`), 'utf-8');
|
const content = fs.readFileSync(path.resolve(__dirname, `../locales/${filename}`), 'utf-8');
|
||||||
const full = JSON.parse(content) as Record<string, string>;
|
const full = JSON.parse(content) as Record<string, string>;
|
||||||
const locale = filename.split('.')[0];
|
const locale = filename.split('.')[0];
|
||||||
|
|
||||||
filtered[locale] = {
|
filtered[locale] = {
|
||||||
|
|
|
@ -8,7 +8,7 @@ const copy = (text: string, onSuccess?: () => void) => {
|
||||||
} else {
|
} else {
|
||||||
const textarea = document.createElement('textarea');
|
const textarea = document.createElement('textarea');
|
||||||
|
|
||||||
textarea.textContent = text;
|
textarea.textContent = text;
|
||||||
textarea.style.position = 'fixed';
|
textarea.style.position = 'fixed';
|
||||||
|
|
||||||
document.body.appendChild(textarea);
|
document.body.appendChild(textarea);
|
||||||
|
|
Loading…
Reference in a new issue