Skip to content

Commit

Permalink
feat: Multiple minor changes (#769)
Browse files Browse the repository at this point in the history
  • Loading branch information
amanharwara authored Dec 10, 2021
1 parent 024d44f commit 24c6b83
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 48 deletions.
14 changes: 11 additions & 3 deletions app/assets/javascripts/components/NotesListOptionsMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { WebApplication } from '@/ui_models/application';
import { CollectionSort, PrefKey } from '@standardnotes/snjs';
import { observer } from 'mobx-react-lite';
import { FunctionComponent } from 'preact';
import { useState } from 'preact/hooks';
import { useRef, useState } from 'preact/hooks';
import { Icon } from './Icon';
import { Menu } from './menu/Menu';
import { MenuItem, MenuItemSeparator, MenuItemType } from './menu/MenuItem';
import { toDirective } from './utils';
import { toDirective, useCloseOnClickOutside } from './utils';

type Props = {
application: WebApplication;
Expand Down Expand Up @@ -108,8 +108,16 @@ flex flex-col py-2 bottom-0 left-2 absolute';
application.setPreference(PrefKey.NotesHideProtected, !hideProtected);
};

const menuRef = useRef<HTMLDivElement>(null);

useCloseOnClickOutside(menuRef as any, (open: boolean) => {
if (!open) {
setShowMenuFalse();
}
});

return (
<div className={menuClassName}>
<div ref={menuRef} className={menuClassName}>
<Menu a11yLabel="Sort by" closeMenu={setShowMenuFalse}>
<div className="px-3 my-1 text-xs font-semibold color-text uppercase">
Sort by
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,55 +76,67 @@ const QuickSettingsMenu: FunctionComponent<MenuProps> = observer(
}, [focusModeEnabled]);

const reloadThemes = useCallback(() => {
application.streamItems(ContentType.Theme, () => {
const themes = application.getDisplayableItems(
ContentType.Theme
) as SNTheme[];
setThemes(
themes.sort((a, b) => {
const aIsLayerable = a.isLayerable();
const bIsLayerable = b.isLayerable();
const themes = application.getDisplayableItems(
ContentType.Theme
) as SNTheme[];
setThemes(
themes.sort((a, b) => {
const aIsLayerable = a.isLayerable();
const bIsLayerable = b.isLayerable();

if (aIsLayerable && !bIsLayerable) {
return 1;
} else if (!aIsLayerable && bIsLayerable) {
return -1;
} else {
return a.package_info.name.toLowerCase() <
b.package_info.name.toLowerCase()
? -1
: 1;
}
})
);
setDefaultThemeOn(
!themes.find((theme) => theme.active && !theme.isLayerable())
);
});
if (aIsLayerable && !bIsLayerable) {
return 1;
} else if (!aIsLayerable && bIsLayerable) {
return -1;
} else {
return a.package_info.name.toLowerCase() <
b.package_info.name.toLowerCase()
? -1
: 1;
}
})
);
setDefaultThemeOn(
!themes.find((theme) => theme.active && !theme.isLayerable())
);
}, [application]);

const reloadToggleableComponents = useCallback(() => {
application.streamItems(ContentType.Component, () => {
const toggleableComponents = (
application.getDisplayableItems(
ContentType.Component
) as SNComponent[]
).filter((component) =>
[ComponentArea.EditorStack, ComponentArea.TagsList].includes(
component.area
)
);
setToggleableComponents(toggleableComponents);
});
const toggleableComponents = (
application.getDisplayableItems(ContentType.Component) as SNComponent[]
).filter((component) =>
[ComponentArea.EditorStack, ComponentArea.TagsList].includes(
component.area
)
);
setToggleableComponents(toggleableComponents);
}, [application]);

useEffect(() => {
reloadThemes();
}, [reloadThemes]);
const cleanupItemStream = application.streamItems(
ContentType.Theme,
() => {
reloadThemes();
}
);

return () => {
cleanupItemStream();
};
}, [application, reloadThemes]);

useEffect(() => {
reloadToggleableComponents();
}, [reloadToggleableComponents]);
const cleanupItemStream = application.streamItems(
ContentType.Component,
() => {
reloadToggleableComponents();
}
);

return () => {
cleanupItemStream();
};
}, [application, reloadToggleableComponents]);

useEffect(() => {
if (themesMenuOpen) {
Expand Down Expand Up @@ -274,18 +286,18 @@ const QuickSettingsMenu: FunctionComponent<MenuProps> = observer(
</Disclosure>
) : null}
{toggleableComponents.map((component) => (
<Switch
className="sn-dropdown-item focus:bg-info-backdrop focus:shadow-none"
checked={component.active}
onChange={() => {
<button
className="sn-dropdown-item justify-between focus:bg-info-backdrop focus:shadow-none"
onClick={() => {
toggleComponent(component);
}}
>
<div className="flex items-center">
<Icon type="window" className="color-neutral mr-2" />
{component.name}
</div>
</Switch>
<Switch checked={component.active} className="px-0" />
</button>
))}
<FocusModeSwitch
application={application}
Expand Down

0 comments on commit 24c6b83

Please sign in to comment.