Files
o2o-infinith-demo/src/lib/normalizeHandles.ts
T
Haewon KamandClaude Opus 4.6 bd7bc45192 fix: Instagram data collection pipeline — handle normalization + DB persistence
- Add normalizeInstagramHandle() utility (Edge + browser) to strip URLs, @ prefixes
- generate-report: normalize handles before saving, persist socialHandles in report JSONB
- enrich-channels: normalize Instagram handle before Apify call (defense in depth)
- useReport: recover socialHandles + channelEnrichment from DB on direct URL access
- ReportPage: skip redundant enrichment when data already exists in DB

Fixes: Instagram enrichment failing due to URL-format handles passed to Apify

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-02 13:34:54 +09:00

33 lines
882 B
TypeScript

/**
* Normalize an Instagram handle from various input formats to a pure username.
* Browser-side copy of supabase/functions/_shared/normalizeHandles.ts
*/
export function normalizeInstagramHandle(
raw: string | null | undefined,
): string | null {
if (!raw || typeof raw !== 'string') return null;
let handle = raw.trim();
if (!handle) return null;
if (handle.includes('instagram.com')) {
try {
const urlStr = handle.startsWith('http') ? handle : `https://${handle}`;
const url = new URL(urlStr);
const segments = url.pathname.split('/').filter(Boolean);
handle = segments[0] || '';
} catch {
const match = handle.match(/instagram\.com\/([^/?#]+)/);
handle = match?.[1] || '';
}
}
if (handle.startsWith('@')) {
handle = handle.slice(1);
}
handle = handle.replace(/\/+$/, '');
return handle || null;
}