(store: Store<CommentStore>, setStore: SetStoreFunction<CommentStore>)
| 80 | } |
| 81 | |
| 82 | function createCommentSessionState(store: Store<CommentStore>, setStore: SetStoreFunction<CommentStore>) { |
| 83 | const [state, setState] = createStore({ |
| 84 | focus: null as CommentFocus | null, |
| 85 | active: null as CommentFocus | null, |
| 86 | }) |
| 87 | |
| 88 | const all = () => aggregate(store.comments) |
| 89 | |
| 90 | const setRef = ( |
| 91 | key: "focus" | "active", |
| 92 | value: CommentFocus | null | ((value: CommentFocus | null) => CommentFocus | null), |
| 93 | ) => setState(key, value) |
| 94 | |
| 95 | const setFocus = (value: CommentFocus | null | ((value: CommentFocus | null) => CommentFocus | null)) => |
| 96 | setRef("focus", value) |
| 97 | |
| 98 | const setActive = (value: CommentFocus | null | ((value: CommentFocus | null) => CommentFocus | null)) => |
| 99 | setRef("active", value) |
| 100 | |
| 101 | const list = (file: string) => store.comments[file] ?? [] |
| 102 | |
| 103 | const add = (input: Omit<LineComment, "id" | "time">) => { |
| 104 | const next: LineComment = { |
| 105 | id: uuid(), |
| 106 | time: Date.now(), |
| 107 | ...input, |
| 108 | selection: cloneSelection(input.selection), |
| 109 | } |
| 110 | |
| 111 | batch(() => { |
| 112 | setStore("comments", input.file, (items) => [...(items ?? []), next]) |
| 113 | setFocus({ file: input.file, id: next.id }) |
| 114 | }) |
| 115 | |
| 116 | return next |
| 117 | } |
| 118 | |
| 119 | const remove = (file: string, id: string) => { |
| 120 | batch(() => { |
| 121 | setStore("comments", file, (items) => (items ?? []).filter((item) => item.id !== id)) |
| 122 | setFocus((current) => (current?.file === file && current.id === id ? null : current)) |
| 123 | }) |
| 124 | } |
| 125 | |
| 126 | const update = (file: string, id: string, comment: string) => { |
| 127 | setStore("comments", file, (items) => |
| 128 | (items ?? []).map((item) => { |
| 129 | if (item.id !== id) return item |
| 130 | return { ...item, comment } |
| 131 | }), |
| 132 | ) |
| 133 | } |
| 134 | |
| 135 | const replace = (comments: LineComment[]) => { |
| 136 | batch(() => { |
| 137 | setStore("comments", reconcile(group(comments))) |
| 138 | setFocus(null) |
| 139 | setActive(null) |
no test coverage detected