Skip to content

Instantly share code, notes, and snippets.

View n8jadams's full-sized avatar

Nate Adams n8jadams

View GitHub Profile
@n8jadams
n8jadams / downloadTextAsFile.ts
Created August 19, 2022 21:09
Download plaintext as a file
async function downloadTextAsFile(text: string, filename: string): Promise<void> {
if (!filename.endsWith(".txt")) {
filename = `${filename}.txt`;
}
const blob = new Blob([text], { type: "text/plain" });
const href = await URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = href;
link.download = filename;
link.style.position = "absolute";
@n8jadams
n8jadams / downloadObjectAsJSONFile.ts
Last active August 19, 2022 21:11
Download a javascript object as a JSON file
async function downloadObjectAsJSONFile(object: any, filename: string): Promise<void> {
if(!filename.endsWith('.json')) {
filename = `${filename}.json`
}
const json = JSON.stringify(object)
const blob = new Blob([json],{ type:'application/json' })
const href = await URL.createObjectURL(blob)
const link = document.createElement('a')
link.href = href
link.download = filename
@n8jadams
n8jadams / webpack.config.js
Created October 28, 2022 17:39
Split up vendors into smaller chunks
// The best way to split up your npm deps into smaller chunks
module.exports = (env) => {
const config = {
// ... rest of config options
optimization: {
runtimeChunk: "single",
splitChunks: {
cacheGroups: {
vendors: {
@n8jadams
n8jadams / useBeforeClosingWindowPrompt.ts
Last active October 19, 2023 16:36
useBeforeClosingWindowPrompt - a hook that prompts user before they close the window when there are unsaved changes
import { useEffect } from "react";
/**
* A hook that prevents closing or refreshing if there are unsaved changes,
* offering the user a prompt to confirm before closing the window.
* @param promptBeforeClosingWindow A boolean indicating if we should prompt before closing
*/
export function useBeforeClosingWindowPrompt(promptBeforeClosingWindow: boolean) {
useEffect((): (() => void) => {
function handleBeforeUnload(e: BeforeUnloadEvent): void {