a tool for shared writing and social publishing

some random small tweaks

+25 -22
+1 -1
app/[doc_id]/Blocks.tsx
··· 9 9 }); 10 10 return ( 11 11 <button 12 - onClick={() => { 12 + onMouseDown={() => { 13 13 rep?.rep?.mutate.addBlock({ 14 14 parent: props.entityID, 15 15 position: generateKeyBetween(null, blocks[0]?.data.position || null),
+1 -1
replicache/clientMutationContext.ts
··· 41 41 (existingFact[0]?.data as Fact<typeof f.attribute>["data"]).value, 42 42 ); 43 43 const newUpdate = base64.toByteArray(f.data.value); 44 - const updateBytes = Y.mergeUpdatesV2([oldUpdate, newUpdate]); 44 + const updateBytes = Y.mergeUpdates([oldUpdate, newUpdate]); 45 45 data.value = base64.fromByteArray(updateBytes); 46 46 } 47 47 }
+22 -18
replicache/index.tsx
··· 1 1 "use client"; 2 - import { createContext, useContext, useEffect, useState } from "react"; 2 + import { createContext, useContext, useEffect, useMemo, useState } from "react"; 3 + import { useSubscribe } from "replicache-react"; 3 4 import { DeepReadonlyObject, Replicache, WriteTransaction } from "replicache"; 4 5 import { Pull } from "./pull"; 5 6 import { mutations } from "./mutations"; ··· 116 117 attribute: A, 117 118 ): CardinalityResult<A> { 118 119 let { rep, initialFacts } = useReplicache(); 119 - let fallbackData = initialFacts.filter( 120 - (f) => f.entity === entity && f.attribute === attribute, 120 + let fallbackData = useMemo( 121 + () => 122 + initialFacts.filter( 123 + (f) => f.entity === entity && f.attribute === attribute, 124 + ), 125 + [entity, attribute, initialFacts], 121 126 ); 122 - let [data, setData] = useState<DeepReadonlyObject<Fact<A>[]>>( 123 - fallbackData as DeepReadonlyObject<Fact<A>>[], 127 + let data = useSubscribe( 128 + rep, 129 + (tx) => { 130 + return tx 131 + .scan<Fact<A>>({ indexName: "eav", prefix: `${entity}-${attribute}` }) 132 + .toArray(); 133 + }, 134 + { 135 + default: null, 136 + dependencies: [entity, attribute], 137 + }, 124 138 ); 125 - useEffect(() => { 126 - if (!rep) return; 127 - return rep.subscribe( 128 - (tx) => { 129 - return tx 130 - .scan<Fact<A>>({ indexName: "eav", prefix: `${entity}-${attribute}` }) 131 - .toArray(); 132 - }, 133 - { onData: setData }, 134 - ); 135 - }, [entity, attribute, rep]); 139 + let d = data || (attribute === "card/block" ? fallbackData : []); 136 140 return Attributes[attribute].cardinality === "many" 137 - ? (data as CardinalityResult<A>) 138 - : (data[0] as CardinalityResult<A>); 141 + ? (d as CardinalityResult<A>) 142 + : (d[0] as CardinalityResult<A>); 139 143 }
-1
replicache/mutations.ts
··· 36 36 }; 37 37 38 38 const removeBlock: Mutation<{ blockEntity: string }> = async (args, ctx) => { 39 - console.log(args); 40 39 await ctx.deleteEntity(args.blockEntity); 41 40 }; 42 41
+1 -1
replicache/serverMutationContext.ts
··· 61 61 (existingFact[0]?.data as Fact<typeof f.attribute>["data"]).value, 62 62 ); 63 63 const newUpdate = base64.toByteArray(f.data.value); 64 - const updateBytes = Y.mergeUpdatesV2([oldUpdate, newUpdate]); 64 + const updateBytes = Y.mergeUpdates([oldUpdate, newUpdate]); 65 65 data.value = base64.fromByteArray(updateBytes); 66 66 } 67 67 }