mirror of
https://github.com/glitch-soc/mastodon.git
synced 2025-12-13 07:49:29 +00:00
[Glitch] Add button to load new replies in web UI
Port 14a781fa24 to glitch-soc
Signed-off-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
import { createAction } from '@reduxjs/toolkit';
|
||||||
|
|
||||||
import { apiGetContext } from 'flavours/glitch/api/statuses';
|
import { apiGetContext } from 'flavours/glitch/api/statuses';
|
||||||
import { createDataLoadingThunk } from 'flavours/glitch/store/typed_functions';
|
import { createDataLoadingThunk } from 'flavours/glitch/store/typed_functions';
|
||||||
|
|
||||||
@@ -6,13 +8,18 @@ import { importFetchedStatuses } from './importer';
|
|||||||
export const fetchContext = createDataLoadingThunk(
|
export const fetchContext = createDataLoadingThunk(
|
||||||
'status/context',
|
'status/context',
|
||||||
({ statusId }: { statusId: string }) => apiGetContext(statusId),
|
({ statusId }: { statusId: string }) => apiGetContext(statusId),
|
||||||
(context, { dispatch }) => {
|
({ context, refresh }, { dispatch }) => {
|
||||||
const statuses = context.ancestors.concat(context.descendants);
|
const statuses = context.ancestors.concat(context.descendants);
|
||||||
|
|
||||||
dispatch(importFetchedStatuses(statuses));
|
dispatch(importFetchedStatuses(statuses));
|
||||||
|
|
||||||
return {
|
return {
|
||||||
context,
|
context,
|
||||||
|
refresh,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
export const completeContextRefresh = createAction<{ statusId: string }>(
|
||||||
|
'status/context/complete',
|
||||||
|
);
|
||||||
|
|||||||
@@ -15,6 +15,50 @@ export const getLinks = (response: AxiosResponse) => {
|
|||||||
return LinkHeader.parse(value);
|
return LinkHeader.parse(value);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export interface AsyncRefreshHeader {
|
||||||
|
id: string;
|
||||||
|
retry: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
const isAsyncRefreshHeader = (obj: object): obj is AsyncRefreshHeader =>
|
||||||
|
'id' in obj && 'retry' in obj;
|
||||||
|
|
||||||
|
export const getAsyncRefreshHeader = (
|
||||||
|
response: AxiosResponse,
|
||||||
|
): AsyncRefreshHeader | null => {
|
||||||
|
const value = response.headers['mastodon-async-refresh'] as
|
||||||
|
| string
|
||||||
|
| undefined;
|
||||||
|
|
||||||
|
if (!value) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const asyncRefreshHeader: Record<string, unknown> = {};
|
||||||
|
|
||||||
|
value.split(/,\s*/).forEach((pair) => {
|
||||||
|
const [key, val] = pair.split('=', 2);
|
||||||
|
|
||||||
|
let typedValue: string | number;
|
||||||
|
|
||||||
|
if (key && ['id', 'retry'].includes(key) && val) {
|
||||||
|
if (val.startsWith('"')) {
|
||||||
|
typedValue = val.slice(1, -1);
|
||||||
|
} else {
|
||||||
|
typedValue = parseInt(val);
|
||||||
|
}
|
||||||
|
|
||||||
|
asyncRefreshHeader[key] = typedValue;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (isAsyncRefreshHeader(asyncRefreshHeader)) {
|
||||||
|
return asyncRefreshHeader;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
const csrfHeader: RawAxiosRequestHeaders = {};
|
const csrfHeader: RawAxiosRequestHeaders = {};
|
||||||
|
|
||||||
const setCSRFHeader = () => {
|
const setCSRFHeader = () => {
|
||||||
@@ -62,7 +106,7 @@ export default function api(withAuthorization = true) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
type ApiUrl = `v${1 | 2}/${string}`;
|
type ApiUrl = `v${1 | '1_alpha' | 2}/${string}`;
|
||||||
type RequestParamsOrData = Record<string, unknown>;
|
type RequestParamsOrData = Record<string, unknown>;
|
||||||
|
|
||||||
export async function apiRequest<ApiResponse = unknown>(
|
export async function apiRequest<ApiResponse = unknown>(
|
||||||
|
|||||||
5
app/javascript/flavours/glitch/api/async_refreshes.ts
Normal file
5
app/javascript/flavours/glitch/api/async_refreshes.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import { apiRequestGet } from 'flavours/glitch/api';
|
||||||
|
import type { ApiAsyncRefreshJSON } from 'flavours/glitch/api_types/async_refreshes';
|
||||||
|
|
||||||
|
export const apiGetAsyncRefresh = (id: string) =>
|
||||||
|
apiRequestGet<ApiAsyncRefreshJSON>(`v1_alpha/async_refreshes/${id}`);
|
||||||
@@ -1,5 +1,14 @@
|
|||||||
import { apiRequestGet } from 'flavours/glitch/api';
|
import api, { getAsyncRefreshHeader } from 'flavours/glitch/api';
|
||||||
import type { ApiContextJSON } from 'flavours/glitch/api_types/statuses';
|
import type { ApiContextJSON } from 'flavours/glitch/api_types/statuses';
|
||||||
|
|
||||||
export const apiGetContext = (statusId: string) =>
|
export const apiGetContext = async (statusId: string) => {
|
||||||
apiRequestGet<ApiContextJSON>(`v1/statuses/${statusId}/context`);
|
const response = await api().request<ApiContextJSON>({
|
||||||
|
method: 'GET',
|
||||||
|
url: `/api/v1/statuses/${statusId}/context`,
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
context: response.data,
|
||||||
|
refresh: getAsyncRefreshHeader(response),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
export interface ApiAsyncRefreshJSON {
|
||||||
|
async_refresh: {
|
||||||
|
id: string;
|
||||||
|
status: 'running' | 'finished';
|
||||||
|
result_count: number;
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,111 @@
|
|||||||
|
import { useEffect, useState, useCallback } from 'react';
|
||||||
|
|
||||||
|
import { useIntl, defineMessages, FormattedMessage } from 'react-intl';
|
||||||
|
|
||||||
|
import classNames from 'classnames';
|
||||||
|
|
||||||
|
import {
|
||||||
|
fetchContext,
|
||||||
|
completeContextRefresh,
|
||||||
|
} from 'flavours/glitch/actions/statuses';
|
||||||
|
import type { AsyncRefreshHeader } from 'flavours/glitch/api';
|
||||||
|
import { apiGetAsyncRefresh } from 'flavours/glitch/api/async_refreshes';
|
||||||
|
import { LoadingIndicator } from 'flavours/glitch/components/loading_indicator';
|
||||||
|
import { useAppSelector, useAppDispatch } from 'flavours/glitch/store';
|
||||||
|
|
||||||
|
const messages = defineMessages({
|
||||||
|
loading: {
|
||||||
|
id: 'status.context.loading',
|
||||||
|
defaultMessage: 'Checking for more replies',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export const RefreshController: React.FC<{
|
||||||
|
statusId: string;
|
||||||
|
withBorder?: boolean;
|
||||||
|
}> = ({ statusId, withBorder }) => {
|
||||||
|
const refresh = useAppSelector(
|
||||||
|
(state) => state.contexts.refreshing[statusId],
|
||||||
|
);
|
||||||
|
const dispatch = useAppDispatch();
|
||||||
|
const intl = useIntl();
|
||||||
|
const [ready, setReady] = useState(false);
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
let timeoutId: ReturnType<typeof setTimeout>;
|
||||||
|
|
||||||
|
const scheduleRefresh = (refresh: AsyncRefreshHeader) => {
|
||||||
|
timeoutId = setTimeout(() => {
|
||||||
|
void apiGetAsyncRefresh(refresh.id).then((result) => {
|
||||||
|
if (result.async_refresh.status === 'finished') {
|
||||||
|
dispatch(completeContextRefresh({ statusId }));
|
||||||
|
|
||||||
|
if (result.async_refresh.result_count > 0) {
|
||||||
|
setReady(true);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
scheduleRefresh(refresh);
|
||||||
|
}
|
||||||
|
|
||||||
|
return '';
|
||||||
|
});
|
||||||
|
}, refresh.retry * 1000);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (refresh) {
|
||||||
|
scheduleRefresh(refresh);
|
||||||
|
}
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
};
|
||||||
|
}, [dispatch, setReady, statusId, refresh]);
|
||||||
|
|
||||||
|
const handleClick = useCallback(() => {
|
||||||
|
setLoading(true);
|
||||||
|
setReady(false);
|
||||||
|
|
||||||
|
dispatch(fetchContext({ statusId }))
|
||||||
|
.then(() => {
|
||||||
|
setLoading(false);
|
||||||
|
return '';
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
setLoading(false);
|
||||||
|
});
|
||||||
|
}, [dispatch, setReady, statusId]);
|
||||||
|
|
||||||
|
if (ready && !loading) {
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
className={classNames('load-more load-gap', {
|
||||||
|
'timeline-hint--with-descendants': withBorder,
|
||||||
|
})}
|
||||||
|
onClick={handleClick}
|
||||||
|
>
|
||||||
|
<FormattedMessage
|
||||||
|
id='status.context.load_new_replies'
|
||||||
|
defaultMessage='New replies available'
|
||||||
|
/>
|
||||||
|
</button>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!refresh && !loading) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className={classNames('load-more load-gap', {
|
||||||
|
'timeline-hint--with-descendants': withBorder,
|
||||||
|
})}
|
||||||
|
aria-busy
|
||||||
|
aria-live='polite'
|
||||||
|
aria-label={intl.formatMessage(messages.loading)}
|
||||||
|
>
|
||||||
|
<LoadingIndicator />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
@@ -62,7 +62,7 @@ import { attachFullscreenListener, detachFullscreenListener, isFullscreen } from
|
|||||||
|
|
||||||
import ActionBar from './components/action_bar';
|
import ActionBar from './components/action_bar';
|
||||||
import { DetailedStatus } from './components/detailed_status';
|
import { DetailedStatus } from './components/detailed_status';
|
||||||
|
import { RefreshController } from './components/refresh_controller';
|
||||||
|
|
||||||
const messages = defineMessages({
|
const messages = defineMessages({
|
||||||
revealAll: { id: 'status.show_more_all', defaultMessage: 'Show more for all' },
|
revealAll: { id: 'status.show_more_all', defaultMessage: 'Show more for all' },
|
||||||
@@ -572,7 +572,7 @@ class Status extends ImmutablePureComponent {
|
|||||||
|
|
||||||
render () {
|
render () {
|
||||||
let ancestors, descendants, remoteHint;
|
let ancestors, descendants, remoteHint;
|
||||||
const { isLoading, status, settings, ancestorsIds, descendantsIds, intl, domain, multiColumn, pictureInPicture } = this.props;
|
const { isLoading, status, settings, ancestorsIds, descendantsIds, refresh, intl, domain, multiColumn, pictureInPicture } = this.props;
|
||||||
const { fullscreen } = this.state;
|
const { fullscreen } = this.state;
|
||||||
|
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
@@ -604,11 +604,9 @@ class Status extends ImmutablePureComponent {
|
|||||||
|
|
||||||
if (!isLocal) {
|
if (!isLocal) {
|
||||||
remoteHint = (
|
remoteHint = (
|
||||||
<TimelineHint
|
<RefreshController
|
||||||
className={classNames(!!descendants && 'timeline-hint--with-descendants')}
|
statusId={status.get('id')}
|
||||||
url={status.get('url')}
|
withBorder={!!descendants}
|
||||||
message={<FormattedMessage id='hints.threads.replies_may_be_missing' defaultMessage='Replies from other servers may be missing.' />}
|
|
||||||
label={<FormattedMessage id='hints.threads.see_more' defaultMessage='See more replies on {domain}' values={{ domain: <strong>{status.getIn(['account', 'acct']).split('@')[1]}</strong> }} />}
|
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import type { Draft, UnknownAction } from '@reduxjs/toolkit';
|
|||||||
import type { List as ImmutableList } from 'immutable';
|
import type { List as ImmutableList } from 'immutable';
|
||||||
|
|
||||||
import { timelineDelete } from 'flavours/glitch/actions/timelines_typed';
|
import { timelineDelete } from 'flavours/glitch/actions/timelines_typed';
|
||||||
|
import type { AsyncRefreshHeader } from 'flavours/glitch/api';
|
||||||
import type { ApiRelationshipJSON } from 'flavours/glitch/api_types/relationships';
|
import type { ApiRelationshipJSON } from 'flavours/glitch/api_types/relationships';
|
||||||
import type {
|
import type {
|
||||||
ApiStatusJSON,
|
ApiStatusJSON,
|
||||||
@@ -12,7 +13,7 @@ import type {
|
|||||||
import type { Status } from 'flavours/glitch/models/status';
|
import type { Status } from 'flavours/glitch/models/status';
|
||||||
|
|
||||||
import { blockAccountSuccess, muteAccountSuccess } from '../actions/accounts';
|
import { blockAccountSuccess, muteAccountSuccess } from '../actions/accounts';
|
||||||
import { fetchContext } from '../actions/statuses';
|
import { fetchContext, completeContextRefresh } from '../actions/statuses';
|
||||||
import { TIMELINE_UPDATE } from '../actions/timelines';
|
import { TIMELINE_UPDATE } from '../actions/timelines';
|
||||||
import { compareId } from '../compare_id';
|
import { compareId } from '../compare_id';
|
||||||
|
|
||||||
@@ -25,11 +26,13 @@ interface TimelineUpdateAction extends UnknownAction {
|
|||||||
interface State {
|
interface State {
|
||||||
inReplyTos: Record<string, string>;
|
inReplyTos: Record<string, string>;
|
||||||
replies: Record<string, string[]>;
|
replies: Record<string, string[]>;
|
||||||
|
refreshing: Record<string, AsyncRefreshHeader>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const initialState: State = {
|
const initialState: State = {
|
||||||
inReplyTos: {},
|
inReplyTos: {},
|
||||||
replies: {},
|
replies: {},
|
||||||
|
refreshing: {},
|
||||||
};
|
};
|
||||||
|
|
||||||
const normalizeContext = (
|
const normalizeContext = (
|
||||||
@@ -127,6 +130,13 @@ export const contextsReducer = createReducer(initialState, (builder) => {
|
|||||||
builder
|
builder
|
||||||
.addCase(fetchContext.fulfilled, (state, action) => {
|
.addCase(fetchContext.fulfilled, (state, action) => {
|
||||||
normalizeContext(state, action.meta.arg.statusId, action.payload.context);
|
normalizeContext(state, action.meta.arg.statusId, action.payload.context);
|
||||||
|
|
||||||
|
if (action.payload.refresh) {
|
||||||
|
state.refreshing[action.meta.arg.statusId] = action.payload.refresh;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.addCase(completeContextRefresh, (state, action) => {
|
||||||
|
delete state.refreshing[action.payload.statusId];
|
||||||
})
|
})
|
||||||
.addCase(blockAccountSuccess, (state, action) => {
|
.addCase(blockAccountSuccess, (state, action) => {
|
||||||
filterContexts(
|
filterContexts(
|
||||||
|
|||||||
Reference in New Issue
Block a user