Skip to content

Instantly share code, notes, and snippets.

@ryanto
ryanto / page.tsx
Created August 9, 2023 17:01
<Await> component
import { Markdown } from "@/app/components/markdown";
import { getComments, getPost } from "@/lib/db";
import { Suspense } from "react";
export default async function PostPage({
params,
}: {
params: { postId: string };
}) {
let post = await getPost(params.postId);
module.exports.addDefaultImport = function addImport(tree, mod, name) {
tree.children.unshift({
type: 'import',
value: `import _${name} from '${mod}'`,
});
return `_${name}`;
};
function useDebounced(value, timeout) {
let [debouncedValue, setDebouncedValue] = useState(value)
useEffect(() => {
let timeoutId = setTimeout(() => {
setDebouncedValue(value)
}, timeout)
return () => {
clearTimeout(timeoutId)
@ryanto
ryanto / use-depends-on.js
Created May 11, 2020 20:00
Depends on. Think of it like a useEffect where you unapologetically use the dependencies list to control when the effect should run.
import { usePrevious } from "./use-previous";
import { useRef, useEffect } from "react";
export const useDependsOn = (
value,
functionToRun,
{ initial = true } = {}
) => {
let serialized = JSON.stringify(value);
let previous = usePrevious(serialized);
@ryanto
ryanto / controllers.application.js
Created July 11, 2019 00:43
mut action boolean
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
actions: {
update(key, value) {
console.log(`updating ${key} to ${value}`);
this.model.set(`variables.${key}`, value);
}
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
@ryanto
ryanto / fullscreen.js
Created May 22, 2018 14:31
Fullscreen test helper
// tests/helpers/fullscreen.js
import $ from 'jquery';
import { registerAsyncHelper } from '@ember/test';
export default registerAsyncHelper('fullscreen', function() {
window.QUnit.config.testTimeout = 9999999;
$('#ember-testing-container').css({
background: 'initial',
@ryanto
ryanto / components.my-component.js
Created February 19, 2018 22:21
typeof DS.attr('number')
import Ember from 'ember';
export default Ember.Component.extend({
store: Ember.inject.service(),
didInsertElement() {
let store = this.get('store');
let model = store.createRecord('my-model');
this.set('model', model);
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'x'
});