Skip to content

Instantly share code, notes, and snippets.

View Psvensso's full-sized avatar
🏠
Working from home

Per Svensson Psvensso

🏠
Working from home
  • Stockholm
View GitHub Profile
@Psvensso
Psvensso / createContext.ts
Created March 31, 2022 12:33
ReactComponentSnippets.code-snippets
//Save this file in some Workspace util package like "utils", it will be used alot.
import React from "react";
export interface ICreateContextOptions {
/**
* If `true`, React will throw if context is `null` or `undefined`
* In some cases, you might want to support nested context, so you can set it to `false`
*/
strict?: boolean;
/**
@Psvensso
Psvensso / .env
Created November 8, 2020 07:29
Create React App - Windows SSL Setup
HTTPS=true
HOST='dev.local.io'
SSL_CRT_FILE=certificate.crt
SSL_KEY_FILE=certificate.key
@Psvensso
Psvensso / ServiceWorkerContext.ts
Last active April 21, 2020 08:25
React Serviceworker
import React from "react";
export const ServiceWorkerContext = React.createContext<{
installedUpdate: boolean;
waitingWorker: ServiceWorker | null;
}>({
installedUpdate: false,
waitingWorker: null,
});
@Psvensso
Psvensso / readme.md
Created February 6, 2020 09:53
Create self signed cert for webpack dev server

SSL

The app uses SSL to work properly on mobile also on development. Webpack creates a .pem certificates but this is unstrusted in windows and you get the prompt on every resfresh.

One way to create a self signed certificate and force webpack to use locally created .pem cert and key.

  1. Install "mkcert" https://github.com/FiloSottile/mkcert#windows (if needed install via Chocolatey)

  2. Create a local folder somewhere outside the project, on the desktop for example

  3. Generate certificate https://github.com/FiloSottile/mkcert#mkcert (use your domain names instead of example.com as they are showing in their readme)

@Psvensso
Psvensso / getColor.ts
Created June 10, 2019 11:43
Generate color ranges
type GetColorArgs = { index: number; outOf: number; };
export function getColor({ index, outOf }: GetColorArgs) {
if (outOf < 1) {
outOf = 1;
}
return "hsl(" + (index * (360 / outOf) % 360) + ",100%,50%)";
}
@Psvensso
Psvensso / node-debug.md
Created June 7, 2019 05:56
Node debugger

node --inspect-brk ./node_modules/.bin/webpack (or whatever file) to start the debugging process. The process will halt at the first line and print a debugger URL. Copy the debugger URL into the Chrome Browser and the developers tools will initialize.

Then go to the JavaScript profiler tab and start profiling After you've pressed stop, the flame graph will be generated.

If you don't want to copy debugger URLs around, you can also use the NIM chrome extension. It discovers debuggable node processes automatically.

@Psvensso
Psvensso / switch-example.js
Last active September 26, 2018 06:46
Switch is not an If-Else - JavaScript
function _t(){
var result = "null";
switch ("A") {
case "A": {
result += "A";
}
case "B": {
result += "B";
}
@Psvensso
Psvensso / tslint.json
Created August 23, 2018 11:10
Basic ts-lint (ts-lint recommended with small tweaks)
{
"defaultSeverity": "warning",
"extends": [],
"linterOptions": {
"exclude": [
"Scripts/typings/**/*.ts"
]
},
"rules": {
"ban-types": {
@Psvensso
Psvensso / localStorage.ts
Last active September 10, 2020 14:52
React LocalStorage HOC that syncs component state to LocalStorage with whitelist
import * as React from 'react';
export function getLocalStorageItem<T>(key: string, ifNull: T) {
try {
var item = localStorage.getItem(key);
return item === null ? ifNull : JSON.parse(item);
} catch (e) {
localStorage.removeItem(key);
return ifNull;
}
}
@Psvensso
Psvensso / examples.rm
Created April 19, 2018 06:36
Node debugging with Chrome debugger
Install Node.js V8 --inspector Manager (NiM) as a Chrome extension (work great!)
or start debugger listener manually or with some other tool.
Then fire up your node process with the debugg flag.
node --inspect-brk ./somenodepath/webpack.js
Done.