Skip to content

Instantly share code, notes, and snippets.

View srcrip's full-sized avatar
🔮
typescript is best script

Andrew Stewart srcrip

🔮
typescript is best script
View GitHub Profile
@srcrip
srcrip / example.html.heex
Last active May 3, 2024 14:30
Some helper functions for making nested forms in LiveView
<.form for={@form} class="max-w-xs">
<NestedFormComponents.inputs_for_embeds
:let={embed_form}
parent_form={@form}
field={@form[:items]}
id={"#{@form[:items].id}_embeds"}
sort_param={:items_sort}
drop_param={:items_drop}
>
<div class="grow">
import 'phoenix_html'
import { Socket } from 'phoenix'
import { LiveSocket } from 'phoenix_live_view'
const hooks = {}
// you can get this from the admin panel
const formId = '<insert here>'
const apiKey = '<insert here>'
const fetchOpts = {
@srcrip
srcrip / error_html.ex
Created February 15, 2024 19:42
Custom Phoenix error pages
# goes in lib/your_app_web/controllers/error_html.ex
defmodule ExampleWeb.ErrorHTML do
use ExampleWeb, :html
def render("404.html" = template, assigns) do
assigns =
assigns
|> Map.merge(%{__changed__: %{}})
|> assign(:message, Phoenix.Controller.status_message_from_template(template))
@srcrip
srcrip / quick-relative-dates.ts
Created February 24, 2022 04:58
Doing 'x hours ago' conversions quickly.
const units = [
['year', 31536000],
['month', 2592000],
['day', 86400],
['hour', 3600],
['minute', 60],
['second', 1]
]
const duration = (timeAgoInSeconds: number) => {
@srcrip
srcrip / .eslintrc.cjs
Last active August 30, 2023 21:36
Standard ESLint Setup for Svelte
module.exports = {
env: {
browser: true,
es2022: true
},
extends: [
'standard',
'eslint:recommended'
],
parser: '@typescript-eslint/parser',
@srcrip
srcrip / load-script.js
Created March 2, 2021 20:26
Create and wait on a script tag
function loadScript (src) {
return new Promise((resolve, reject) => {
const script = document.createElement('script')
script.type = 'text/javascript'
script.src = src
script.addEventListener('load', () => resolve(script), false)
script.addEventListener('error', () => reject(script), false)
document.body.appendChild(script)
@srcrip
srcrip / context.js
Created February 16, 2021 04:45
Simple example of using a regular React context, and one with a provider
import React, { createContext, useState, useContext } from 'react'
import update from 'immutability-helper'
import './App.css'
const CounterContext = createContext()
const CounterProvider = ({ children }) => {
const [state, setState] = useState({
resources: [
{ name: 'stone', amount: 0 },
@srcrip
srcrip / bookmarks.js
Created February 13, 2021 17:36
Flatten the browser bookmarks tree from an extension (Chrome or Firefox)
import browser from 'webextension-polyfill'
// Flatten a tree with 'children' properties recursively
const flattenTree = data => {
return data.reduce((r, { children, ...rest }) => {
if (rest.url) r.push(rest)
if (children) r.push(...flattenTree(children))
return r
}, [])
}
@srcrip
srcrip / to_indexed_hash.rb
Created February 1, 2021 22:45
Pointless method for turning arrays into hashes based on their positional index
# Converts an array like:
# ["M", nil, "G", nil, "G"]
# to:
# {0=>"M", 1=>nil, 2=>"G", 3=>nil, 4=>"G"}
def to_indexed_hash(arr)
arr.each_with_object({}).with_index { |(el, acc), index| acc[index] = el }
end
# Is this pointless? Probably. I thought it was interesting though.
@srcrip
srcrip / PKGBUILD
Created September 12, 2020 02:19
Insomnia 2020.4.0 PKGBUILD
pkgname=insomnia-designer
pkgver=2020.4.0
pkgrel=1
pkgdesc="The Collaborative API Design Tool for designing and managing OpenAPI specs."
url="https://github.com/Kong/insomnia"
arch=('x86_64')
license=('MIT')
depends=()
makedepends=()
provides=($pkgname)