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
13 changes: 10 additions & 3 deletions .github/workflows/cleanup-stripe-test-accounts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,16 @@ jobs:
RESPONSE=$(curl -s -u "$STRIPE_SECRET_KEY:" "https://api.stripe.com/v1/accounts?limit=100&starting_after=$STARTING_AFTER")
fi

# Extract account data
ACCOUNTS=$(echo "$RESPONSE" | jq -c '.data[]')
HAS_MORE=$(echo "$RESPONSE" | jq -r '.has_more')
# Check for API errors
ERROR=$(echo "$RESPONSE" | jq -r '.error.message // empty')
if [ -n "$ERROR" ]; then
echo "Stripe API error: $ERROR"
exit 1
fi

# Extract account data - handle null/missing data array gracefully
ACCOUNTS=$(echo "$RESPONSE" | jq -c '.data // [] | .[]')
HAS_MORE=$(echo "$RESPONSE" | jq -r '.has_more // false')

while IFS= read -r account; do
[ -z "$account" ] && continue
Expand Down
182 changes: 181 additions & 1 deletion apps/admin-x-framework/src/api/members.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,62 @@
import {Meta, createMutation, createQuery, createQueryWithId} from '../utils/api/hooks';
import {InfiniteData} from '@tanstack/react-query';
import {Meta, createInfiniteQuery, createMutation, createQuery, createQueryWithId} from '../utils/api/hooks';

export type MemberLabel = {
id: string;
name: string;
slug: string;
created_at: string;
};

export type MemberTier = {
id: string;
name: string;
slug: string;
active: boolean;
type: string;
};

export type MemberNewsletter = {
id: string;
uuid: string;
name: string;
slug: string;
status: string;
};

export type MemberSubscription = {
id: string;
customer: {
id: string;
name: string | null;
email: string;
};
plan: {
id: string;
nickname: string;
interval: 'month' | 'year';
currency: string;
amount: number;
};
status: string;
start_date: string;
current_period_end: string;
cancel_at_period_end: boolean;
price: {
id: string;
price_id: string;
nickname: string;
amount: number;
currency: string;
type: string;
interval: 'month' | 'year';
};
tier: MemberTier;
offer: {
id: string;
name: string;
} | null;
};

export type Member = {
id: string;
Expand All @@ -7,6 +65,26 @@ export type Member = {
name?: string;
email?: string;
avatar_image?: string;
status: 'free' | 'paid' | 'comped';
note?: string;
subscribed: boolean;
labels?: MemberLabel[];
tiers?: MemberTier[];
newsletters?: MemberNewsletter[];
subscriptions?: MemberSubscription[];
email_count?: number;
email_opened_count?: number;
email_open_rate?: number | null;
// TODO: The server returns geolocation as a JSON-encoded string (not a parsed object).
// Long term we should parse this on the server side and return a proper object.
geolocation?: string | null;
email_suppression?: {
suppressed: boolean;
info?: {
reason: string;
timestamp: string;
};
};
last_seen_at: string | null;
last_commented_at: string | null;
can_comment?: boolean;
Expand Down Expand Up @@ -62,3 +140,105 @@ export const useEnableMemberCommenting = createMutation<
dataType: 'CommentsResponseType'
}
});

// Infinite query for members list with virtual scrolling
export interface MembersInfiniteResponseType extends MembersResponseType {
isEnd: boolean;
}

export const useBrowseMembersInfinite = createInfiniteQuery<MembersInfiniteResponseType>({
dataType,
path: '/members/',
defaultSearchParams: {
include: 'labels,tiers',
limit: '50',
order: 'created_at desc'
},
defaultNextPageParams: (lastPage, otherParams) => {
if (!lastPage.meta?.pagination.next) {
return undefined;
}
return {
...otherParams,
page: lastPage.meta.pagination.next.toString()
};
},
returnData: (originalData) => {
const {pages} = originalData as InfiniteData<MembersResponseType>;
const members = pages.flatMap(page => page.members);
const meta = pages[pages.length - 1].meta;

return {
members,
meta,
isEnd: meta ? meta.pagination.pages === meta.pagination.page : true
};
}
});

// Bulk operations
export interface BulkEditAction {
type: 'addLabel' | 'removeLabel' | 'unsubscribe';
meta?: {
label?: {id: string};
};
}

export interface BulkOperationResponseType {
meta: {
stats: {
successful: number;
unsuccessful: number;
};
unsuccessfulIds?: string[];
errors?: Array<{id?: string; message: string}>;
};
}

export const useBulkEditMembers = createMutation<
BulkOperationResponseType,
{filter: string; all?: boolean; action: BulkEditAction}
>({
method: 'PUT',
path: () => '/members/bulk/',
body: ({action}) => ({
bulk: {
action: action.type,
meta: action.meta || {}
}
}),
searchParams: ({filter, all}) => {
if (!all && !filter) {
throw new Error('Bulk edit requires either a filter or all flag');
}
const params: Record<string, string> = {};
if (all) {
params.all = 'true';
} else {
params.filter = filter;
}
return params;
},
invalidateQueries: {dataType}
});

export const useBulkDeleteMembers = createMutation<
BulkOperationResponseType,
{filter: string; all?: boolean}
>({
method: 'DELETE',
path: () => '/members/',
searchParams: ({filter, all}) => {
if (!all && !filter) {
throw new Error('Bulk delete requires either a filter or all flag');
}
const params: Record<string, string> = {};
if (all) {
params.all = 'true';
} else {
params.filter = filter;
}
return params;
},
invalidateQueries: {dataType}
});
22 changes: 22 additions & 0 deletions apps/admin-x-framework/src/api/pages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {Meta, createQuery} from '../utils/api/hooks';

export type Page = {
id: string;
title: string;
slug: string;
url: string;
status?: string;
published_at?: string;
};

export interface PagesResponseType {
meta?: Meta
pages: Page[];
}

const dataType = 'PagesResponseType';

export const useBrowsePages = createQuery<PagesResponseType>({
dataType,
path: '/pages/'
});
29 changes: 29 additions & 0 deletions apps/admin-x-framework/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,32 @@ export function downloadFile(url: string) {
export function downloadFromEndpoint(path: string) {
downloadFile(`${getGhostPaths().apiRoot}${path}`);
}

/**
* Downloads a file by fetching it as a blob and triggering a browser download.
* Use this instead of downloadFile/downloadFromEndpoint for streaming responses
* (e.g. large CSV exports) where the iframe approach may not work reliably.
*/
export async function blobDownload(url: string, filename: string): Promise<void> {
const response = await fetch(url, {method: 'GET'});

if (!response.ok) {
throw new Error(`Download failed: ${response.status} ${response.statusText}`);
}

const blob = await response.blob();
const blobUrl = window.URL.createObjectURL(blob);
const a = document.createElement('a');

a.href = blobUrl;
a.download = filename;
document.body.appendChild(a);
a.click();
a.remove();
window.URL.revokeObjectURL(blobUrl);
}

export async function blobDownloadFromEndpoint(path: string, filename: string): Promise<void> {
const url = `${getGhostPaths().apiRoot}${path}`;
return blobDownload(url, filename);
}
Loading
Loading