Skip to content

Instantly share code, notes, and snippets.

View freddi301's full-sized avatar

Frederik Batuna freddi301

View GitHub Profile
@freddi301
freddi301 / configuration.json
Last active January 17, 2024 15:03
Liferay React Portlet TypeScript integration
// this files goes to feature/configuration.json
// it lets you setup configuration fields for a portlet
// it work with an adapted create-react-app for liferay https://help.liferay.com/hc/en-us/articles/360035467712-Adapting-Existing-Apps-to-Run-on-Liferay-DXP
{
"$schema": "https://raw.githubusercontent.com/liferay/liferay-js-toolkit/master/resources/schemas/configuration.schema.json",
"system": {
"category": "react-js-toolkit",
"name": "react-js-toolkit",
"fields": {
"fruit": {
@freddi301
freddi301 / setup
Last active January 24, 2024 10:49
MSW configuration for create-react-app
import { setupWorker } from 'msw/browser'
// https://mswjs.io/docs/best-practices/typescript
// also ensure there is .env or env.local file
// with PUBLIC_URL=index set to something otherwise chrome wont load the mock service worker
const worker = setupWorker(/* here goes handlers */);
if (process.env.NODE_ENV === "development") {
@freddi301
freddi301 / useValueWithTimeToLive.ts
Created May 5, 2023 08:30
useValueWithTimeToLive
function useValueWithTimeToLive<T>() {
const [state, setState] = React.useState<{ value: T | undefined; timeToLive: number | undefined }>({
value: undefined,
timeToLive: undefined,
});
const setValueWithTimeToLive = React.useCallback((value: T, timeToLive: number) => {
setState({ value, timeToLive });
}, []);
React.useEffect(() => {
if (state.timeToLive !== undefined) {
@freddi301
freddi301 / moveItem.tsx
Last active December 20, 2022 14:48
Move item in array for drag drop reordering
function moveItem<T>(fromIndex: number, toIndex: number, array: Array<T>): Array<T> {
if (fromIndex < toIndex) {
return [...array.slice(0, fromIndex), ...array.slice(fromIndex + 1, toIndex + 1), array[fromIndex], ...array.slice(toIndex + 1)];
}
if (fromIndex > toIndex) {
return [...array.slice(0, toIndex), array[fromIndex], ...array.slice(toIndex, fromIndex), ...array.slice(fromIndex + 1)];
}
return array;
}
@freddi301
freddi301 / Frederik-Batuna-CV.md
Last active August 4, 2022 08:48
Frederik Batuna Curriculum Vitae

Frederik Batuna

freddi301

Senior Front-End engineer with high focus on best-practices, productivity enhancement and cutting-edge technologies (mainly ReactJS related).

I have also experience as Full-Stack engineer with Back-End (nodejs, sql, mongo) and CI/CD (heroku, docker, github)

I like to experiment, learn and keep myself up to date with software development technologies.

@freddi301
freddi301 / incremental-behaviours.idr
Created February 7, 2022 15:19
Incremental Behaviours
module IncrementalBehaviors
-- esempio di una prova per induzione
-- è una funzione
-- dato qualsiasi x che è un booleano
-- ritorna una equality (che sarebbe una struttra dati, leggasi new Equality(x, not(not(x))))
-- il tipo di questa funzione è la nostra ipotesi
-- se si riesce a scrivere il corpo della funzione rispettando il tipo di ritorno
-- si ottiene la prova per induzione che l'ipotesi è corretta
example : (x : Bool) -> x = not (not x)
@freddi301
freddi301 / useWhyDidYouUpdate.ts
Created January 24, 2022 16:19
useWhyDidYouUpdate react hook
import React from "react"
export function useWhyDidYouUpdate<Props extends Record<string, any>>(label: string, props: Props) {
const previousProps = React.useRef<Props>();
React.useEffect(() => {
if (previousProps.current) {
const allKeys = Object.keys({ ...previousProps.current, ...props }) as Array<keyof Props>;
const changesObj: {[K in keyof Props]?: {from: Props[K], to: Props[K]}} = {};
for (const key of allKeys) {
if (previousProps.current[key] !== props[key]) {
@freddi301
freddi301 / TruncatedLine.tsx
Created October 20, 2021 15:40
truncates single line to parent width
import React from "react";
export function TruncatedLine({ text }: { text: string }) {
return (
<span style={{ position: "relative", display: "block" }}>
<span style={{ color: "transparent" }}>X</span>
<span
style={{
display: "block",
position: "absolute",
@freddi301
freddi301 / useClickAway.ts
Last active July 25, 2023 17:20
react hook that notifies when clicking outside an element
import React from "react";
export function useClickAway<E extends HTMLElement | null>(
ref: React.MutableRefObject<E>,
onClickAway: () => void
) {
const wasInsideRef = React.useRef(false);
React.useLayoutEffect(() => {
const onClick = (event: MouseEvent) => {
if (ref.current?.contains(event.target as Node)) {
@freddi301
freddi301 / DeBruijn.sc
Last active April 19, 2021 16:29
De Bruijn index reduction
object DeBrujin {
// https://en.wikipedia.org/wiki/De_Bruijn_index
sealed trait Term
case class Lambda(body: Term) extends Term
case class Variable(index: Int) extends Term
case class Application(left: Term, right: Term) extends Term
/** Modifies indices of the free variables by a given amount