-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9590fa9
commit b98b87a
Showing
9 changed files
with
150 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { useEffect, useRef } from 'react'; | ||
|
||
/** | ||
* A debounced alternative to `useEffect`. | ||
*/ | ||
export function useDebouncedEffect( | ||
callback: () => void | (() => void), | ||
delay = 200, | ||
deps: unknown[] = [], | ||
) { | ||
const firstTimeRef = useRef(true); | ||
const clearRef = useRef<(() => void) | void>(undefined); | ||
|
||
useEffect( | ||
() => { | ||
if (firstTimeRef.current) { | ||
firstTimeRef.current = false; | ||
return; | ||
} | ||
|
||
const timeout = setTimeout(() => { | ||
if (clearRef.current && typeof clearRef.current === 'function') { | ||
clearRef.current(); | ||
} | ||
clearRef.current = callback(); | ||
}, delay); | ||
|
||
return () => clearTimeout(timeout); | ||
}, | ||
// TODO: Should 'callback' really be omitted? | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[delay, ...deps], | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { useState, useEffect, useRef } from 'react'; | ||
|
||
type Timeout = ReturnType<typeof setTimeout>; | ||
|
||
/** | ||
* A debounced alternative to `useState`, API-equivalent except for the | ||
* addition of `delay` as the second parameter to the hook. | ||
* | ||
* Example: | ||
* ``` | ||
* const [value, setValue] = useDebouncedState(0, 200); | ||
* ``` | ||
* | ||
* When `setValue` is called, state does not change immediately. Instead | ||
* the hook waits (`delay` milliseconds) before flushing changes to state. | ||
* Any additional calls to `setValue` will reset the timer, such that state | ||
* is not updated until `delay` milliseconds have passed since the last | ||
* change. | ||
*/ | ||
export function useDebouncedState<T>( | ||
initialValue: T, | ||
delay: number, | ||
): [T, (state: T) => void] { | ||
const [debouncedValue, setDebouncedValue] = useState(initialValue); | ||
|
||
// Last value passed, will be assigned to debouncedValue after debounce. | ||
const pendingValueRef = useRef(initialValue); | ||
|
||
// Active timeout, if any. | ||
const setValueTimeoutRef = useRef<Timeout | null>(null); | ||
|
||
// Component-scoped `setValue` function, with debounce. | ||
const setValueRef = useRef((value: T) => { | ||
if (setValueTimeoutRef.current) { | ||
clearTimeout(setValueTimeoutRef.current); | ||
setValueTimeoutRef.current = null; | ||
} | ||
|
||
pendingValueRef.current = value; | ||
setValueTimeoutRef.current = setTimeout(() => { | ||
setDebouncedValue(pendingValueRef.current); | ||
setValueTimeoutRef.current = null; | ||
}, delay); | ||
}); | ||
|
||
// When component unmounts, cancel any active timeout. | ||
useEffect(() => { | ||
return () => { | ||
if (setValueTimeoutRef.current) { | ||
clearTimeout(setValueTimeoutRef.current); | ||
setValueTimeoutRef.current = null; | ||
} | ||
}; | ||
}, []); | ||
|
||
return [debouncedValue, setValueRef.current]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { | ||
addFilter, | ||
Filter, | ||
FilterType, | ||
getFilter, | ||
removeFilter, | ||
} from '@carto/api-client'; | ||
|
||
export type ToggleFilterProps = { | ||
column: string; | ||
owner: string; | ||
filters?: Record<string, Filter>; | ||
onChange?: (filters: Record<string, Filter>) => void; | ||
}; | ||
|
||
export function useToggleFilter({ | ||
column, | ||
owner, | ||
filters, | ||
onChange, | ||
}: ToggleFilterProps): (category: string) => void { | ||
const { IN } = FilterType; | ||
|
||
return function onToggleFilter(category: string): void { | ||
if (!filters || !onChange) return; | ||
|
||
const filter = getFilter(filters, { column, type: IN, owner }); | ||
|
||
let values: string[]; | ||
|
||
if (!filter) { | ||
values = [category]; | ||
} else if ((filter.values as string[]).includes(category)) { | ||
values = (filter.values as string[]).filter( | ||
(v: string) => v !== category, | ||
); | ||
} else { | ||
values = [...(filter.values as string[]), category]; | ||
} | ||
|
||
if (values.length > 0) { | ||
filters = addFilter(filters, { | ||
column, | ||
type: IN, | ||
owner, | ||
values: values, | ||
}); | ||
} else { | ||
filters = removeFilter(filters, { column, owner }); | ||
} | ||
|
||
onChange({ ...filters }); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.