Skip to content

Commit

Permalink
fix(ui): do not use hook use if logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Gggpound committed Mar 26, 2024
1 parent 8724366 commit 4f4ac79
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions packages/ui/src/views/components/doc-bars/ToolbarItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import { ITextSelectionRenderManager } from '@univerjs/engine-render';
import { MoreDownSingle } from '@univerjs/icons';
import { useDependency, useInjector } from '@wendellhu/redi/react-bindings';
import type { Ref } from 'react';
import React, { forwardRef, useEffect, useState } from 'react';
import React, { forwardRef, useEffect, useMemo, useState } from 'react';
import type { Subscription } from 'rxjs';
import { isObservable } from 'rxjs';
import { isObservable, Observable } from 'rxjs';

import { ComponentManager } from '../../../common/component-manager';
import { CustomLabel } from '../../../components/custom-label/CustomLabel';
Expand Down Expand Up @@ -108,14 +108,29 @@ export const ToolbarItem = forwardRef((props: IDisplayMenuItem<IMenuItem>, ref:
const tooltipTitle = localeService.t(tooltip ?? '') + (shortcut ? ` (${shortcut})` : '');

const { selections } = props as IDisplayMenuItem<IMenuSelectorItem>;
const selections$ = useMemo(() => {
if (isObservable(selections)) {
return selections;
} else {
return new Observable< typeof selections>((subscribe) => {
subscribe.next(selections);
});
}
}, [selections]);
const options = useObservable(selections$) as IValueOption[];

const options = isObservable(selections) ? useObservable(selections) : selections as IValueOption[];
let iconToDisplay = icon;
if (isObservable(icon)) {
iconToDisplay = useObservable(icon, undefined, true);
} else {
iconToDisplay = options?.find((o) => o.value === value)?.icon ?? icon;
}
const icon$ = useMemo(() => {
if (isObservable(icon)) {
return icon;
} else {
return new Observable< typeof icon>((subscribe) => {
const v = options?.find((o) => o.value === value)?.icon ?? icon;
subscribe.next(v);
});
}
}, [icon, options, value]);

const iconToDisplay = useObservable(icon$, undefined, true);

function renderSelectorType(menuType: MenuItemType) {
function handleSelect(option: IValueOption) {
Expand Down

0 comments on commit 4f4ac79

Please sign in to comment.