Skip to content

Commit

Permalink
feat(component/menu): allow inject auth token to display in usebox (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
luvkapur authored Nov 7, 2024
1 parent fd6f86f commit 0d11de8
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 11 deletions.
1 change: 1 addition & 0 deletions scopes/component/component/component.ui.runtime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ export class ComponentUI {
componentIdStr={options.componentId}
useComponentFilters={options.useComponentFilters}
RightNode={options.RightNode}
authToken={options.authToken}
/>
);
}
Expand Down
1 change: 1 addition & 0 deletions scopes/component/component/get-component-opts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export type GetComponentsOptions = {
RightNode?: React.ReactNode;
className?: string;
routes?: RouteProps[];
authToken?: string;
};
12 changes: 10 additions & 2 deletions scopes/component/component/ui/menu/menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { useComponent as useComponentQuery, UseComponentType, Filters } from '..
import { CollapsibleMenuNav } from './menu-nav';
import { OrderedNavigationSlot, ConsumeMethodSlot, ConsumePluginProps } from './nav-plugin';
import { useIdFromLocation } from '../use-component-from-location';

import styles from './menu.module.scss';

export type RightSideMenuItem = { item: ReactNode; order: number };
Expand Down Expand Up @@ -69,6 +70,8 @@ export type MenuProps = {
};

path?: string;

authToken?: string;
};
function getComponentIdStr(componentIdStr?: string | (() => string | undefined)): string | undefined {
if (isFunction(componentIdStr)) return componentIdStr();
Expand All @@ -91,6 +94,7 @@ export function ComponentMenu({
useComponent,
path,
useComponentFilters,
authToken,
}: MenuProps) {
const idFromLocation = useIdFromLocation();
const componentIdStrWithScopeFromLocation = useIdFromLocation(undefined, true);
Expand All @@ -117,6 +121,7 @@ export function ComponentMenu({
componentId={componentId?.toString() || idFromLocation}
useComponent={useComponentVersions}
componentFilters={componentFilters}
authToken={authToken}
// loading={loading}
/>
{rightSideItems.map(({ item }) => item)}
Expand Down Expand Up @@ -159,6 +164,7 @@ export type VersionRelatedDropdownsProps = {
showVersionDetails?: boolean;
getActiveTabIndex?: GetActiveTabIndex;
};
authToken?: string;
};
export type UseComponentVersionsProps = {
skip?: boolean;
Expand Down Expand Up @@ -294,17 +300,19 @@ export function VersionRelatedDropdowns(props: VersionRelatedDropdownsProps) {
const currentVersion =
isWorkspace && !isNew && !location?.search.includes('version') ? 'workspace' : (_currentVersion ?? '');

const authToken = props.authToken;

const consumeMethodProps: ConsumePluginProps | undefined = React.useMemo(() => {
return id
? {
id,
packageName: packageName ?? '',
latest,
options: { viewedLane, disableInstall: !packageName },
authToken,
}
: undefined;
}, [id, packageName, latest, viewedLane]);

}, [id, packageName, latest, viewedLane, authToken]);
const methods = useConsumeMethods(consumeMethods, consumeMethodProps);
const hasMethods = methods?.length > 0;

Expand Down
1 change: 1 addition & 0 deletions scopes/component/component/ui/menu/nav-plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type ConsumePluginProps = {
// @deprecated - pass id, packageName and latest instead via props
componentModel?: ComponentModel;
options?: ConsumePluginOptions;
authToken?: string;
};

export type ConsumePlugin = (props: ConsumePluginProps) => ConsumeMethod | undefined;
Expand Down
13 changes: 12 additions & 1 deletion scopes/dependencies/pnpm/pnpm.ui.runtime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,36 @@ export class PnpmUI {

constructor(private compUI: ComponentUI) {}

private getNpmConfig(registry: string, authToken?: string): string {
const registryUrl = 'https://node-registry.bit.cloud';
const configs = [
`${registry}:registry="${registryUrl}"`,
authToken ? `"//node-registry.bit.cloud/:_authToken=${authToken}"` : undefined,
].filter(Boolean);
return `npm config set ${configs.join(' ')}`;
}

private consumeMethod: ConsumePlugin = ({
id: componentId,
packageName: packageNameFromProps,
latest: latestFromProps,
options,
componentModel,
authToken,
}) => {
const packageName = componentModel?.packageName || packageNameFromProps;
const latest = componentModel?.latest || latestFromProps;

const registry = packageName.split('/')[0];
const packageVersion =
componentId.version === latest ? '' : `@${this.compUI.formatToInstallableVersion(componentId.version as string)}`;
const npmConfig = this.getNpmConfig(registry, authToken);

return {
Title: <img style={{ height: '16px', marginTop: '-2px' }} src="https://static.bit.dev/brands/pnpm.svg" />,
Component: !options?.hide ? (
<Install
config={`npm config set '${registry}:registry' https://node-registry.bit.cloud`}
config={npmConfig}
componentName={componentId.name}
packageManager="pnpm"
copyString={`pnpm i ${packageName}${packageVersion}`}
Expand Down
13 changes: 12 additions & 1 deletion scopes/dependencies/yarn/yarn.ui.runtime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,38 @@ export class YarnUI {

constructor(private compUI: ComponentUI) {}

private getNpmConfig(registry: string, authToken?: string): string {
const registryUrl = 'https://node-registry.bit.cloud';
const configs = [
`${registry}:registry="${registryUrl}"`,
authToken ? `"//node-registry.bit.cloud/:_authToken=${authToken}"` : undefined,
].filter(Boolean);
return `npm config set ${configs.join(' ')}`;
}

private consumeMethod: ConsumePlugin = ({
packageName: packageNameFromProps,
id: componentId,
latest: latestFromProps,
options,
componentModel,
authToken,
}) => {
const packageName = componentModel?.packageName || packageNameFromProps;
const latest = componentModel?.latest || latestFromProps;

const registry = packageName.split('/')[0];
const packageVersion =
componentId.version === latest ? '' : `@${this.compUI.formatToInstallableVersion(componentId.version as string)}`;
const npmConfig = this.getNpmConfig(registry, authToken);

return {
Title: (
<img style={{ height: '17px', paddingTop: '4px' }} src="https://static.bit.dev/brands/logo-yarn-text.svg" />
),
Component: !options?.hide ? (
<Install
config={`npm config set '${registry}:registry' https://node-registry.bit.cloud`}
config={npmConfig}
componentName={componentId.name}
packageManager="yarn"
copyString={`yarn add ${packageName}${packageVersion}`}
Expand Down
5 changes: 3 additions & 2 deletions scopes/docs/docs/overview/overview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export type OverviewProps = {
export function Overview({ titleBadges, overviewOptions, previewProps, getEmptyState, TaggedAPI }: OverviewProps) {
const component = useContext(ComponentContext);
const componentDescriptor = useComponentDescriptor();
const { current } = useThemePicker();
const theme = useThemePicker();
const currentTheme = theme?.current;
const overviewProps = flatten(overviewOptions.values())[0];
const showHeader = !component.preview?.legacyHeader;
const EmptyState = getEmptyState && getEmptyState();
Expand All @@ -62,7 +63,7 @@ export function Overview({ titleBadges, overviewOptions, previewProps, getEmptyS

const overviewPropsValues = overviewProps && overviewProps();

const themeParams = current?.themeName ? `theme=${current?.themeName}` : '';
const themeParams = currentTheme?.themeName ? `theme=${currentTheme?.themeName}` : '';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { onLoad, style, ...rest } = previewProps || {};
Expand Down
1 change: 0 additions & 1 deletion scopes/harmony/cli-reference/cli-reference.docs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
description: 'Bit command synopses. Bit version: 1.8.103'
labels: ['cli', 'mdx', 'docs']
---

13 changes: 12 additions & 1 deletion scopes/pkg/pkg/pkg.ui.runtime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,22 @@ export class PkgUI {

constructor(private compUI: ComponentUI) {}

private getNpmConfig(registry: string, authToken?: string): string {
const registryUrl = 'https://node-registry.bit.cloud';
const configs = [
`${registry}:registry="${registryUrl}"`,
authToken ? `"//node-registry.bit.cloud/:_authToken=${authToken}"` : undefined,
].filter(Boolean);
return `npm config set ${configs.join(' ')}`;
}

private npmConsumeMethod: ConsumePlugin = ({
packageName: packageNameFromProps,
latest: latestFromProps,
id: componentId,
options,
componentModel,
authToken,
}) => {
const packageName = componentModel?.packageName || packageNameFromProps;
const latest = componentModel?.latest || latestFromProps;
Expand All @@ -31,12 +41,13 @@ export class PkgUI {

const packageVersion =
componentId.version === latest ? '' : `@${this.compUI.formatToInstallableVersion(componentId.version as string)}`;
const npmConfig = this.getNpmConfig(registry, authToken);

return {
Title: <img style={{ width: '30px' }} src="https://static.bit.dev/brands/logo-npm-new.svg" />,
Component: !options?.hide ? (
<Install
config={`npm config set '${registry}:registry' https://node-registry.bit.cloud`}
config={npmConfig}
componentName={componentId.name}
packageManager="npm"
copyString={`npm i ${packageName}${packageVersion}`}
Expand Down
5 changes: 3 additions & 2 deletions scopes/workspace/workspace/ui/workspace/workspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ export type WorkspaceProps = {
export function Workspace({ routeSlot, menuSlot, sidebar, workspaceUI, onSidebarTogglerChange }: WorkspaceProps) {
const reactions = useComponentNotifications();
const { workspace } = useWorkspace(reactions);
const { current } = useThemePicker();
const theme = useThemePicker();
const currentTheme = theme?.current;
const [isSidebarOpen, handleSidebarToggle] = useReducer((x) => !x, true);
const sidebarOpenness = isSidebarOpen ? Layout.row : Layout.right;
const themeName = current?.themeName || 'light';
const themeName = currentTheme?.themeName || 'light';
onSidebarTogglerChange(handleSidebarToggle);

if (!workspace) {
Expand Down
2 changes: 1 addition & 1 deletion workspace.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,8 @@
"@teambit/ui-foundation.ui.use-box.back-button": "^0.0.119",
"@teambit/ui-foundation.ui.use-box.bit-info": "^0.0.128",
"@teambit/ui-foundation.ui.use-box.bottom-link": "^0.0.119",
"@teambit/ui-foundation.ui.use-box.menu": "^1.0.12",
"@teambit/ui-foundation.ui.use-box.dropdown": "^0.0.143",
"@teambit/ui-foundation.ui.use-box.menu": "^1.0.11",
"@teambit/ui-foundation.ui.use-box.scope-menu": "^0.0.146",
"@teambit/ui-foundation.ui.use-box.tab": "^0.0.122",
"@teambit/ui-foundation.ui.use-box.tab-content": "^0.0.122",
Expand Down

0 comments on commit 0d11de8

Please sign in to comment.