nx/graph/shared/src/lib/expanded-targets-provider.tsx
Emily Xiong 66ff751f0d
feat(graph): add expandedTargets to project details on nx dev (#26911)
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

https://nx.dev/getting-started/tutorials/gradle-tutorial#running-tasks

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->


https://nx-dev-git-feat-graph-add-expand-targets-nrwl.vercel.app/getting-started/tutorials/gradle-tutorial#running-tasks

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #

---------

Co-authored-by: Max Kless <maxk@nrwl.io>
2024-08-20 13:46:43 -04:00

47 lines
1.1 KiB
TypeScript

import { createContext, useState } from 'react';
export const ExpandedTargetsContext = createContext<{
expandedTargets?: string[];
setExpandedTargets?: (expandedTargets: string[]) => void;
toggleTarget?: (targetName: string) => void;
collapseAllTargets?: () => void;
}>({});
export const ExpandedTargetsProvider = ({
children,
initialExpanededTargets = [],
}: {
children: React.ReactNode;
initialExpanededTargets?: string[];
}) => {
const [expandedTargets, setExpandedTargets] = useState<string[]>(
initialExpanededTargets
);
const toggleTarget = (targetName: string) => {
setExpandedTargets((prevExpandedTargets) => {
if (prevExpandedTargets.includes(targetName)) {
return prevExpandedTargets.filter((name) => name !== targetName);
}
return [...prevExpandedTargets, targetName];
});
};
const collapseAllTargets = () => {
setExpandedTargets([]);
};
return (
<ExpandedTargetsContext.Provider
value={{
expandedTargets,
setExpandedTargets,
toggleTarget,
collapseAllTargets,
}}
>
{children}
</ExpandedTargetsContext.Provider>
);
};