Skip to content

Instantly share code, notes, and snippets.

View fmartins-andre's full-sized avatar

André Martins fmartins-andre

View GitHub Profile
@fmartins-andre
fmartins-andre / safe-promise.ts
Created April 17, 2025 20:35
Factory function to create tanstack react lazy query hooks
/**
* execute a callback and return an result/error array
*
* @param {callback} function to be executed
*/
export const safePromise = async <TData, TError>(
callback: () => Promise<TData>
): Promise<[false, TError, undefined] | [true, undefined, TData]> => {
try {
const response: TData = await callback()
@fmartins-andre
fmartins-andre / utility-types.d.ts
Created February 6, 2025 16:58
Useful types to typescript projects
declare type StringfyKeys<T> =
T extends Record<string, unknown> ? `${keyof T}` : never
declare type NonNullableFields<T> = {
[P in keyof T]: NonNullable<T[P]>
}
declare type NonNullableField<T, K extends keyof T> = T &
NonNullableFields<Pick<T, K>>
@fmartins-andre
fmartins-andre / patch-deep.ts
Created February 6, 2025 16:52
patchDeep: A function to deeply patch an object
declare type DeepPartial<T> = {
[K in keyof T]?: DeepPartial<T[K]> | undefined
}
const isObject = (obj: unknown) => obj && typeof obj === 'object'
/**
* Patch source object with other object.
* Only the source object keys are kept!
* Patch undefined values are ignored
@fmartins-andre
fmartins-andre / git-remove-local-branches.sh
Last active December 20, 2024 13:19
Remove local branches that were merged
#!/bin/bash
echo 'Removing origins...'
git remote prune origin
BRANCHES=`git branch -vv | grep 'origin/.*: gone]' | awk '{print $1}'`
if [[ `echo -e "$BRANCHES" | wc -w` -gt 0 ]]; then
echo 'Removing branches:'
echo $BRANCHES
@fmartins-andre
fmartins-andre / toggle-dark-mode.sh
Created July 18, 2024 00:49
Toggle between Gnome light and dark modes
#!/bin/bash
lightMode="'prefer-light'"
darkMode="'prefer-dark'"
ligthTheme="'Adwaita'"
darkTheme="'Adwaita-dark'"
function toggleMode {
@fmartins-andre
fmartins-andre / create-unique-refs-elements-map.md
Created August 20, 2023 23:09
How to create unique refs for elements being rendered via array.map()

You could define a ref, and then inside the object, make multiple subvalues based on the key of the child:

const ref = useRef({})

{array.map((item)=><div key={item.id} ref={ref.current[item.id] ??= { current: null }}>{item.id}</div>)}

Example: https://playcode.io/1096173

Another approach is callback refs:

@fmartins-andre
fmartins-andre / how-to-differ-objects-in-js.md
Created July 26, 2023 14:59
How to differ objects in JavaScript

To check what the real type of and variable, run this command:

  Object.prototype.toString.call(myVar)

It'll return something like [object Object]. The second word is the real type of the variable.

Some examples:

@fmartins-andre
fmartins-andre / useDebouncedFn.ts
Created October 24, 2022 16:25
Debounce a React.js function
import { DebouncedFunc } from "lodash"
import debounce from "lodash.debounce"
import { useMemo } from "react"
export default function useDebouncedFn<T extends (...args: any[]) => void>(fn: T, wait?: number) {
return useMemo(() =>(
debounce((...args:any[]) => {fn(...args)}, wait ?? 300) as DebouncedFunc<T>
), [fn, wait])
}
@fmartins-andre
fmartins-andre / wayland-chrome-screensharing.md
Created May 11, 2022 11:31
chrome shows a black screen on screen sharing on wayland

How to enable wayland screen sharing on chrome browsers

Firefox (84+) supports this method by default, while on Chromium (73+) one needs to enable WebRTC PipeWire support by setting the corresponding (experimental) flag at the URL chrome://flags/#enable-webrtc-pipewire-capturer.

This requires xdg-desktop-portal and one of its backends to be installed. The available backends are:

  • xdg-desktop-portal-gnome for GNOME.
  • xdg-desktop-portal-kde for KDE.
  • xdg-desktop-portal-wlr for wlroots-based Wayland compositors (e.g. Sway, dwl)
@fmartins-andre
fmartins-andre / .gitconfig
Last active June 6, 2024 20:51
Git global config file
[user]
signingkey = CASDF9879ASDF
name = Me
email = [email protected]
[gpg]
program = gpg
[core]
editor = vim
[diff]
tool = meld