2
0

perf(flow): ️ Better save management

This commit is contained in:
Baptiste Arnaud
2022-03-02 19:09:43 +01:00
parent a5a1fef597
commit 507fe4fa06

View File

@@ -155,11 +155,14 @@ export const TypebotContext = ({
}) })
} }
useAutoSave({ useAutoSave(
handler: saveTypebot, {
item: localTypebot, handler: saveTypebot,
debounceTimeout: autoSaveTimeout, item: localTypebot,
}) debounceTimeout: autoSaveTimeout,
},
[typebot, publishedTypebot, webhooks]
)
useEffect(() => { useEffect(() => {
Router.events.on('routeChangeStart', saveTypebot) Router.events.on('routeChangeStart', saveTypebot)
@@ -167,7 +170,7 @@ export const TypebotContext = ({
Router.events.off('routeChangeStart', saveTypebot) Router.events.off('routeChangeStart', saveTypebot)
} }
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, []) }, [typebot, publishedTypebot, webhooks])
const [isSavingLoading, setIsSavingLoading] = useState(false) const [isSavingLoading, setIsSavingLoading] = useState(false)
const [isPublishing, setIsPublishing] = useState(false) const [isPublishing, setIsPublishing] = useState(false)
@@ -211,7 +214,7 @@ export const TypebotContext = ({
perform: () => saveTypebot(), perform: () => saveTypebot(),
}, },
], ],
[] [typebot, publishedTypebot, webhooks]
) )
useRegisterActions( useRegisterActions(
@@ -366,16 +369,19 @@ export const useFetchedTypebot = ({
} }
} }
const useAutoSave = <T,>({ const useAutoSave = <T,>(
handler, {
item, handler,
debounceTimeout, item,
}: { debounceTimeout,
// eslint-disable-next-line @typescript-eslint/no-explicit-any }: {
handler: (item?: T) => Promise<any> // eslint-disable-next-line @typescript-eslint/no-explicit-any
item?: T handler: (item?: T) => Promise<any>
debounceTimeout: number item?: T
}) => { debounceTimeout: number
},
dependencies: unknown[]
) => {
const [debouncedItem] = useDebounce(item, debounceTimeout) const [debouncedItem] = useDebounce(item, debounceTimeout)
useEffect(() => { useEffect(() => {
const save = () => handler(item) const save = () => handler(item)
@@ -384,7 +390,7 @@ const useAutoSave = <T,>({
document.removeEventListener('visibilitychange', save) document.removeEventListener('visibilitychange', save)
} }
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, []) }, dependencies)
return useEffect(() => { return useEffect(() => {
handler(item) handler(item)
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps