| 35 | } |
| 36 | |
| 37 | function createSectionStore(sections: Array<Section>) { |
| 38 | return createStore<SectionState>()((set) => ({ |
| 39 | sections, |
| 40 | visibleSections: [], |
| 41 | setVisibleSections: (visibleSections) => |
| 42 | set((state) => |
| 43 | state.visibleSections.join() === visibleSections.join() |
| 44 | ? {} |
| 45 | : { visibleSections }, |
| 46 | ), |
| 47 | registerHeading: ({ id, ref, offsetRem }) => |
| 48 | set((state) => { |
| 49 | return { |
| 50 | sections: state.sections.map((section) => { |
| 51 | if (section.id === id) { |
| 52 | return { |
| 53 | ...section, |
| 54 | headingRef: ref, |
| 55 | offsetRem, |
| 56 | } |
| 57 | } |
| 58 | return section |
| 59 | }), |
| 60 | } |
| 61 | }), |
| 62 | })) |
| 63 | } |
| 64 | |
| 65 | function useVisibleSections(sectionStore: StoreApi<SectionState>) { |
| 66 | let setVisibleSections = useStore(sectionStore, (s) => s.setVisibleSections) |