Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions projects/packages/sharing-likes/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,14 @@ The React screen is the exception: behind `rsm_jetpack_ui_modernization_sharing_
`routes/` and `packages/init/` sit at the root, where wp-build requires them); a
later JS surface gets its own sibling directory. The screen takes Newsletter's
Jetpack chrome on purpose. It reads `JetpackScriptData.sharing_likes` for its first
render and saves through `src/rest/`.
render and saves through `src/rest/`. Its data lives in one `@wordpress/data` store
(`data/store.ts`), and every write goes through the queue in `data/writes.ts`, so
requests reach the server one at a time, in order. Like Newsletter's settings, it
uses neither TanStack Query nor `@wordpress/core-data`, which would load the block
editor's scripts onto the page.

Its services manager, under `src/settings-app/services/`, saves the enabled lists
through `useSaveServices`, in the same write scope as every other save. Removing
through `useSaveServices`, in the same write queue as every other save. Removing
the last service on a block theme hands the section to the block with no way
back, which is why that one removal asks first instead of offering Undo. Deleting
a custom service leaves its ID in `sharing-services`, so `useCustomService` saves
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Settings: Keep the React Sharing screen's data in a WordPress data store instead of TanStack Query.
1 change: 0 additions & 1 deletion projects/packages/sharing-likes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"@automattic/jetpack-components": "workspace:*",
"@automattic/jetpack-script-data": "workspace:*",
"@jetpack-sharing-likes/init": "link:packages/init",
"@tanstack/react-query": "5.102.8",
"@wordpress/admin-ui": "2.12.0",
"@wordpress/api-fetch": "7.57.0",
"@wordpress/base-styles": "13.3.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"dependencies": {
"@automattic/jetpack-components": "workspace:*",
"@automattic/jetpack-script-data": "workspace:*",
"@tanstack/react-query": "5.102.8",
"@types/react": "18.3.31",
"@wordpress/api-fetch": "7.57.0",
"@wordpress/components": "42.0.0",
Expand Down
14 changes: 4 additions & 10 deletions projects/packages/sharing-likes/routes/settings/stage.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
import { QueryClientProvider } from '@tanstack/react-query';
import { createQueryClient, seedQueryClient } from '../../src/settings-app/data/queries';
import { dispatch } from '@wordpress/data';
import { store } from '../../src/settings-app/data/store';
import { getSharingLikesScriptData } from '../../src/settings-app/script-data';
import { SettingsScreen } from '../../src/settings-app/settings-screen';
import '../../src/settings-app/style.scss';

const queryClient = createQueryClient();

const scriptData = getSharingLikesScriptData();
if ( scriptData ) {
seedQueryClient( queryClient, scriptData );
dispatch( store ).seed( scriptData );
}

/**
* wp-build stage for Settings > Sharing.
*
* @return The settings screen.
*/
const Stage = () => (
<QueryClientProvider client={ queryClient }>
<SettingsScreen />
</QueryClientProvider>
);
const Stage = () => <SettingsScreen />;

export { Stage as stage };
98 changes: 38 additions & 60 deletions projects/packages/sharing-likes/src/settings-app/data/queries.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,15 @@
import { QueryClient, useQuery } from '@tanstack/react-query';
import { fetchServices, fetchSettings, fetchStatus } from './api';
import type { Settings, SharingLikesScriptData, Status } from '../types';

export const queryKeys = {
status: [ 'sharing-likes', 'status' ] as const,
settings: [ 'sharing-likes', 'settings' ] as const,
services: [ 'sharing-likes', 'services' ] as const,
};

// Every write shares this scope, so requests reach the server one at a time, in order.
export const MUTATION_SCOPE = { id: 'sharing-likes' };

export const SAVE_SETTING_KEY = [ 'sharing-likes', 'save-setting' ] as const;

export const SAVE_SERVICES_KEY = [ 'sharing-likes', 'save-services' ] as const;

export const CUSTOM_SERVICE_KEY = [ 'sharing-likes', 'custom-service' ] as const;

export const FEATURE_ACTION_KEY = [ 'sharing-likes', 'feature-action' ] as const;

// Matches every write by prefix, saves and feature actions alike.
export const WRITES_KEY = [ 'sharing-likes' ] as const;

/**
* The screen's query client.
*
* @return Query client.
*/
export function createQueryClient(): QueryClient {
return new QueryClient( {
defaultOptions: {
// Writes wait on their rereads, and a paused read only resumes once every queued write has finished.
queries: { refetchOnWindowFocus: false, retry: false, networkMode: 'always' },
},
} );
}

/**
* Start the cache from the script data, so the first render waits on no request.
*
* @param queryClient - Query client.
* @param scriptData - The page's script data.
*/
export function seedQueryClient( queryClient: QueryClient, scriptData: SharingLikesScriptData ) {
queryClient.setQueryData( queryKeys.status, scriptData.status );
queryClient.setQueryData( queryKeys.settings, scriptData.settings );
}
import { useRegistry, useSelect } from '@wordpress/data';
import { useEffect } from '@wordpress/element';
import { store } from './store';
import type { Services, Settings, Status } from '../types';

/**
* Which variant each section renders.
*
* @return Status, once known.
*/
export function useStatus(): Status | undefined {
return useQuery( { queryKey: queryKeys.status, queryFn: fetchStatus, staleTime: Infinity } ).data;
return useSelect( select => select( store ).getStatus(), [] );
}

/**
Expand All @@ -62,22 +18,44 @@ export function useStatus(): Status | undefined {
* @return Settings, once known.
*/
export function useSettings(): Settings | undefined {
return useQuery( { queryKey: queryKeys.settings, queryFn: fetchSettings, staleTime: Infinity } )
.data;
return useSelect( select => select( store ).getSettings(), [] );
}

/**
* The enabled services. The route answers 409 unless the Sharing section configures.
*
* @param enabled - Whether to fetch.
* @return The services query.
* @return The services, and whether the first read is still to come.
*/
export function useServices( enabled: boolean ): {
data: Services | undefined;
isPending: boolean;
} {
const registry = useRegistry();
// A list that could not be loaded tries again when it next appears.
useEffect( () => {
if ( enabled && registry.select( store ).hasReadFailed( 'services' ) ) {
registry.dispatch( store ).reread( 'services' );
}
}, [ enabled, registry ] );

return useSelect(
select => {
if ( ! enabled ) {
return { data: undefined, isPending: true };
}
const data = select( store ).getServices();
return { data, isPending: ! data && ! select( store ).hasReadFailed( 'services' ) };
},
[ enabled ]
);
}

/**
* Whether any write is queued or in flight.
*
* @return Whether something is saving.
*/
export function useServices( enabled: boolean ) {
return useQuery( {
queryKey: queryKeys.services,
queryFn: fetchServices,
enabled,
staleTime: Infinity,
retry: false,
} );
export function useIsSaving(): boolean {
return useSelect( select => select( store ).getPendingWrites() > 0, [] );
}
Loading
Loading