diff --git a/.changeset/perfect-mails-clap.md b/.changeset/perfect-mails-clap.md new file mode 100644 index 0000000..24ccf5f --- /dev/null +++ b/.changeset/perfect-mails-clap.md @@ -0,0 +1,5 @@ +--- +'gov4git-desktop-app': patch +--- + +Refactor data sync diff --git a/src/electron/services/CommunityService.ts b/src/electron/services/CommunityService.ts index 9d584e4..680a206 100644 --- a/src/electron/services/CommunityService.ts +++ b/src/electron/services/CommunityService.ts @@ -253,12 +253,14 @@ ${user.memberPublicBranch}` } } - public insertCommunity = async (projectUrl: string): Promise => { + public insertCommunity = async ( + projectUrl: string, + ): Promise<[string, string[]]> => { const user = await this.userService.getUser() if (user == null) - return ['Unauthorized. Please log in to join a new community'] + return ['', ['Unauthorized. Please log in to join a new community']] if (projectUrl === '') { - return [`Community URL is a required field.`] + return ['', [`Community URL is a required field.`]] } const projectRepoUrl = projectUrl @@ -279,13 +281,13 @@ ${user.memberPublicBranch}` projectRepoUrl, ) if (projectRepoError != null) { - return [projectRepoError] + return ['', [projectRepoError]] } const [communityMainBranch, communityRepoError] = await this.getDefaultBranchFromUrl(user, communityUrl) if (communityRepoError != null) { - return [communityRepoError] + return ['', [communityRepoError]] } const configPath = resolve( @@ -312,25 +314,17 @@ ${user.memberPublicBranch}` }) .returning() )[0]! - const communityCount = await this.db - .select({ - count: sql`count(*)`, - }) - .from(communities) - if (communityCount.length === 0 || communityCount[0]?.count === 1) { - await this.selectCommunity(community.url) - } const initializationResults = await this.govService.initId() if (!initializationResults.ok) { - return [initializationResults.error] + return ['', [initializationResults.error]] } const syncedCommunity = await this.syncCommunity(user, currentCommunity) await this.requestToJoin(user, syncedCommunity) - return [] + return [syncedCommunity.url, []] } public deployCommunity = async ({ @@ -358,7 +352,15 @@ ${user.memberPublicBranch}` const projectUrl = `https://github.com/${org}/${repo}` - const errors = await this.insertCommunity(projectUrl) + const [communityUrl, errors] = await this.insertCommunity(projectUrl) + const communityCount = await this.db + .select({ + count: sql`count(*)`, + }) + .from(communities) + if (communityCount.length === 0 || communityCount[0]?.count === 1) { + await this.selectCommunity(communityUrl) + } if (errors.length) { throw new Error(JSON.stringify(errors, undefined, 2)) diff --git a/src/electron/services/MotionService.ts b/src/electron/services/MotionService.ts index 7e9699e..953fcb9 100644 --- a/src/electron/services/MotionService.ts +++ b/src/electron/services/MotionService.ts @@ -145,7 +145,7 @@ export class MotionService extends AbstractMotionService { return null } - private loadMotions = serialAsync(async () => { + private loadMotions = async () => { const [user, community] = await Promise.all([ this.userService.getUser(), this.communityService.getCommunity(), @@ -229,7 +229,7 @@ export class MotionService extends AbstractMotionService { set: motion, }) } - }) + } public getMotions = async ( options?: MotionSearch, @@ -264,6 +264,9 @@ export class MotionService extends AbstractMotionService { )[0] if (motionsCounts == null || motionsCounts.count === 0 || skipCache) { + await this.db + .delete(motions) + .where(eq(motions.communityUrl, community.url)) await this.loadMotions() } diff --git a/src/renderer/src/pages/layout/DataLoader.tsx b/src/renderer/src/pages/layout/DataLoader.tsx index 401ac8d..443f2f8 100644 --- a/src/renderer/src/pages/layout/DataLoader.tsx +++ b/src/renderer/src/pages/layout/DataLoader.tsx @@ -4,8 +4,6 @@ import { serialAsync } from '~/shared' import { useDataStore } from '../../store/store.js' -let firstMount = true - export const DataLoader: FC = function DataLoader() { const getUser = useDataStore((s) => s.userInfo.fetchUser) const getCommunities = useDataStore((s) => s.communityInfo.fetchCommunities) @@ -20,12 +18,6 @@ export const DataLoader: FC = function DataLoader() { await fetchMotions(motionSearchArgs, false, false, () => shouldUpdate) } void run() - if (firstMount) { - setTimeout(async () => { - await fetchMotions(motionSearchArgs, true, true, () => shouldUpdate) - firstMount = false - }, 1000) - } return () => { shouldUpdate = false diff --git a/src/renderer/src/pages/polls/motions/Motions.tsx b/src/renderer/src/pages/polls/motions/Motions.tsx index e8aa72c..cc3da05 100644 --- a/src/renderer/src/pages/polls/motions/Motions.tsx +++ b/src/renderer/src/pages/polls/motions/Motions.tsx @@ -40,14 +40,8 @@ export const Motions: FC = function Motions({ community?.name ?? '', ) const motionsLoading = useDataStore((s) => s.motionInfo.loading) - const userLoading = useDataStore((s) => s.userInfo.loading) - const communityLoading = useDataStore((s) => s.communityInfo.loading) const navigate = useNavigate() - const loading = useMemo(() => { - return motionsLoading || userLoading || communityLoading - }, [motionsLoading, userLoading, communityLoading]) - useEffect(() => { setInternalCommunityName(community?.name ?? '') }, [community, setInternalCommunityName]) @@ -138,7 +132,7 @@ export const Motions: FC = function Motions({ )} - + {(motions == null || motions.length === 0) && (

No matching ballots to display at this time.

diff --git a/src/renderer/src/store/communityJoinStore.ts b/src/renderer/src/store/communityJoinStore.ts index 25a3b7f..6a7f433 100644 --- a/src/renderer/src/store/communityJoinStore.ts +++ b/src/renderer/src/store/communityJoinStore.ts @@ -13,10 +13,24 @@ export const createCommunityJoinStore: StateCreator< > = (set, get) => ({ joinCommunity: serialAsync(async (projectUrl: string) => { try { - const insertErrors = await communityService.insertCommunity(projectUrl) + set((s) => { + s.communityInfo.loading = true + s.motionInfo.loading = true + }) + const communities = get().communityInfo.communities + const [communityUrl, insertErrors] = + await communityService.insertCommunity(projectUrl) if (insertErrors.length === 0) { - await get().refreshCache(false) + if (communities.length === 0) { + await get().communityInfo.selectCommunity(communityUrl) + } else { + await get().communityInfo.fetchCommunities(false) + } } + set((s) => { + s.communityInfo.loading = false + s.motionInfo.loading = false + }) return insertErrors } catch (ex) { get().setException(`Failed to join community. ${ex}`) diff --git a/src/renderer/src/store/communityStore.ts b/src/renderer/src/store/communityStore.ts index 0289235..def3765 100644 --- a/src/renderer/src/store/communityStore.ts +++ b/src/renderer/src/store/communityStore.ts @@ -37,13 +37,14 @@ export const createCommunityStore: StateCreator< }), selectCommunity: serialAsync(async (url: string) => { await get().tryRun(async () => { + set((s) => { + s.motionInfo.loading = true + s.communityInfo.loading = true + }) await communityService.selectCommunity(url) const searchArgs = get().motionInfo.searchArgs - await Promise.all([ - get().motionInfo.fetchMotions(searchArgs, false, false), - get().communityInfo.fetchCommunities(false), - ]) - await get().refreshCache(true) + await get().motionInfo.fetchMotions(searchArgs, false, false) + await get().communityInfo.fetchCommunities(false) }, `Failed to select community.`) }), }, diff --git a/src/shared/services/CommunityService.ts b/src/shared/services/CommunityService.ts index 8f0c976..447724e 100644 --- a/src/shared/services/CommunityService.ts +++ b/src/shared/services/CommunityService.ts @@ -8,7 +8,9 @@ import type { } from '../../electron/services/index.js' export abstract class AbstractCommunityService { - public abstract insertCommunity(projectUrl: string): Promise + public abstract insertCommunity( + projectUrl: string, + ): Promise<[string, string[]]> public abstract selectCommunity(communityUrl: string): Promise public abstract getCommunities(): Promise public abstract deployCommunity(