2024-10-01 07:49:17 -07:00
|
|
|
import { useQuery } from '@tanstack/react-query';
|
|
|
|
import { useAppSelector, useClient } from 'pl-fe/hooks';
|
|
|
|
import { selectAccount, selectAccounts } from 'pl-fe/selectors';
|
2024-10-12 15:15:58 -07:00
|
|
|
import { useIntl } from 'react-intl';
|
|
|
|
|
|
|
|
import { queryClient } from 'pl-hooks/client';
|
|
|
|
import { useAccount } from 'pl-hooks/hooks/accounts/useAccount';
|
|
|
|
import { importEntities } from 'pl-hooks/importer';
|
2024-10-01 07:49:17 -07:00
|
|
|
|
2024-10-09 03:25:13 -07:00
|
|
|
import { normalizeStatus, type Status } from '../../normalizers/normalizeStatus';
|
2024-10-05 14:12:51 -07:00
|
|
|
|
|
|
|
// import type { Group } from 'pl-fe/normalizers';
|
2024-10-01 07:49:17 -07:00
|
|
|
|
|
|
|
type Account = ReturnType<typeof selectAccount>;
|
|
|
|
|
2024-10-07 14:53:05 -07:00
|
|
|
// const toServerSideType = (columnType: string): Filter['context'][0] => {
|
|
|
|
// switch (columnType) {
|
|
|
|
// case 'home':
|
|
|
|
// case 'notifications':
|
|
|
|
// case 'public':
|
|
|
|
// case 'thread':
|
|
|
|
// return columnType;
|
|
|
|
// default:
|
|
|
|
// if (columnType.includes('list:')) {
|
|
|
|
// return 'home';
|
|
|
|
// } else {
|
|
|
|
// return 'public'; // community, account, hashtag
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
|
|
|
|
// type FilterContext = { contextType?: string };
|
|
|
|
|
|
|
|
// const getFilters = (state: RootState, query: FilterContext) =>
|
|
|
|
// state.filters.filter((filter) =>
|
|
|
|
// (!query?.contextType || filter.context.includes(toServerSideType(query.contextType)))
|
|
|
|
// && (filter.expires_at === null || Date.parse(filter.expires_at) > new Date().getTime()),
|
|
|
|
// );
|
|
|
|
|
|
|
|
// const escapeRegExp = (string: string) =>
|
|
|
|
// string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
|
|
|
|
|
|
|
|
// const regexFromFilters = (filters: ImmutableList<Filter>) => {
|
|
|
|
// if (filters.size === 0) return null;
|
|
|
|
|
|
|
|
// return new RegExp(filters.map(filter =>
|
|
|
|
// filter.keywords.map(keyword => {
|
|
|
|
// let expr = escapeRegExp(keyword.keyword);
|
|
|
|
|
|
|
|
// if (keyword.whole_word) {
|
|
|
|
// if (/^[\w]/.test(expr)) {
|
|
|
|
// expr = `\\b${expr}`;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if (/[\w]$/.test(expr)) {
|
|
|
|
// expr = `${expr}\\b`;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return expr;
|
|
|
|
// }).join('|'),
|
|
|
|
// ).join('|'), 'i');
|
|
|
|
// };
|
|
|
|
|
|
|
|
// const checkFiltered = (index: string, filters: ImmutableList<Filter>) =>
|
|
|
|
// filters.reduce((result: Array<string>, filter) =>
|
|
|
|
// result.concat(filter.keywords.reduce((result: Array<string>, keyword) => {
|
|
|
|
// let expr = escapeRegExp(keyword.keyword);
|
|
|
|
|
|
|
|
// if (keyword.whole_word) {
|
|
|
|
// if (/^[\w]/.test(expr)) {
|
|
|
|
// expr = `\\b${expr}`;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if (/[\w]$/.test(expr)) {
|
|
|
|
// expr = `${expr}\\b`;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// const regex = new RegExp(expr);
|
|
|
|
|
|
|
|
// if (regex.test(index)) return result.concat(filter.title);
|
|
|
|
// return result;
|
|
|
|
// }, [])), []);
|
|
|
|
|
2024-10-09 03:25:13 -07:00
|
|
|
const importStatus = (status: Status) => {
|
|
|
|
queryClient.setQueryData<Status>(
|
2024-10-01 07:49:17 -07:00
|
|
|
['statuses', 'entities', status.id],
|
|
|
|
status,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-10-05 14:12:51 -07:00
|
|
|
const useStatus = (statusId?: string) => {
|
2024-10-01 07:49:17 -07:00
|
|
|
const client = useClient();
|
2024-10-05 14:12:51 -07:00
|
|
|
const intl = useIntl();
|
2024-10-01 07:49:17 -07:00
|
|
|
|
|
|
|
const statusQuery = useQuery({
|
|
|
|
queryKey: ['statuses', 'entities', statusId],
|
2024-10-05 14:12:51 -07:00
|
|
|
queryFn: () => client.statuses.getStatus(statusId!, {
|
|
|
|
language: intl.locale,
|
|
|
|
})
|
2024-10-07 14:53:05 -07:00
|
|
|
.then(status => (importEntities({ statuses: [status] }, { withParents: false }), status))
|
2024-10-09 03:25:13 -07:00
|
|
|
.then(normalizeStatus),
|
2024-10-05 14:12:51 -07:00
|
|
|
enabled: !!statusId,
|
2024-10-01 07:49:17 -07:00
|
|
|
});
|
|
|
|
|
2024-10-02 03:17:49 -07:00
|
|
|
const status = statusQuery.data;
|
|
|
|
|
2024-10-12 15:15:58 -07:00
|
|
|
const { data: account } = useAccount(status?.account_id || undefined);
|
2024-10-01 07:49:17 -07:00
|
|
|
|
2024-10-05 14:12:51 -07:00
|
|
|
// : (Status & {
|
|
|
|
// account: Account;
|
|
|
|
// accounts: Array<Account>;
|
|
|
|
// reblog: Status | null;
|
|
|
|
// }) | null
|
|
|
|
const data = useAppSelector((state) => {
|
2024-10-01 07:49:17 -07:00
|
|
|
if (!status) return null;
|
|
|
|
const accounts = selectAccounts(state, status.account_ids).filter((account): account is Account => account !== undefined);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...status,
|
2024-10-07 14:53:05 -07:00
|
|
|
account: account!,
|
2024-10-01 07:49:17 -07:00
|
|
|
accounts,
|
2024-10-07 14:53:05 -07:00
|
|
|
// quote,
|
|
|
|
// reblog,
|
|
|
|
// poll
|
2024-10-01 07:49:17 -07:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return { ...statusQuery, data };
|
|
|
|
};
|
|
|
|
|
|
|
|
export { useStatus, importStatus };
|