-
-
Notifications
You must be signed in to change notification settings - Fork 11.8k
Added gift links admin UI for analytics, posts list, and settings #28897
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
Open
jonatansberg
wants to merge
2
commits into
main
Choose a base branch
from
jonatan-ber-3729-gift-links-admin-ui-editor-settings
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import {createMutation, createQueryWithId} from '../utils/api/hooks'; | ||
|
|
||
| // A gift link as the admin API emits it. Usage counts (visits/views) are NOT | ||
| // stored on the link — they come from the analytics pipeline (see the | ||
| // gift-link usage hook in the posts app), so the resource is just the token. | ||
| export type GiftLink = { | ||
| token: string; | ||
| created_at: string; | ||
| }; | ||
|
|
||
| export type GiftLinksResponseType = { | ||
| gift_links: GiftLink[]; | ||
| }; | ||
|
|
||
| export type RemoveAllGiftLinksResponseType = { | ||
| meta: { | ||
| count: number; | ||
| }; | ||
| }; | ||
|
|
||
| // Gift links hang off a post or a page; both map to the same id-keyed, | ||
| // type-agnostic controller server-side, but we address the canonical route so | ||
| // the resource stays explicit. | ||
| export type GiftLinkResource = 'posts' | 'pages'; | ||
|
|
||
| export type GiftLinkMutationPayload = { | ||
| id: string; | ||
| resource?: GiftLinkResource; | ||
| }; | ||
|
|
||
| const dataType = 'GiftLinksResponseType'; | ||
|
|
||
| const giftLinkPath = ({id, resource = 'posts'}: GiftLinkMutationPayload) => `/${resource}/${id}/gift_links/`; | ||
|
|
||
| // GET /posts/:id/gift_links/ — read the active link for a post without minting | ||
| // one (empty when none exists yet). Read-only, so it's safe to fire on render | ||
| // (e.g. the analytics gift-link card). Posts only; the analytics screen never | ||
| // reads pages. | ||
| export const useReadGiftLink = createQueryWithId<GiftLinksResponseType>({ | ||
| dataType, | ||
| path: id => `/posts/${id}/gift_links/` | ||
| }); | ||
|
|
||
| // PUT /:resource/:id/gift_links/ — idempotently ensure (create-or-get) the | ||
| // active link, so opening the UI always has a URL to show. | ||
| export const useEnsureGiftLink = createMutation<GiftLinksResponseType, GiftLinkMutationPayload>({ | ||
| method: 'PUT', | ||
| path: giftLinkPath, | ||
| invalidateQueries: { | ||
| dataType | ||
| } | ||
| }); | ||
|
|
||
| // POST /:resource/:id/gift_links/ — mint a fresh link, invalidating the old | ||
| // token. Surfaced in the UI as "reset", but the backend controller is `create`. | ||
| export const useCreateGiftLink = createMutation<GiftLinksResponseType, GiftLinkMutationPayload>({ | ||
| method: 'POST', | ||
| path: giftLinkPath, | ||
| invalidateQueries: { | ||
| dataType | ||
| } | ||
| }); | ||
|
|
||
| // PUT /gift_links/remove_all/ — site-wide kill switch (Owner/Admin), danger zone. | ||
| export const useRemoveAllGiftLinks = createMutation<RemoveAllGiftLinksResponseType, null>({ | ||
| method: 'PUT', | ||
| path: () => '/gift_links/remove_all/', | ||
| invalidateQueries: { | ||
| dataType | ||
| } | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { Suspense, lazy, useEffect, useState } from "react"; | ||
| import { EmberFallback, subscribeOpenGiftLinkModal } from "./ember-bridge"; | ||
| import type { OpenGiftLinkModalEvent } from "./ember-bridge"; | ||
|
|
||
| // The gift-link modal is React-owned but triggered from the Ember posts/pages | ||
| // list. It's only needed once someone opens it, so lazy-load it rather than | ||
| // pulling the posts bundle into every list view. | ||
| const GiftLinkModal = lazy(() => import("@tryghost/posts/gift-link-modal")); | ||
|
|
||
| /** | ||
| * Bridges the Ember posts/pages context menu to the React gift-link modal. | ||
| * | ||
| * Subscribes to the bridge on mount and owns the modal's open/close state: | ||
| * each request opens the modal for the named post/page (reopening the same one | ||
| * just re-fires the event), and closing flips `open` while keeping the target | ||
| * so the modal can animate out. | ||
| */ | ||
| function GiftLinkModalHost() { | ||
| const [target, setTarget] = useState<OpenGiftLinkModalEvent | null>(null); | ||
| const [open, setOpen] = useState(false); | ||
|
|
||
| useEffect(() => subscribeOpenGiftLinkModal((event) => { | ||
| setTarget(event); | ||
| setOpen(true); | ||
| }), []); | ||
|
|
||
| if (!target) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <Suspense fallback={null}> | ||
| <GiftLinkModal | ||
| key={`${target.resource}:${target.id}`} | ||
| open={open} | ||
| postId={target.id} | ||
| resource={target.resource} | ||
| onOpenChange={setOpen} | ||
| /> | ||
| </Suspense> | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Route element for the Ember-backed posts and pages lists. Delegates the page | ||
| * itself to Ember (EmberFallback) while keeping the React gift-link modal host | ||
| * mounted so the list's context menu can open it. | ||
| */ | ||
| export function EmberListWithGiftLinks() { | ||
| return ( | ||
| <> | ||
| <EmberFallback /> | ||
| <GiftLinkModalHost /> | ||
| </> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import {isAdminUser, isAuthorUser, isEditorUser, isOwnerUser} from '@tryghost/admin-x-framework/api/users'; | ||
| import {useCurrentUser} from '@tryghost/admin-x-framework/api/current-user'; | ||
| import {useGlobalData} from '@src/providers/post-analytics-context'; | ||
| import {useMemo} from 'react'; | ||
|
|
||
| // Whether the current user can manage a gift link for this post. Mirrors | ||
| // canCopyGiftLink in the Ember app/utils/gift-link.js: requires the labs flag, | ||
| // a published gated (non-public) post, and a managing role. | ||
| export const useCanManageGiftLink = (post?: {status?: string; visibility?: string}) => { | ||
| const {data: globalData} = useGlobalData(); | ||
| const {data: currentUser} = useCurrentUser(); | ||
|
|
||
| return useMemo(() => { | ||
| if (!globalData?.labs?.giftLinks || !post || !currentUser) { | ||
| return false; | ||
| } | ||
| const eligible = post.status === 'published' && Boolean(post.visibility) && post.visibility !== 'public'; | ||
| const canManage = isOwnerUser(currentUser) || isAdminUser(currentUser) || isEditorUser(currentUser) || isAuthorUser(currentUser); | ||
| return eligible && canManage; | ||
| }, [globalData?.labs?.giftLinks, post, currentUser]); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import {StatsConfig, useTinybirdQuery} from '@tryghost/admin-x-framework'; | ||
| import {useBrowseConfig} from '@tryghost/admin-x-framework/api/config'; | ||
| import {useMemo} from 'react'; | ||
|
|
||
| export interface GiftLinkUsage { | ||
| visits: number; | ||
| views: number; | ||
| } | ||
|
|
||
| interface GiftLinkVisitsRow { | ||
| gift: string; | ||
| visits: number | string; | ||
| views: number | string; | ||
| } | ||
|
|
||
| // Reads gift-link usage from the web-analytics pipeline (the same Tinybird | ||
| // mechanism every other analytics surface uses), keyed on the link token. | ||
| // | ||
| // Usage tracking is best-effort and entirely optional: analytics may be turned | ||
| // off (no statsConfig), the per-link pipe may not be deployed yet, or the query | ||
| // may error. In every one of those cases `usage` is `undefined` and the caller | ||
| // simply hides the count — the rest of the gift-link UI keeps working. | ||
| export const useGiftLinkUsage = ({postUuid, token, enabled = true}: { | ||
| postUuid?: string; | ||
| token?: string; | ||
| enabled?: boolean; | ||
| }) => { | ||
| const {data: configData} = useBrowseConfig(); | ||
| const statsConfig = configData?.config?.stats as StatsConfig | undefined; | ||
|
|
||
| const params = useMemo(() => ({ | ||
| site_uuid: statsConfig?.id || '', | ||
| post_uuid: postUuid || '' | ||
| }), [statsConfig?.id, postUuid]); | ||
|
|
||
| const {data, loading, error} = useTinybirdQuery({ | ||
| endpoint: 'api_gift_link_visits', | ||
| statsConfig: statsConfig || {id: ''}, | ||
| params, | ||
| enabled: enabled && Boolean(statsConfig?.id) && Boolean(postUuid) | ||
| }); | ||
|
|
||
| const usage = useMemo<GiftLinkUsage | undefined>(() => { | ||
| // No token yet, the query is disabled/loading, or it errored → no data | ||
| // to show. Distinct from "ran and found zero", which yields {visits: 0}. | ||
| if (!token || error || !Array.isArray(data)) { | ||
| return undefined; | ||
| } | ||
| const row = (data as unknown as GiftLinkVisitsRow[]).find(r => r.gift === token); | ||
| return { | ||
| visits: Number(row?.visits) || 0, | ||
| views: Number(row?.views) || 0 | ||
| }; | ||
| }, [data, token, error]); | ||
|
|
||
| return {usage, loading}; | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
With analytics enabled, this hook asks Tinybird for
api_gift_link_visits, but repo-widerg "api_gift_link_visits" ghost/core/core/server/data/tinybirdfinds no endpoint datafile, whilegetStatEndpointUrlresolves endpoint names to/v0/pipes/<name>.json. As a result every gift-link card/modal calls a non-existent pipe and the visitor count stays hidden/—even when visits exist; add the Tinybird endpoint (and any versioned variants required bystatsConfig.version) or gate this query until it exists.Useful? React with 👍 / 👎.