Skip to content

Instantly share code, notes, and snippets.

View baptistemanson's full-sized avatar
🌼
Blooming

Baptiste Manson baptistemanson

🌼
Blooming
View GitHub Profile
@baptistemanson
baptistemanson / gist:d38c260b68acd64cbbb2cb764b00721d
Last active January 5, 2023 16:27
example hack for service worker
useEffect(() => {
if (!navigator.serviceWorker)
throw new Error("We only support browsers with service workers enabled");
// The trickery below appears to be necessary to register a compiled ServiceWorker with nextjs
// Webpack only compiles workers imported via the pattern `new Worker(new URL("script_to_be_compiled"))`
// So we need to use that exact expression and have it register a service worker instead
const RealWorker = window.Worker;
(
window as any
@baptistemanson
baptistemanson / exampleErrorHandling.ts
Created February 16, 2023 16:58
An example of how error handling could look like in TS
// Better error handling in TS
// Exceptions in TS have a few issues:
// - they can't be typechecked
// - they can't be easily documented (this may fail)
// - the "catch" code is usually not colocated to what may fail, adding cognitive load
// - it's hard to differentiate between different errors
// - they are costly in performance
// - they can propagate all the way from the bottom to the top without the intermediate layers to be warned it may fail
// - anything can be thrown
interface Jsonable {
[x: string]: string | number | boolean | Date | Jsonable | Jsonable[];
}
// errors that can be discriminated based on a type,
// contain a message
// and can have context extra info that are serializable to json.
interface IdeaError<ErrorType> {
type: ErrorType;
message: string;
extra: Jsonable;
@baptistemanson
baptistemanson / history.tsx
Created April 11, 2024 16:39
history provider for next
import { atomWithStorage, createJSONStorage } from "jotai/utils";
const storage = createJSONStorage(() => sessionStorage);
export const historyAtom = atomWithStorage(
"history",
[window.location.href],
storage
);
// will give you access to the history in the component you wish
// AOS
import { AppAccount, Collaborator } from "@prisma/client";
// list of all row type in the app
enum TableRowType {
Collab,
AppAcc,
}
type Spec<A, B> = {
// SOA
import { AppAccount, Collaborator } from "@prisma/client";
// list of all row type in the app
enum TableRowTypeString {
Collaborator,
AppAccount,
}
// list of all fields for app accounts