Skip to content

Commit

Permalink
fix(graph): fix search params reload when back to graph
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongemi committed Jun 17, 2024
1 parent e5d7805 commit 2cceaf6
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 83 deletions.
25 changes: 12 additions & 13 deletions graph/client/src/app/feature-projects/projects-sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -296,23 +296,22 @@ export function ProjectsSidebar(): JSX.Element {
}, [searchParams]);

useIntervalWhen(
() => {
fetchProjectGraph(
async () => {
const response: ProjectGraphClientResponse = await fetchProjectGraph(
projectGraphDataService,
params,
environmentConfig.appConfig
).then((response: ProjectGraphClientResponse) => {
if (response.hash === lastHash) {
return;
}
projectGraphService.send({
type: 'updateGraph',
projects: response.projects,
dependencies: response.dependencies,
fileMap: response.fileMap,
});
setLastHash(response.hash);
);
if (response.hash === lastHash) {
return;
}
projectGraphService.send({
type: 'updateGraph',
projects: response.projects,
dependencies: response.dependencies,
fileMap: response.fileMap,
});
setLastHash(response.hash);
},
5000,
environmentConfig.watch
Expand Down
10 changes: 5 additions & 5 deletions graph/client/src/app/shell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ export function Shell(): JSX.Element {
useLayoutEffect(() => {
setErrors(routerErrors);
}, [routerErrors]);

useIntervalWhen(
() => {
fetchProjectGraph(
async () => {
const response: ProjectGraphClientResponse = await fetchProjectGraph(
projectGraphDataService,
params,
environmentConfig.appConfig
).then((response: ProjectGraphClientResponse) => {
setErrors(response.errors);
});
);
setErrors(response.errors);
},
1000,
environmentConfig.watch
Expand Down
22 changes: 11 additions & 11 deletions graph/client/src/app/ui-components/error-boundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ export function ErrorBoundary() {

useIntervalWhen(
async () => {
fetchProjectGraph(projectGraphDataService, params, appConfig).then(
(data) => {
if (
isRouteErrorResponse(error) &&
error.data.id === 'project-not-found' &&
data.projects.find((p) => p.name === error.data.projectName)
) {
window.location.reload();
}
return;
}
const data = await fetchProjectGraph(
projectGraphDataService,
params,
appConfig
);
if (
isRouteErrorResponse(error) &&
error.data.id === 'project-not-found' &&
data.projects.find((p) => p.name === error.data.projectName)
) {
window.location.reload();
}
},
1000,
watch
Expand Down
16 changes: 8 additions & 8 deletions graph/project-details/src/lib/project-details-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ export function ProjectDetailsPage() {

useIntervalWhen(
async () => {
fetchProjectGraph(projectGraphDataService, params, appConfig).then(
(data) => {
if (data?.hash !== hash) {
window.location.reload();
}
return;
}
const data = await fetchProjectGraph(
projectGraphDataService,
params,
appConfig
);
if (data?.hash !== hash) {
window.location.reload();
}
},
1000,
watch
true
);

return (
Expand Down
19 changes: 7 additions & 12 deletions graph/project-details/src/lib/project-details-wrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export function ProjectDetailsWrapper({
navigate(
routeConstructor(
`/projects/${encodeURIComponent(data.projectName)}`,
true
true,
['expanded'] // omit expanded targets from search params
)
);
}
Expand All @@ -75,7 +76,8 @@ export function ProjectDetailsWrapper({
pathname: `/tasks/${encodeURIComponent(data.targetName)}`,
search: `?projects=${encodeURIComponent(data.projectName)}`,
},
true
true,
['expanded'] // omit expanded targets from search params
)
);
}
Expand All @@ -95,9 +97,9 @@ export function ProjectDetailsWrapper({

const updateSearchParams = (
params: URLSearchParams,
targetNames: string[]
targetNames?: string[]
) => {
if (targetNames.length === 0) {
if (!targetNames || targetNames.length === 0) {
params.delete('expanded');
} else {
params.set('expanded', targetNames.join(','));
Expand All @@ -118,21 +120,14 @@ export function ProjectDetailsWrapper({
if (collapseAllTargets) {
collapseAllTargets();
}
setSearchParams(
(currentSearchParams) => {
currentSearchParams.delete('expanded');
return currentSearchParams;
},
{ replace: true, preventScrollReset: true }
);
};
}, []); // only run on mount

useEffect(() => {
const expandedTargetsParams =
searchParams.get('expanded')?.split(',') || [];

if (expandedTargetsParams.join(',') === expandedTargets.join(',')) {
if (expandedTargetsParams.join(',') === expandedTargets?.join(',')) {
return;
}

Expand Down
25 changes: 17 additions & 8 deletions graph/shared/src/lib/use-interval-when.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useEffect, useRef } from 'react';

export const useIntervalWhen = (
callback: () => void,
callback: () => Promise<void>,
delay: number,
condition: boolean
) => {
const savedCallback = useRef(() => {});
const savedCallback = useRef(() => Promise.resolve());

useEffect(() => {
if (condition) {
Expand All @@ -14,15 +14,24 @@ export const useIntervalWhen = (
}, [callback, condition]);

useEffect(() => {
if (condition) {
const tick = () => {
savedCallback.current();
};
if (!condition) {
return;
}
let timeoutId: NodeJS.Timeout;

async function callTickAfterDelay() {
await savedCallback.current();
if (delay !== null) {
let id = setInterval(tick, delay);
return () => clearInterval(id);
timeoutId = setTimeout(callTickAfterDelay, delay);
}
}

callTickAfterDelay();

return () => {
if (timeoutId) {
clearTimeout(timeoutId);
}
};
}, [delay, condition]);
};
14 changes: 12 additions & 2 deletions graph/shared/src/lib/use-route-constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@ import { getEnvironmentConfig } from './use-environment-config';

export const useRouteConstructor = (): ((
to: To,
retainSearchParams: boolean
retainSearchParams: boolean,
searchParamsKeysToOmit?: string[]
) => To) => {
const { environment } = getEnvironmentConfig();
const { selectedWorkspaceId } = useParams();
const [searchParams] = useSearchParams();

return (to: To, retainSearchParams: true) => {
return (
to: To,
retainSearchParams: boolean = true,
searchParamsKeysToOmit: string[] = []
) => {
if (searchParamsKeysToOmit?.length) {
searchParamsKeysToOmit.forEach((key) => {
searchParams.delete(key);
});
}
let pathname = '';

if (typeof to === 'object') {
Expand Down
4 changes: 2 additions & 2 deletions graph/ui-graph/src/lib/tooltip-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export class GraphTooltipService {
if (graph.getTaskInputs) {
graph.getTaskInputs(event.data.id).then((inputs) => {
if (
this.currentTooltip.type === 'taskNode' &&
this.currentTooltip.props.id === event.data.id
this.currentTooltip?.type === 'taskNode' &&
this.currentTooltip?.props.id === event.data.id
) {
this.openTaskNodeTooltip(event.ref, {
...event.data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,15 @@ export const TargetConfigurationDetailsHeader = ({
// TODO: fix tooltip overflow in collapsed state
data-tooltip={isCollasped ? false : 'View in Task Graph'}
data-tooltip-align-right
onClick={(e) => {
if (isCollasped) {
return;
}
e.stopPropagation();
onViewInTaskGraph({ projectName, targetName });
}}
>
<EyeIcon
className={`h-5 w-5 !cursor-pointer`}
onClick={(e) => {
e.stopPropagation();
onViewInTaskGraph({ projectName, targetName });
}}
/>
<EyeIcon className={`h-5 w-5 !cursor-pointer`} />
</button>
)}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@
"react-markdown": "^8.0.7",
"react-redux": "8.0.5",
"react-refresh": "^0.10.0",
"react-router-dom": "^6.21.2",
"react-router-dom": "^6.23.1",
"react-textarea-autosize": "^8.5.3",
"regenerator-runtime": "0.13.7",
"resolve.exports": "1.1.0",
Expand Down
28 changes: 14 additions & 14 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2cceaf6

Please sign in to comment.