-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat(actions): add community export hook #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6e670c3
c9b4d92
5133a07
2d54a9c
4f65aaf
14b96b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,6 +50,8 @@ import type { | |
| UseBlockResult, | ||
| UseCreateCommunityOptions, | ||
| UseCreateCommunityResult, | ||
| UseExportCommunityOptions, | ||
| UseExportCommunityResult, | ||
| UsePublishVoteOptions, | ||
| UsePublishVoteResult, | ||
| UsePublishCommentEditOptions, | ||
|
|
@@ -66,6 +68,7 @@ import type { | |
| CommunityEdit, | ||
| Vote, | ||
| Community, | ||
| CommunityExport, | ||
| } from "../../types"; | ||
|
|
||
| type PublishChallengeAnswers = (challengeAnswers: string[]) => Promise<void>; | ||
|
|
@@ -711,3 +714,73 @@ export function useCreateCommunity(options?: UseCreateCommunityOptions): UseCrea | |
| [state, errors, createdCommunity, options, accountName], | ||
| ); | ||
| } | ||
|
|
||
| export function useExportCommunity(options?: UseExportCommunityOptions): UseExportCommunityResult { | ||
| assert( | ||
| !options || typeof options === "object", | ||
| `useExportCommunity options argument '${options}' not an object`, | ||
| ); | ||
| const { accountName, communityAddress, communityAddresses, onError, ...exportCommunityOptions } = | ||
| options || {}; | ||
| const accountsActions = useAccountsStore((state) => state.accountsActions); | ||
| const accountId = useAccountId(accountName); | ||
| const [errors, setErrors] = useState<Error[]>([]); | ||
| const [exportingState, setExportingState] = useState<string>(); | ||
| const [communityExports, setCommunityExports] = useState<CommunityExport[]>([]); | ||
| const targetCommunityAddresses = | ||
| communityAddresses || (communityAddress ? [communityAddress] : undefined); | ||
| const exportContextKey = JSON.stringify([ | ||
| accountId || null, | ||
| targetCommunityAddresses || null, | ||
| exportCommunityOptions.includePrivateKey === true, | ||
| exportCommunityOptions.exportPath, | ||
| ]); | ||
| const previousExportContextKeyRef = useRef(exportContextKey); | ||
| const exportContextVersionRef = useRef(0); | ||
| if (previousExportContextKeyRef.current !== exportContextKey) { | ||
| previousExportContextKeyRef.current = exportContextKey; | ||
| exportContextVersionRef.current += 1; | ||
| } | ||
| const [exportingContext, setExportingContext] = useState<{ | ||
| key: string; | ||
| version: number; | ||
| }>(); | ||
|
|
||
| let initialState = "initializing"; | ||
| if (accountId) { | ||
| initialState = "ready"; | ||
| } | ||
| const hasCurrentExportState = | ||
| accountId && | ||
| exportingContext?.key === exportContextKey && | ||
| exportingContext.version === exportContextVersionRef.current; | ||
|
|
||
| const exportCommunity = async () => { | ||
| try { | ||
| setExportingContext({ | ||
| key: exportContextKey, | ||
| version: exportContextVersionRef.current, | ||
| }); | ||
| setExportingState("exporting"); | ||
| const communityExports = await accountsActions.exportCommunity( | ||
| targetCommunityAddresses, | ||
| exportCommunityOptions, | ||
| accountName, | ||
| ); | ||
| setCommunityExports(communityExports); | ||
| setExportingState("succeeded"); | ||
| } catch (e: any) { | ||
| setExportingState("failed"); | ||
| setErrors((errors) => [...errors, e]); | ||
| onError?.(e); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Success keeps prior export errorMedium Severity Calling Reviewed by Cursor Bugbot for commit 14b96b0. Configure here. |
||
| }; | ||
|
|
||
| return { | ||
| communityExports: hasCurrentExportState ? communityExports : [], | ||
| exportCommunity, | ||
| state: hasCurrentExportState ? exportingState! : initialState, | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| error: hasCurrentExportState ? errors[errors.length - 1] : undefined, | ||
| errors: hasCurrentExportState ? errors : [], | ||
| }; | ||
| } | ||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stale export overwrites newer run
High Severity
exportCommunityappliessetCommunityExports,setExportingState, and error updates when the awaited store call finishes, without checking that the export still matches the activeexportingContext. A slower earlier export can finish after a later one starts and show the wrong communities as succeeded or failed.Reviewed by Cursor Bugbot for commit 14b96b0. Configure here.