Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

3.9 KB
Raw Blame
import { ADMIN_EMAIL } from '../../lib/contact.ts';
import { accountHandle } from '../../lib/accounts.ts';
import type { AccountFields } from '../../lib/accounts.ts';
import type { Platform } from '../../../../shared/types/domain.ts';

export const TESTER_HINT = `If your account is not an Instagram tester yet, email ${ADMIN_EMAIL} to be added, then accept the invite in Instagram.`;

// the counts each platform card shows
export interface AccountMetricFields {
  subscriberCount?: number;
  viewCount?: number;
  videoCount?: number;
  followersCount?: number;
  followsCount?: number;
  mediaCount?: number;
}

export type AccountMetric = [label: string, value: number | undefined];

export interface AccountSectionInfo {
  key: Platform;
  title: string;
}

export interface OAuthReturn {
  connected: boolean;
  // the account was already connected, and connecting it again renewed its token
  reconnected: boolean;
  errorMessage: string | null;
  openAdd: boolean;
}

const IG_ERRORS: Record<string, string> = {
  already_added: 'This Instagram account is already connected.',
  cancelled: 'Instagram sign-in was cancelled. Try again when you are ready.',
  access_denied: `Instagram did not grant access. Approve every requested permission and try again. ${TESTER_HINT}`,
  invalid_state: 'The Instagram sign-in link expired. Start the connection again.',
  not_configured: 'Instagram connections are not set up on this server. Ask your administrator to add the Instagram app ID and secret.',
};

const METRICS: Record<Platform, (acct: AccountMetricFields) => AccountMetric[]> = {
  youtube:   acct => [['Subscribers', acct.subscriberCount], ['Views', acct.viewCount], ['Videos', acct.videoCount]],
  instagram: acct => [['Followers', acct.followersCount], ['Following', acct.followsCount], ['Posts', acct.mediaCount]],
};

const ACCOUNT_SECTIONS: AccountSectionInfo[] = [
  { key: 'youtube',   title: 'YouTube channels' },
  { key: 'instagram', title: 'Instagram accounts' },
];

export function instagramErrorMessage(code: string): string {
  return IG_ERRORS[code] || `Instagram connection failed: ${code}. ${TESTER_HINT}`;
}

export interface InstagramCompletion {
  code: string;
  state: string;
}

// a finished Instagram sign-in comes back as #ig_code=...&ig_state=..., in the fragment so it never reaches a server log
export function readInstagramCompletion(hash: string): InstagramCompletion | null {
  const params = new URLSearchParams(hash.replace(/^#/, ''));
  const code = params.get('ig_code');
  const state = params.get('ig_state');
  return code && state ? { code, state } : null;
}

// a failed or cancelled sign-in returns ?ig_error=...; ?add=1 opens the add flow
export function readOAuthReturn(searchParams: URLSearchParams): OAuthReturn | null {
  const connected = searchParams.get('ig_connected');
  const igError = searchParams.get('ig_error');
  const add = searchParams.get('add');
  if (!connected && !igError && !add) return null;

  return {
    connected: connected === 'true' || connected === 'reconnected',
    reconnected: connected === 'reconnected',
    errorMessage: igError ? instagramErrorMessage(igError) : null,
    openAdd: Boolean(add),
  };
}

export function accountMetrics(platform: Platform, account: AccountMetricFields): AccountMetric[] {
  return METRICS[platform](account);
}

export function accountCardHandle(account: AccountFields): string {
  return accountHandle(account);
}

export function visibleSections(groups: Record<Platform, unknown[]>): AccountSectionInfo[] {
  return ACCOUNT_SECTIONS.filter(section => groups[section.key].length > 0);
}

export type AddAccountField = 'channel';

export interface AddAccountProblem {
  field: AddAccountField;
  message: string;
}

export function channelFieldProblem(input: string): AddAccountProblem | null {
  return input.trim() ? null : { field: 'channel', message: 'Paste a channel link, an @handle or a channel ID that starts with UC.' };
}