Skip to content
Myr

read-marks.ts

This file

Path

apps/web/src/features/finish/read-marks.ts

107 lines · 2.7 KiB · TypeScript

Last changed by

Merge redesign into mainoscabrielbefore adoptioncaedd21

History of this file

Take it

Merge build/1224-decider-pagesoscabrielbefore adoptiond22b7a2

line 1why, one step back
1/**

Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72

lines 2–4why, one step back
2 * Kept in this browser alone, never sent anywhere. Storage may be missing, full
3 * or refused (a private window), so every read and write is tried and a refused4 * one keeps nothing.

Merge build/1224-decider-pagesoscabrielbefore adoptiond22b7a2

lines 5–8why, one step back
5 */
6import { Option, Result, Schema } from "effect";7import { useCallback, useEffect, useState } from "react";8

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 9–10why, one step back
9import { browserStorage } from "../shared/storage.ts";
10

Merge build/1224-decider-pagesoscabrielbefore adoptiond22b7a2

lines 11–107why, one step back
11const Kept = Schema.Struct({
12 head: Schema.String,13 read: Schema.Record(Schema.String, Schema.String),14});1516const decodeKept = Schema.decodeUnknownOption(Schema.fromJsonString(Kept));1718export const marksKey = (project: string, decision: string) =>19 `finish.read:${project}/${decision}`;2021const CHANGED = "finish-read-marks";2223export interface MarkedFile {24 readonly path: string;25 readonly patchId: string;26}2728export const readMarks = (29 storage: Storage | undefined,30 key: string,31 head: string,32 files: readonly MarkedFile[]33): ReadonlySet<string> => {34 const kept = Result.getOrElse(35 Result.try(() => storage?.getItem(key) ?? null),36 () => null37 );3839 return Option.match(Option.flatMap(Option.fromNullishOr(kept), decodeKept), {40 onNone: () => new Set(),41 onSome: ({ head: at, read }) =>42 at === head43 ? new Set(44 files.flatMap(({ patchId, path }) =>45 read[path] === patchId ? [path] : []46 )47 )48 : new Set(),49 });50};5152export const writeMark = (53 storage: Storage | undefined,54 key: string,55 head: string,56 file: MarkedFile,57 read: boolean58): void => {59 const kept = Result.getOrElse(60 Result.try(() => storage?.getItem(key) ?? null),61 () => null62 );6364 const current = Option.getOrUndefined(65 Option.filter(66 Option.flatMap(Option.fromNullishOr(kept), decodeKept),67 (marks) => marks.head === head68 )69 );7071 const { [file.path]: _was, ...others } = current?.read ?? {};72 const next = read ? { ...others, [file.path]: file.patchId } : others;7374 Result.try(() => storage?.setItem(key, JSON.stringify({ head, read: next })));75};7677export const useReadMarks = (78 key: string,79 head: string,80 files: readonly MarkedFile[],81 storage: () => Storage | undefined = browserStorage82) => {83 const [read, setRead] = useState<ReadonlySet<string>>(new Set());8485 useEffect(() => {86 const load = () => {87 setRead(readMarks(storage(), key, head, files));88 };8990 load();91 globalThis.addEventListener(CHANGED, load);9293 return () => {94 globalThis.removeEventListener(CHANGED, load);95 };96 }, [files, head, key, storage]);9798 const mark = useCallback(99 (file: MarkedFile, on: boolean) => {100 writeMark(storage(), key, head, file, on);101 globalThis.dispatchEvent(new Event(CHANGED));102 },103 [head, key, storage]104 );105106 return { mark, read };107};