2024-10-01 07:49:17 -07:00
|
|
|
import { useQuery } from '@tanstack/react-query';
|
2024-10-05 14:12:51 -07:00
|
|
|
import { useIntl } from 'react-intl';
|
2024-10-02 03:17:49 -07:00
|
|
|
|
|
|
|
import { useAccount, useGroup } from 'pl-fe/api/hooks';
|
2024-10-01 07:49:17 -07:00
|
|
|
import { useAppSelector, useClient } from 'pl-fe/hooks';
|
|
|
|
import { queryClient } from 'pl-fe/queries/client';
|
|
|
|
import { selectAccount, selectAccounts } from 'pl-fe/selectors';
|
|
|
|
|
|
|
|
import { type MinifiedStatus, minifyStatus } from '../../minifiers/minifyStatus';
|
2024-10-05 14:12:51 -07:00
|
|
|
import { normalizeStatus } from '../../normalizers/normalizeStatus';
|
|
|
|
|
|
|
|
// import type { Group } from 'pl-fe/normalizers';
|
2024-10-01 07:49:17 -07:00
|
|
|
|
|
|
|
type Account = ReturnType<typeof selectAccount>;
|
|
|
|
|
|
|
|
const importStatus = (status: MinifiedStatus) => {
|
|
|
|
queryClient.setQueryData<MinifiedStatus>(
|
|
|
|
['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-01 07:49:17 -07:00
|
|
|
.then(normalizeStatus)
|
|
|
|
.then(minifyStatus),
|
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;
|
|
|
|
|
|
|
|
const { account } = useAccount(status?.account_id || undefined);
|
|
|
|
const { group } = useGroup(status?.group_id || undefined);
|
2024-10-01 07:49:17 -07:00
|
|
|
|
2024-10-05 14:12:51 -07:00
|
|
|
// : (Status & {
|
|
|
|
// account: Account;
|
|
|
|
// accounts: Array<Account>;
|
|
|
|
// group: Group | null;
|
|
|
|
// 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,
|
|
|
|
account,
|
|
|
|
accounts,
|
2024-10-02 03:17:49 -07:00
|
|
|
group,
|
2024-10-01 07:49:17 -07:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return { ...statusQuery, data };
|
|
|
|
};
|
|
|
|
|
|
|
|
export { useStatus, importStatus };
|