Skip to content

Instantly share code, notes, and snippets.

@drumnickydrum
drumnickydrum / useGoBackBrowserSync.tsx
Last active December 30, 2022 21:13
[React: Browser/Router Sync] Sync the browser's back button with react-router history.goBack #react #browser #router
function useGoBackBrowserSync() {
useEffect(() => {
return history.listen((newLocation, action) => {
if (action === 'POP') {
history.goBack();
}
});
}, [history]);
}
@drumnickydrum
drumnickydrum / sleep.ts
Last active December 30, 2022 17:38
[TS: Sleep Function] A Promise that resolves after ms argument #typescript
const sleep = (ms: number) => new Promise(r => setTimeout(r, ms));
@drumnickydrum
drumnickydrum / reverse-percentages.txt
Last active December 30, 2022 17:31
[Reverse Percentages] Reverse calculate original amount given a percentage #math
1. Add or subtract the percentage given in the problem from 100% to determine what percentage we have.
2. Find 1% by dividing by the percentage found in the previous step.
3. Find 100% (original amount) by multiplying your answer in step 2 by 100.
<100% Example:
A shop is having a sale where all items are 30% off.
An item is on sale for $84.
What was the original price of the item?
100% - 30% = 70% of the original price
@drumnickydrum
drumnickydrum / browserOnly.ts
Last active December 30, 2022 21:12
[TS: Browser Only Function] Executes fn arg only if window is in global #typescript #browser
/**
* Safely execute browser only code.
*
* @param fn - The function to execute.
* @param fallback - Optional default return value.
*
*/
function browserOnly<T>(
fn: (window: Window, document: Document) => T,
fallback?: T
@drumnickydrum
drumnickydrum / sdkProxy.ts
Created December 30, 2022 21:03
[TS: SDK Proxy] Wrap a script-added SDK so it is usable in server and client contexts #typescript #browser #sdk #script
declare global {
interface Window {
someSDK: SomeSDKType | undefined;
}
}
/**
* Wrap a script-added SDK so it is usable in server and client contexts.
* This allows it to be freely used without errors related to undefined `window`
*/
@drumnickydrum
drumnickydrum / useContext.tsx
Created December 30, 2022 21:12
[React: Context Boilerplate] Sets up a hook to use context without the undefined initialization value #react #context
type FooContextType = { some: 'type' };
export const FooContext = React.createContext<FooContextType | undefined>(
undefined
);
export function FooProvider(props: { children: React.ReactNode }): JSX.Element {
const value = useFoo();
return (
<FooContext.Provider value={value}>{props.children}</FooContext.Provider>
@drumnickydrum
drumnickydrum / pojoEnum.ts
Last active December 30, 2022 21:26
[TS: enum -> pojo] Use a pojo instead of an enum #typescript #enum
const SomeEnum = {
SomeValue: 'SomeValue',
SomeOtherValue: 'SomeOtherValue'
} as const;
type SomeEnumKey = keyof typeof SomeEnum
@drumnickydrum
drumnickydrum / satisfies.ts
Created December 30, 2022 21:36
[TS: Satisfies Operator] Example of the satisfies operator in TypesScript #typescript
type City = CityName | CityCoordinates;
type CityName = 'New York' | 'Mumbai' | 'Lagos';
type CityCoordinates = {
x: number;
y: number;
};
type User = {
@drumnickydrum
drumnickydrum / csrf.txt
Created December 30, 2022 22:03
[Security: CSRF] Cross Site Request Forgery #csrf #security
https://www.youtube.com/watch?v=eWEgUcHPle0
___
https://dev.to/hemanth/explain-csrf-like-im-five
CSRF (Cross Site Request Forgery) is also known as Sea-Surf or Session Riding. It's is a form of trick that bad folks play on the browser in order to get it to do unexpected things in applications that you're already logged in.
For example, imagine you were logged into your Supercell game on the internet. You get an e-mail saying "Click here to get 500 gems for free!". Clicking on the text, on the contrary, will actually initiate a request to Supercell to transfer all your gems to the hacker's account. Now, along with the request, the browser always sends the cookies to Supercell as well. Supercell verifies if the cookies are valid (which they are because you just logged in!), Supercell will trust the browser and the request and doesn't know that this is not what you wanted. They will go ahead and execute this instruction thinking this is what you wanted to do.
@drumnickydrum
drumnickydrum / xss.txt
Created December 30, 2022 22:19
[Security: XSS] Cross-Site Scripting #security #xss
https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html