Skip to content

Instantly share code, notes, and snippets.

View tomfa's full-sized avatar

Tomas Fagerbekk tomfa

View GitHub Profile
@tomfa
tomfa / NextJSComponentWithQueryStringState.tsx
Created May 20, 2021 22:24
NextJS string state in URL query param
import useQueryString from './useQueryString'
export const Component = () => {
const [cat, setCat] = useQueryString<string>({
key: 'cat',
defaultValue: 'Robert Paulson',
});
return <h1>His name is <strong>{cat}</strong></h1>
@tomfa
tomfa / client.ts
Last active April 7, 2023 22:15
graphql-request with automatic parsing of DateTime
import { GraphQLClient } from 'graphql-request';
import { gqlSerializer } from './serializer.ts'
const gqlClient = new GraphQLClient(url, {
credentials: 'include',
mode: 'cors',
jsonSerializer: gqlSerializer,
});
@tomfa
tomfa / database.tf
Created September 16, 2020 21:30
Draft EC2 instance with RDS using Terraform.
# TODO: THIS FILE MIGHT HAVE TO GO TO OWN FOLDER database/main.tf
variable "security_group_ids" {
description = "Ids of VPC Security groups"
type = list(string)
}
variable "database_password" {
description = "Enter a new root SQL password. This variable is ignored if the DB is already set up."
type = string
@tomfa
tomfa / generate-story.js
Created March 3, 2023 21:56
Script to generate basic storybook stories for
/**
* Usage node generate-story.js [path]
*
* Example (generate stories for all components in folder "components":
* node generate-story.js ./src/components
*/
const fs = require('fs');
const SUPPORTED_FILE_EXT = ['tsx', 'jsx'];
const EXCLUDED_FILES = [];
@tomfa
tomfa / handler.py
Last active February 14, 2023 15:08
AWS Lambda: Python store to S3
# This file is your Lambda function
import json
import boto3
def save_to_bucket(event, context):
AWS_BUCKET_NAME = 'my-bucket-name'
s3 = boto3.resource('s3')
bucket = s3.Bucket(AWS_BUCKET_NAME)
path = 'my-path-name.txt'
@tomfa
tomfa / csvDownload.ts
Created February 14, 2023 07:46
React hook for downloading csv files (typescript)
import { useCallback } from 'react';
export function useCsvDownload(
data: Record<string, any>[],
title = 'data.csv',
) {
return useCallback(() => {
const headerKeys = data
.map((d) => Object.keys(d))
.reduce((a, b) => {
@tomfa
tomfa / custom_js_in_footer.js
Last active February 8, 2023 08:08
Forwarding UTM data in Webflow, when using Weglot as translation tool
@tomfa
tomfa / index.html
Created February 6, 2023 20:47
A link, but a form that'll forward utm values
<html>
<form action="https://google.com/search" method="get">
<input type="hidden" name="utm_source" />
<input type="hidden" name="utm_medium" />
<input type="hidden" name="utm_campaign" />
<input type="hidden" name="utm_term" />
<input type="hidden" name="utm_content" />
<input type="submit" value="I pretend to be link" />
</form>
<script>
@tomfa
tomfa / types.ts
Last active January 30, 2023 21:05
LogStructure.ts
type ApplicationLog = {
level: 'debug' | 'info' | 'warning' | 'danger' | 'critical',
message: string,
createdAt: number,
userId?: string,
// a business metric, e.g. SIGNUP
action?: string,
monetaryValue?: number,
requestId?: string,
@tomfa
tomfa / handler.py
Last active December 26, 2022 13:51
Python: AWS lambda receiving form file (with serverless.yml)
# This file is your Lambda function
import base64
import json
import boto3
def save_to_bucket(event, context):
AWS_BUCKET_NAME = 'my-bucket-name'
s3 = boto3.resource('s3')