Files
edr-platform/apps/edr-freight-web/backoffice/src/hooks/useMyTradeAccess.ts
2026-08-02 22:29:58 +00:00

33 lines
994 B
TypeScript

import { useQuery } from "@tanstack/react-query";
import {
ALL_TRADE_DIRECTIONS,
userTradeAccessService,
type TradeDirection,
} from "@/services/userTradeAccess.service";
/**
* Current user's trade-direction scope. While loading (or on error) it
* reports full access — the API enforces the real scope regardless; this
* hook only trims filter dropdowns to the directions the user can see.
*/
export function useMyTradeAccess() {
const { data } = useQuery({
queryKey: ["user-trade-access", "me"],
queryFn: userTradeAccessService.me,
staleTime: 5 * 60 * 1000,
});
const directions: TradeDirection[] = data?.directions ?? [
...ALL_TRADE_DIRECTIONS,
];
return {
restricted: data?.restricted ?? false,
directions,
/** Trim `{ value }`-shaped dropdown options to the allowed directions. */
filterOptions: <T extends { value: string }>(options: T[]): T[] =>
options.filter((o) => directions.includes(o.value as TradeDirection)),
};
}