| 1 |
import React, { createContext, useContext, useState, useCallback } from 'react'; |
| 2 |
|
| 3 |
const FAQContext = createContext(); |
| 4 |
|
| 5 |
export const FAQProvider = ({ children }) => { |
| 6 |
const [categories, setCategories] = useState([]); |
| 7 |
const [reFetchCategories, setReFetchCategories] = useState(() => () => {}); |
| 8 |
// Bumped on any FAQ activity (add / delete / duplicate / draft toggle) so |
| 9 |
// the stat cards re-derive even when the change is to an uncategorized FAQ |
| 10 |
// (which doesn't alter the categories query the cards otherwise watch). |
| 11 |
const [statsVersion, setStatsVersion] = useState(0); |
| 12 |
const bumpStats = useCallback(() => setStatsVersion((v) => v + 1), []); |
| 13 |
|
| 14 |
// One-shot "reveal this FAQ" signal, set after a FAQ is created |
| 15 |
// (?faq_action=create) or moved into a group via Edit. Shape: |
| 16 |
// { groupId, faqId } where groupId ("uncategorized" | term id) drives the |
| 17 |
// pagination jump + group expand, and faqId is the FAQ row that then |
| 18 |
// flashes. Mirrors the Doc Categories / Doc Tags panels' focusTermId. |
| 19 |
// `highlightGroupId` = group currently being expanded; `highlightFaqId` = |
| 20 |
// FAQ row currently flashing (consumed by the row once it renders). |
| 21 |
const [focusTarget, setFocusTarget] = useState(null); |
| 22 |
const [highlightGroupId, setHighlightGroupId] = useState(null); |
| 23 |
const [highlightFaqId, setHighlightFaqId] = useState(null); |
| 24 |
|
| 25 |
return ( |
| 26 |
<FAQContext.Provider value={{ categories, setCategories, reFetchCategories, setReFetchCategories, statsVersion, bumpStats, focusTarget, setFocusTarget, highlightGroupId, setHighlightGroupId, highlightFaqId, setHighlightFaqId }}> |
| 27 |
{children} |
| 28 |
</FAQContext.Provider> |
| 29 |
); |
| 30 |
}; |
| 31 |
|
| 32 |
export const useFAQ = () => { |
| 33 |
return useContext(FAQContext); |
| 34 |
}; |
| 35 |
|