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

refactor: resolve hook violations #448

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/app/login/githublogin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const GithubLogin: React.FC = () => {
setErrorMsg(errorMessage);
setShowError(true);
}
}, []);
}, [searchParams]);

const handleGitHubLogin = () => {
signIn('github', { callbackUrl: '/' }); // Redirect to home page after login
Expand Down
53 changes: 36 additions & 17 deletions src/components/Contribute/Knowledge/Github/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -426,23 +426,42 @@ export const KnowledgeFormGithub: React.FunctionComponent<KnowledgeFormProps> =
setSeedExamples(yamlSeedExampleToFormSeedExample(data.seed_examples));
};

const knowledgeFormData: KnowledgeFormData = {
email: email,
name: name,
submissionSummary: submissionSummary,
domain: domain,
documentOutline: documentOutline,
filePath: filePath,
seedExamples: seedExamples,
knowledgeDocumentRepositoryUrl: knowledgeDocumentRepositoryUrl,
knowledgeDocumentCommit: knowledgeDocumentCommit,
documentName: documentName,
titleWork: titleWork,
linkWork: linkWork,
revision: revision,
licenseWork: licenseWork,
creators: creators
};
const knowledgeFormData: KnowledgeFormData = useMemo(
() => ({
email,
name,
submissionSummary,
domain,
documentOutline,
filePath,
seedExamples,
knowledgeDocumentRepositoryUrl,
knowledgeDocumentCommit,
documentName,
titleWork,
linkWork,
revision,
licenseWork,
creators
}),
[
email,
name,
submissionSummary,
domain,
documentOutline,
filePath,
seedExamples,
knowledgeDocumentRepositoryUrl,
knowledgeDocumentCommit,
documentName,
titleWork,
linkWork,
revision,
licenseWork,
creators
]
);

useEffect(() => {
setDisableAction(!checkKnowledgeFormCompletion(knowledgeFormData));
Expand Down
53 changes: 36 additions & 17 deletions src/components/Contribute/Knowledge/Native/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -425,23 +425,42 @@ export const KnowledgeFormNative: React.FunctionComponent<KnowledgeFormProps> =
setSeedExamples(yamlSeedExampleToFormSeedExample(data.seed_examples));
};

const knowledgeFormData: KnowledgeFormData = {
email: email,
name: name,
submissionSummary: submissionSummary,
domain: domain,
documentOutline: documentOutline,
filePath: filePath,
seedExamples: seedExamples,
knowledgeDocumentRepositoryUrl: knowledgeDocumentRepositoryUrl,
knowledgeDocumentCommit: knowledgeDocumentCommit,
documentName: documentName,
titleWork: titleWork,
linkWork: linkWork,
revision: revision,
licenseWork: licenseWork,
creators: creators
};
const knowledgeFormData: KnowledgeFormData = useMemo(
() => ({
email,
name,
submissionSummary,
domain,
documentOutline,
filePath,
seedExamples,
knowledgeDocumentRepositoryUrl,
knowledgeDocumentCommit,
documentName,
titleWork,
linkWork,
revision,
licenseWork,
creators
}),
[
email,
name,
submissionSummary,
domain,
documentOutline,
filePath,
seedExamples,
knowledgeDocumentRepositoryUrl,
knowledgeDocumentCommit,
documentName,
titleWork,
linkWork,
revision,
licenseWork,
creators
]
);

useEffect(() => {
setDisableAction(!checkKnowledgeFormCompletion(knowledgeFormData));
Expand Down
27 changes: 15 additions & 12 deletions src/components/Contribute/Skill/Github/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// src/components/Contribute/Skill/Github/index.tsx
'use client';
import React, { useEffect, useState } from 'react';
import React, { useEffect, useState, useMemo } from 'react';
import './skills.css';
import { Alert, AlertActionCloseButton, AlertGroup } from '@patternfly/react-core/dist/dynamic/components/Alert';
import { ActionGroup } from '@patternfly/react-core/dist/dynamic/components/Form';
Expand Down Expand Up @@ -324,17 +324,20 @@ export const SkillFormGithub: React.FunctionComponent<SkillFormProps> = ({ skill
setSeedExamples(yamlSeedExampleToFormSeedExample(data.seed_examples));
};

const skillFormData: SkillFormData = {
email: email,
name: name,
submissionSummary: submissionSummary,
documentOutline: documentOutline,
filePath: filePath,
seedExamples: seedExamples,
titleWork: titleWork,
licenseWork: licenseWork,
creators: creators
};
const skillFormData: SkillFormData = useMemo(
() => ({
email,
name,
submissionSummary,
documentOutline,
filePath,
seedExamples,
titleWork,
licenseWork,
creators
}),
[email, name, submissionSummary, documentOutline, filePath, seedExamples, titleWork, licenseWork, creators]
);

useEffect(() => {
setDisableAction(!checkSkillFormCompletion(skillFormData));
Expand Down
27 changes: 15 additions & 12 deletions src/components/Contribute/Skill/Native/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// src/components/Contribute/Skill/Native/index.tsx
'use client';
import React, { useEffect, useState } from 'react';
import React, { useEffect, useState, useMemo } from 'react';
import './skills.css';
import { Alert, AlertActionCloseButton, AlertGroup } from '@patternfly/react-core/dist/dynamic/components/Alert';
import { ActionGroup } from '@patternfly/react-core/dist/dynamic/components/Form';
Expand Down Expand Up @@ -301,17 +301,20 @@ export const SkillFormNative: React.FunctionComponent<SkillFormProps> = ({ skill
setSeedExamples(yamlSeedExampleToFormSeedExample(data.seed_examples));
};

const skillFormData: SkillFormData = {
email: email,
name: name,
submissionSummary: submissionSummary,
documentOutline: documentOutline,
filePath: filePath,
seedExamples: seedExamples,
titleWork: titleWork,
licenseWork: licenseWork,
creators: creators
};
const skillFormData: SkillFormData = useMemo(
() => ({
email,
name,
submissionSummary,
documentOutline,
filePath,
seedExamples,
titleWork,
licenseWork,
creators
}),
[email, name, submissionSummary, documentOutline, filePath, seedExamples, titleWork, licenseWork, creators]
);

useEffect(() => {
setDisableAction(!checkSkillFormCompletion(skillFormData));
Expand Down
2 changes: 1 addition & 1 deletion src/components/GithubAccessPopup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const GithubAccessPopup: React.FunctionComponent<Props> = ({ onAccept }) => {
}
};
showPopupWarning();
}, []);
}, [onAccept]);

const setDecisionAndClose = () => {
setIsOpen(false);
Expand Down
61 changes: 32 additions & 29 deletions src/components/PathService/PathService.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// /src/components/PathService.tsx
import React, { useState, useEffect, useRef } from 'react';
import React, { useState, useEffect, useRef, useCallback } from 'react';
import { SearchInput } from '@patternfly/react-core/dist/dynamic/components/SearchInput';
import { List } from '@patternfly/react-core/dist/dynamic/components/List';
import { ListItem } from '@patternfly/react-core/dist/dynamic/components/List';
Expand All @@ -23,40 +23,43 @@ const PathService: React.FC<PathServiceProps> = ({ reset, rootPath, path, handle
const inputRef = useRef<HTMLInputElement>(null);
const [validPath, setValidPath] = React.useState<ValidatedOptions>();

const validatePath = () => {
const validatePath = useCallback(() => {
if (inputValue.trim().length > 0) {
setValidPath(ValidatedOptions.success);
return;
}
setValidPath(ValidatedOptions.error);
};

const fetchData = async (subpath: string) => {
try {
const response = await fetch('/api/tree', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ root_path: rootPath, dir_name: subpath })
});

if (!response.ok) {
console.warn('Failed to get path service tree for subpath ( ' + subpath + ' ) from server.');
}
}, [inputValue]);

const result = await response.json();
// set items to be displayed in the dropdown
if (result.data === null || result.data.length === 0) {
const fetchData = useCallback(
async (subpath: string) => {
try {
const response = await fetch('/api/tree', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ root_path: rootPath, dir_name: subpath })
});

if (!response.ok) {
console.warn('Failed to get path service tree for subpath ( ' + subpath + ' ) from server.');
}

const result = await response.json();
// set items to be displayed in the dropdown
if (result.data === null || result.data.length === 0) {
setItems([]);
return;
}
setItems(result.data.map((item: string) => item.valueOf()));
} catch (error) {
console.warn('Error fetching path service data:', error);
setItems([]);
return;
}
setItems(result.data.map((item: string) => item.valueOf()));
} catch (error) {
console.warn('Error fetching path service data:', error);
setItems([]);
}
};
},
[rootPath]
);

useEffect(() => {
setInputValue('');
Expand All @@ -79,7 +82,7 @@ const PathService: React.FC<PathServiceProps> = ({ reset, rootPath, path, handle
return () => {
window.removeEventListener('keydown', handleEsc);
};
}, []);
}, [path]);

useEffect(() => {
// check if input value is empty or ends with a slash
Expand All @@ -91,7 +94,7 @@ const PathService: React.FC<PathServiceProps> = ({ reset, rootPath, path, handle
setItems([]);
}
validatePath();
}, [inputValue]);
}, [inputValue, fetchData, handlePathChange, validatePath]);

const handleChange = (value: string) => {
setInputValue(value);
Expand Down
Loading