Skip to content

Commit

Permalink
Maintenance: Consolidate useMergedRef and convert to typescript (#1733)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikesGlitch authored Sep 24, 2023
1 parent 3c4f62b commit 5f347bb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 24 deletions.
24 changes: 1 addition & 23 deletions packages/desktop-client/src/components/sort.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,17 @@ import React, {
useEffect,
useRef,
useLayoutEffect,
useMemo,
useState,
useContext,
type RefCallback,
type MutableRefObject,
type Context,
type Ref,
} from 'react';
import { useDrag, useDrop } from 'react-dnd';

import { useMergedRefs } from '../hooks/useMergedRefs';
import { theme } from '../style';

import View from './common/View';

function useMergedRefs<T>(
ref1: RefCallback<T> | MutableRefObject<T>,
ref2: RefCallback<T> | MutableRefObject<T>,
): Ref<T> {
return useMemo(() => {
function ref(value) {
[ref1, ref2].forEach(ref => {
if (typeof ref === 'function') {
ref(value);
} else if (ref != null) {
ref.current = value;
}
});
}

return ref;
}, [ref1, ref2]);
}

type DragState = {
state: 'start-preview' | 'start' | 'end';
type?: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { useMemo } from 'react';
import type { MutableRefObject, Ref, RefCallback } from 'react';

export function useMergedRefs(ref1, ref2) {
export function useMergedRefs<T>(
ref1: RefCallback<T> | MutableRefObject<T>,
ref2: RefCallback<T> | MutableRefObject<T>,
): Ref<T> {
return useMemo(() => {
function ref(value) {
[ref1, ref2].forEach(ref => {
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/1733.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Maintenance
authors: [MikesGlitch]
---

Consolidate useMergedRefs hook and convert it to TypeScript.

0 comments on commit 5f347bb

Please sign in to comment.