Skip to content

Commit

Permalink
fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Jan 18, 2024
1 parent 1a683f3 commit 78420ab
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
8 changes: 6 additions & 2 deletions library/src/containers/Operations/Operation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
CollapseButton,
} from '../../components';
import { Href } from '../../components/Href';
import { useConfig } from '../../contexts';
import { useConfig, useSpec } from '../../contexts';
import { CommonHelpers, SchemaHelpers } from '../../helpers';
import { EXTERAL_DOCUMENTATION_TEXT } from '../../constants';
import { PayloadType } from '../../types';
Expand All @@ -25,10 +25,10 @@ interface Props {

export const Operation: React.FunctionComponent<Props> = props => {
const { type = PayloadType.SEND, operation, channelName, channel } = props;
const config = useConfig();
if (!operation || !channel) {
return null;
}
const config = useConfig();

// check typeof as fallback for older version than `2.2.0`
const servers =
Expand Down Expand Up @@ -174,12 +174,16 @@ export const OperationInfo: React.FunctionComponent<Props> = props => {
const operationSummary = operation.summary();
const externalDocs = operation.externalDocs();
const operationId = operation.id();
const specV = useSpec().version();
const version = specV.localeCompare('2.6.0', undefined, { numeric: true });
const isAsyncAPIv2 = version === 0;
const {
borderColor,
typeLabel,
} = CommonHelpers.getOperationDesignInformation({
type,
config,
isAsyncAPIv2,
});
return (
<>
Expand Down
5 changes: 4 additions & 1 deletion library/src/containers/Operations/Operations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export const Operations: React.FunctionComponent = () => {
const operationsList: React.ReactNodeArray = operations.map(operation => {
const channel = operation.channels().all()[0];
const channelAddress = channel?.address() ?? '';
const operationId = CommonHelpers.getOperationIdentifier(operation);
const operationId = CommonHelpers.getOperationIdentifier({
operation,
config,
});
const type = CommonHelpers.getOperationType(operation);
return (
<li className="mb-12" key={`${type}-${operation.id()}`} id={operationId}>
Expand Down
18 changes: 11 additions & 7 deletions library/src/containers/Sidebar/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import { CollapseButton } from '../../components';
import { useConfig, useSpec } from '../../contexts';
import { CommonHelpers } from '../../helpers';
import { PayloadType } from '../../types';
import {
PUBLISH_LABEL_DEFAULT_TEXT,
SUBSCRIBE_LABEL_DEFAULT_TEXT,
} from '../../constants';
import { TagObject, filterObjectsByTags } from '../../helpers/sidebar';

const SidebarContext = React.createContext<{
Expand Down Expand Up @@ -240,7 +236,8 @@ interface OperationItemProps {
operationHrefId: string;
}
const OperationsList: React.FunctionComponent = () => {
const sidebarConfig = useConfig().sidebar;
const config = useConfig();
const sidebarConfig = config.sidebar;
const asyncapi = useSpec();
const operations = asyncapi.operations().all();
const showOperations = sidebarConfig?.showOperations ?? 'byDefault';
Expand All @@ -249,10 +246,13 @@ const OperationsList: React.FunctionComponent = () => {
OperationItemProps
>> = operations.map(operation => {
const operationChannel = operation.channels();
const operationHrefId = CommonHelpers.getOperationIdentifier(operation);
const operationHrefId = CommonHelpers.getOperationIdentifier({
operation,
config,
});
const type = CommonHelpers.getOperationType(operation);

const specV = useSpec().version();
const specV = asyncapi.version();
const version = specV.localeCompare('2.6.0', undefined, { numeric: true });
let label: string = '';
if (version === 0) {
Expand Down Expand Up @@ -340,12 +340,16 @@ const OperationItem: React.FunctionComponent<OperationItemProps> = ({
}) => {
const config = useConfig();
const { setShowSidebar } = useContext(SidebarContext);
const specV = useSpec().version();
const version = specV.localeCompare('2.6.0', undefined, { numeric: true });
const isAsyncAPIv2 = version === 0;
const {
typeLabel,
backgroundColor,
} = CommonHelpers.getOperationDesignInformation({
type,
config,
isAsyncAPIv2,
});

return (
Expand Down
27 changes: 15 additions & 12 deletions library/src/helpers/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ export class CommonHelpers {
}
return PayloadType.RECEIVE;
}
static getOperationIdentifier(operation: OperationInterface) {
const config = useConfig();
static getOperationIdentifier({
operation,
config,
}: {
operation: OperationInterface;
config: ConfigInterface;
}) {
if (operation.isSend()) {
if (operation.reply() !== undefined) {
return CommonHelpers.getIdentifier(
Expand Down Expand Up @@ -64,20 +69,19 @@ export class CommonHelpers {
static getOperationDesignInformation({
type,
config,
isAsyncAPIv2,
}: {
type: PayloadType;
config: ConfigInterface;
isAsyncAPIv2: boolean;
}): { borderColor: string; typeLabel: string; backgroundColor: string } {
const specV = useSpec().version();
const version = specV.localeCompare('2.6.0', undefined, { numeric: true });
if (type === PayloadType.RECEIVE) {
return {
borderColor: 'border-green-600 text-green-600',
backgroundColor: 'bg-green-600',
typeLabel:
version === 1
? config.receiveLabel ?? RECEIVE_TEXT_LABEL_DEFAULT_TEXT
: config.publishLabel ?? PUBLISH_LABEL_DEFAULT_TEXT,
typeLabel: !isAsyncAPIv2
? config.receiveLabel ?? RECEIVE_TEXT_LABEL_DEFAULT_TEXT
: config.publishLabel ?? PUBLISH_LABEL_DEFAULT_TEXT,
};
}
if (type === PayloadType.REPLY) {
Expand All @@ -97,10 +101,9 @@ export class CommonHelpers {
return {
borderColor: 'border-blue-600 text-blue-500',
backgroundColor: 'bg-blue-600',
typeLabel:
version === 1
? config.sendLabel ?? SEND_LABEL_DEFAULT_TEXT
: config.subscribeLabel ?? SUBSCRIBE_LABEL_DEFAULT_TEXT,
typeLabel: !isAsyncAPIv2
? config.sendLabel ?? SEND_LABEL_DEFAULT_TEXT
: config.subscribeLabel ?? SUBSCRIBE_LABEL_DEFAULT_TEXT,
};
}
}

0 comments on commit 78420ab

Please sign in to comment.