2024-10-22 06:28:57 -07:00
|
|
|
import { useQueries, useQuery } from '@tanstack/react-query';
|
2024-10-12 15:15:58 -07:00
|
|
|
|
2024-10-17 12:08:50 -07:00
|
|
|
import { usePlHooksApiClient } from 'pl-hooks/contexts/api-client';
|
|
|
|
import { queryClient, usePlHooksQueryClient } from 'pl-hooks/contexts/query-client';
|
2024-10-12 15:15:58 -07:00
|
|
|
import { importEntities } from 'pl-hooks/importer';
|
2024-10-23 06:26:55 -07:00
|
|
|
import { type Account, normalizeAccount } from 'pl-hooks/normalizers/account';
|
2024-10-01 07:49:17 -07:00
|
|
|
|
2024-10-23 06:26:55 -07:00
|
|
|
import { normalizeStatus, type Status } from '../../normalizers/status';
|
2024-10-05 14:12:51 -07:00
|
|
|
|
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-17 12:08:50 -07:00
|
|
|
const useStatus = (statusId?: string, opts: { language?: string } = {}) => {
|
|
|
|
const queryClient = usePlHooksQueryClient();
|
|
|
|
const { client } = usePlHooksApiClient();
|
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!, {
|
2024-10-17 12:08:50 -07:00
|
|
|
language: opts.language,
|
2024-10-05 14:12:51 -07:00
|
|
|
})
|
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-17 12:08:50 -07:00
|
|
|
}, queryClient);
|
2024-10-01 07:49:17 -07:00
|
|
|
|
2024-10-02 03:17:49 -07:00
|
|
|
const status = statusQuery.data;
|
|
|
|
|
2024-10-22 06:28:57 -07:00
|
|
|
const accountsQuery = useQueries({
|
|
|
|
queries: status?.account_ids.map(accountId => ({
|
|
|
|
queryKey: ['accounts', 'entities', accountId],
|
|
|
|
queryFn: () => client.accounts.getAccount(accountId!)
|
|
|
|
.then(account => (importEntities({ accounts: [account] }, { withParents: false }), account))
|
|
|
|
.then(normalizeAccount),
|
|
|
|
})) || [],
|
|
|
|
}, queryClient);
|
2024-10-01 07:49:17 -07:00
|
|
|
|
2024-10-17 12:08:50 -07:00
|
|
|
let data: (Status & {
|
|
|
|
account: Account;
|
|
|
|
accounts: Array<Account>;
|
|
|
|
}) | null = null;
|
|
|
|
|
|
|
|
if (status) {
|
|
|
|
data = {
|
2024-10-01 07:49:17 -07:00
|
|
|
...status,
|
2024-10-22 06:28:57 -07:00
|
|
|
account: accountsQuery[0].data!,
|
|
|
|
accounts: accountsQuery.map(({ data }) => data!).filter(Boolean),
|
2024-10-07 14:53:05 -07:00
|
|
|
// quote,
|
|
|
|
// reblog,
|
|
|
|
// poll
|
2024-10-01 07:49:17 -07:00
|
|
|
};
|
2024-10-17 12:08:50 -07:00
|
|
|
}
|
2024-10-01 07:49:17 -07:00
|
|
|
|
|
|
|
return { ...statusQuery, data };
|
|
|
|
};
|
|
|
|
|
|
|
|
export { useStatus, importStatus };
|