Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sheet height locking too early for filter help #10391

Merged
merged 6 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions src/app/dim-ui/Sheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export default function Sheet({
const sheetContents = useRef<HTMLDivElement | null>(null);

const [frozenHeight, setFrozenHeight] = useState<number | undefined>(undefined);
const frozenHeightIntervalRef = useRef<NodeJS.Timeout | undefined>(undefined);
const [disabled, setParentDisabled] = useDisableParent(forceDisabled);

const reducedMotion = Boolean(useReducedMotion());
Expand Down Expand Up @@ -222,13 +223,20 @@ export default function Sheet({
);

useLayoutEffect(() => {
clearInterval(frozenHeightIntervalRef.current);
if (freezeInitialHeight && sheetContents.current && !frozenHeight) {
if (sheetContents.current.clientHeight > 0) {
setFrozenHeight(sheetContents.current.clientHeight);
} else {
setTimeout(() => {
sheetContents.current && setFrozenHeight(sheetContents.current.clientHeight);
}, 500);
const setHeight = () => {
if (!sheetContents.current || sheetContents.current.clientHeight === 0) {
return false;
}
setFrozenHeight(sheetContents.current.clientHeight);
frozenHeightIntervalRef.current = undefined;
return true;
};
frozenHeightIntervalRef.current = tryRepeatedlyWithLimit(setHeight);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is just a slightly better version of the intended functionality. I don't think it is explicitly needed for this fix, but it was something I noticed upon having a read of the code. Happy to remove if requested.

Note: I did it initially with a timeout and a backoff but cancelation came to mind. An interval was just simpler to implement then having to deal with recursive timeouts with a backoff so I did this instead.

}
}
}, [freezeInitialHeight, frozenHeight]);
Expand Down Expand Up @@ -321,3 +329,17 @@ export default function Sheet({
</Portal>
);
}

function tryRepeatedlyWithLimit(callback: () => boolean, timeout = 500, limit = 5_000) {
let totalTime = 0;
return setInterval(() => {
if (totalTime > limit) {
return;
}
const res = callback();
totalTime += timeout;
if (res) {
return;
}
}, timeout);
}
35 changes: 23 additions & 12 deletions src/app/search/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { toggleSearchResults } from 'app/shell/actions';
import { useIsPhonePortrait } from 'app/shell/selectors';
import { useThunkDispatch } from 'app/store/thunk-dispatch';
import { isiOSBrowser } from 'app/utils/browsers';
import { Portal } from 'app/utils/temp-container';
import clsx from 'clsx';
import { UseComboboxState, UseComboboxStateChangeOptions, useCombobox } from 'downshift';
import { AnimatePresence, LayoutGroup, Variants, motion } from 'framer-motion';
Expand Down Expand Up @@ -545,21 +546,31 @@ function SearchBar(
</LayoutGroup>

{filterHelpOpen && (
<Sheet
onClose={() => setFilterHelpOpen(false)}
header={
<>
<h1>{t('Header.Filters')}</h1>
<UserGuideLink topic="Item-Search" />
</>
<Suspense
fallback={
<Portal>
<Loading message={t('Loading.FilterHelp')} />
</Portal>
Comment on lines +550 to +553
Copy link
Contributor Author

@ryan-rushton ryan-rushton May 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just going to delete this until I realise the Loading component is a full screen mask. For it to display, I needed to stick it in a portal.

To test it, delay the loading using the following

const LazyFilterHelp = lazy(
  () =>
    new Promise<{ default: () => React.ReactNode }>((resolve) => {
      setTimeout(() => resolve(import(/* webpackChunkName: "filter-help" */ './FilterHelp')), 5000);
    }),
);

Here is a video of the current state

Screen.Recording.2024-05-06.at.9.40.40.AM.mov

}
freezeInitialHeight
sheetClassName={styles.filterHelp}
>
<Suspense fallback={<Loading message={t('Loading.FilterHelp')} />}>
{/* Because FilterHelp suspends, the entire sheet will suspend while it is loaded.
* This stops us having issues with incorrect frozen initial heights as it will
* get locked to the fallback height if we don't do this.
*/}
ryan-rushton marked this conversation as resolved.
Show resolved Hide resolved
<Sheet
onClose={() => setFilterHelpOpen(false)}
header={
<>
<h1>{t('Header.Filters')}</h1>
<UserGuideLink topic="Item-Search" />
</>
}
freezeInitialHeight
sheetClassName={styles.filterHelp}
>
<LazyFilterHelp />
</Suspense>
</Sheet>
</Sheet>
</Suspense>
)}

{autocompleteMenu}
Expand Down
Loading