(params: UseGroupCommitsParams)
| 124 | } |
| 125 | |
| 126 | export function useGroupCommits(params: UseGroupCommitsParams) { |
| 127 | const { activeCompPath, showToast, projectIdRef } = params; |
| 128 | |
| 129 | const groupSelection = useCallback( |
| 130 | async (members: DomEditSelection[]): Promise<string | null> => { |
| 131 | const pid = projectIdRef.current; |
| 132 | if (!pid || members.length === 0) return null; |
| 133 | |
| 134 | // All members must live in the same source file — the wrapper is one node |
| 135 | // in one document. (Cross-file grouping is out of scope.) |
| 136 | const targetPath = members[0].sourceFile || activeCompPath || "index.html"; |
| 137 | if (members.some((m) => (m.sourceFile || activeCompPath || "index.html") !== targetPath)) { |
| 138 | showToast("Can't group elements from different files", "error"); |
| 139 | return null; |
| 140 | } |
| 141 | |
| 142 | // Auto-name "Group N" by the count of existing groups in the document. |
| 143 | const doc = members[0].element.ownerDocument; |
| 144 | const groupId = `Group ${doc.querySelectorAll("[data-hf-group]").length + 1}`; |
| 145 | const { bbox, targets, rebases } = computeGroupGeometry(members); |
| 146 | |
| 147 | try { |
| 148 | const data = await commitStructuralMutation( |
| 149 | pid, |
| 150 | targetPath, |
| 151 | "wrap-elements", |
| 152 | { targets, groupId, bbox, rebases }, |
| 153 | "Group elements", |
| 154 | params, |
| 155 | ); |
| 156 | return data.groupId ?? groupId; |
| 157 | } catch (error) { |
| 158 | showToast(error instanceof Error ? error.message : "Failed to group elements", "error"); |
| 159 | return null; |
| 160 | } |
| 161 | }, |
| 162 | [activeCompPath, projectIdRef, showToast, params], |
| 163 | ); |
| 164 | |
| 165 | const ungroupSelection = useCallback( |
| 166 | async (group: DomEditSelection): Promise<void> => { |
| 167 | const pid = projectIdRef.current; |
| 168 | if (!pid) return; |
| 169 | const targetPath = group.sourceFile || activeCompPath || "index.html"; |
| 170 | |
| 171 | try { |
| 172 | await commitStructuralMutation( |
| 173 | pid, |
| 174 | targetPath, |
| 175 | "unwrap-elements", |
| 176 | { target: buildDomEditPatchTarget(group) }, |
| 177 | "Ungroup elements", |
| 178 | params, |
| 179 | ); |
| 180 | } catch (error) { |
| 181 | showToast(error instanceof Error ? error.message : "Failed to ungroup elements", "error"); |
| 182 | } |
| 183 | }, |
no test coverage detected