bigbuffet-rw/packages/pl-fe/src/hooks/use-can-interact.ts
marcin mikołajczak 2963504736 pl-fe: Rename files to kebab case
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
2024-10-26 00:06:13 +02:00

32 lines
1,009 B
TypeScript

import { useAppSelector } from './use-app-selector';
import type { InteractionPolicy, InteractionPolicyEntry } from 'pl-api';
import type { MinifiedStatus } from 'pl-fe/reducers/statuses';
const useCanInteract = (status: Pick<MinifiedStatus, 'account_id' | 'mentions' | 'interaction_policy'>, type: keyof InteractionPolicy): {
canInteract: boolean;
approvalRequired: boolean | null;
allowed?: Array<InteractionPolicyEntry>;
} => {
const interactionPolicy = status.interaction_policy;
const me = useAppSelector(state => state.me);
if (me === status.account_id || interactionPolicy[type].always.includes('me')) return {
canInteract: true,
approvalRequired: false,
};
if (interactionPolicy[type].with_approval.includes('me')) return {
canInteract: true,
approvalRequired: true,
};
return {
canInteract: false,
approvalRequired: null,
allowed: [...interactionPolicy[type].always, ...interactionPolicy[type].with_approval],
};
};
export { useCanInteract };