Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [M3-8538] - Incorrect Commands in Linode CLI Modal #11303

Merged
merged 6 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
mjac0bs marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useTheme } from '@mui/material/styles';
import React, { useMemo } from 'react';
import { useFormContext } from 'react-hook-form';

import { CodeBlock } from 'src/components/CodeBlock/CodeBlock';
import { Link } from 'src/components/Link';
Expand All @@ -8,6 +9,9 @@ import { Typography } from 'src/components/Typography';
import { sendApiAwarenessClickEvent } from 'src/utilities/analytics/customEventAnalytics';
import { generateCurlCommand } from 'src/utilities/codesnippets/generate-cURL';

import { useLinodeCreateQueryParams } from '../utilities';

import type { LinodeCreateFormValues } from '../utilities';
import type { CreateLinodeRequest } from '@linode/api-v4/lib/linodes';

export interface CurlTabPanelProps {
Expand All @@ -19,10 +23,20 @@ export interface CurlTabPanelProps {
export const CurlTabPanel = ({ index, payLoad, title }: CurlTabPanelProps) => {
const theme = useTheme();

const curlCommand = useMemo(
() => generateCurlCommand(payLoad, '/linode/instances'),
[payLoad]
);
const { getValues } = useFormContext<LinodeCreateFormValues>();
const sourceLinodeID = getValues('linode.id');

const { params } = useLinodeCreateQueryParams();
const linodeCLIAction = params.type === 'Clone Linode' ? 'clone' : 'create';
const path =
linodeCLIAction === 'create'
? '/linode/instances'
: `linode/instances/${sourceLinodeID}/clone`;

const curlCommand = useMemo(() => generateCurlCommand(payLoad, path), [
path,
payLoad,
]);

return (
<SafeTabPanel index={index}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useMemo } from 'react';
import { useFormContext } from 'react-hook-form';

import { CodeBlock } from 'src/components/CodeBlock/CodeBlock';
import { Link } from 'src/components/Link';
Expand All @@ -7,6 +8,9 @@
import { sendApiAwarenessClickEvent } from 'src/utilities/analytics/customEventAnalytics';
import { generateCLICommand } from 'src/utilities/codesnippets/generate-cli';

import { useLinodeCreateQueryParams } from '../utilities';

import type { LinodeCreateFormValues } from '../utilities';
import type { CreateLinodeRequest } from '@linode/api-v4/lib/linodes';

export interface LinodeCLIPanelProps {
Expand All @@ -20,7 +24,16 @@
payLoad,
title,
}: LinodeCLIPanelProps) => {
const cliCommand = useMemo(() => generateCLICommand(payLoad), [payLoad]);
const { params } = useLinodeCreateQueryParams();
const linodeCLIAction = params.type;

const { getValues } = useFormContext<LinodeCreateFormValues>();

Check failure on line 30 in packages/manager/src/features/Linodes/LinodeCreate/ApiAwarenessModal/LinodeCLIPanel.tsx

View workflow job for this annotation

GitHub Actions / test-manager

src/features/Linodes/LinodeCreate/ApiAwarenessModal/ApiAwarenessModal.test.tsx > ApiAwarenessModal > Should render ApiAwarenessModal componet when enabled

TypeError: Cannot destructure property 'getValues' of 'useFormContext(...)' as it is null. ❯ LinodeCLIPanel src/features/Linodes/LinodeCreate/ApiAwarenessModal/LinodeCLIPanel.tsx:30:11 ❯ renderWithHooks ../../node_modules/react-dom/cjs/react-dom.development.js:15486:18 ❯ mountIndeterminateComponent ../../node_modules/react-dom/cjs/react-dom.development.js:20103:13 ❯ beginWork ../../node_modules/react-dom/cjs/react-dom.development.js:21626:16 ❯ beginWork$1 ../../node_modules/react-dom/cjs/react-dom.development.js:27465:14 ❯ performUnitOfWork ../../node_modules/react-dom/cjs/react-dom.development.js:26599:12 ❯ workLoopSync ../../node_modules/react-dom/cjs/react-dom.development.js:26505:5 ❯ renderRootSync ../../node_modules/react-dom/cjs/react-dom.development.js:26473:7 ❯ recoverFromConcurrentError ../../node_modules/react-dom/cjs/react-dom.development.js:25889:20 ❯ performSyncWorkOnRoot ../../node_modules/react-dom/cjs/react-dom.development.js:26135:20

Check failure on line 30 in packages/manager/src/features/Linodes/LinodeCreate/ApiAwarenessModal/LinodeCLIPanel.tsx

View workflow job for this annotation

GitHub Actions / test-manager

src/features/Linodes/LinodeCreate/ApiAwarenessModal/ApiAwarenessModal.test.tsx > ApiAwarenessModal > Should invoke onClose handler upon cliking close button

TypeError: Cannot destructure property 'getValues' of 'useFormContext(...)' as it is null. ❯ LinodeCLIPanel src/features/Linodes/LinodeCreate/ApiAwarenessModal/LinodeCLIPanel.tsx:30:11 ❯ renderWithHooks ../../node_modules/react-dom/cjs/react-dom.development.js:15486:18 ❯ mountIndeterminateComponent ../../node_modules/react-dom/cjs/react-dom.development.js:20103:13 ❯ beginWork ../../node_modules/react-dom/cjs/react-dom.development.js:21626:16 ❯ beginWork$1 ../../node_modules/react-dom/cjs/react-dom.development.js:27465:14 ❯ performUnitOfWork ../../node_modules/react-dom/cjs/react-dom.development.js:26599:12 ❯ workLoopSync ../../node_modules/react-dom/cjs/react-dom.development.js:26505:5 ❯ renderRootSync ../../node_modules/react-dom/cjs/react-dom.development.js:26473:7 ❯ recoverFromConcurrentError ../../node_modules/react-dom/cjs/react-dom.development.js:25889:20 ❯ performSyncWorkOnRoot ../../node_modules/react-dom/cjs/react-dom.development.js:26135:20
mjac0bs marked this conversation as resolved.
Show resolved Hide resolved
const sourceLinodeID = getValues('linode.id');

const cliCommand = useMemo(
() => generateCLICommand(payLoad, sourceLinodeID, linodeCLIAction),
[linodeCLIAction, payLoad, sourceLinodeID]
);

return (
<SafeTabPanel index={index}>
Expand Down
18 changes: 14 additions & 4 deletions packages/manager/src/utilities/codesnippets/generate-cli.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {
import type { PlacementGroup } from '@linode/api-v4';
import type {
ConfigInterfaceIPv4,
UserData,
} from '@linode/api-v4/lib/linodes/types';

import type { PlacementGroup } from '@linode/api-v4';

// Credit: https://github.com/xxorax/node-shell-escape
function escapeStringForCLI(s: string): string {
if (/[^A-Za-z0-9_\/:=-]/.test(s)) {
Expand Down Expand Up @@ -111,7 +110,18 @@ const dataEntriesReduce = (acc: string[], [key, value]: JSONFieldToArray) => {
return acc;
};

export const generateCLICommand = (data: {}) => {
export const generateCLICommand = (
data: {},
mjac0bs marked this conversation as resolved.
Show resolved Hide resolved
sourceLinodeID?: number,
linodeCLIAction?: string
) => {
const dataForCLI = Object.entries(data).reduce(dataEntriesReduce, []);

if (linodeCLIAction === 'Clone Linode') {
return `linode-cli linodes clone ${sourceLinodeID} \\\n${dataForCLI.join(
' \\\n'
)}`;
}

return `linode-cli linodes create \\\n${dataForCLI.join(' \\\n')}`;
};
Loading