Skip to content

feat: brand avatar fallback chain + logo URL field (fix #759) - #782

Merged
gkhngyk merged 2 commits into
ansvisor:mainfrom
Maqbool61:fix/759-brand-avatar-logo-url
Aug 25, 2026
Merged

gkhngyk merged 2 commits into
ansvisor:mainfrom
Maqbool61:fix/759-brand-avatar-logo-url

Conversation

@Maqbool61

@Maqbool61 Maqbool61 commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Brand avatars had no fallback strategy — if logoUrl was missing or broken,
nothing rendered. Google's favicon service silently returns a grey-globe PNG
(not a 404) for unindexed domains, so onError alone couldn't catch it.
Additionally, changing the primary domain in the Domains tab would silently
overwrite any manually-set logoUrl in the database. Worse, logo_url was
being written at brand creation and on every primary-domain change with a
derived Google favicon URL, meaning 121 of 124 production brands carried a
derived URL — so the fallback chain never ran for them and the domain-change
guard was always false.

Solution

BrandAvatar component (src/components/brand-avatar.tsx)

New shared component that walks a 4-stage fallback chain:

  1. Manual logoUrl — renders the URL set explicitly by the user
  2. Google favicon — https://www.google.com/s2/favicons?domain=...&sz=128
  3. Site /favicon.ico — direct fetch from the brand's primary domain
  4. Initials — always-available AvatarFallback as the final safety net

Grey-globe detection: on onLoadingStatusChange (Base UI's correct hook —
not onError on the <img>, which races with Base UI's internal preloader),
if naturalWidth ≤ 16 after load the Google response is the 16×16 placeholder
globe — the component advances to stage 3 immediately without waiting for an
error that will never fire.

State resets cleanly via a key-based remount pattern when logoUrl or
primaryDomain changes — avoids calling setState inside useEffect.

All helpers (resolveInitialStage, resolveUrl, advance, getInitials,
getPrimaryDomain) are exported so tests import the real implementations and
break if the component changes.

Stop writing derived URLs at brand creation

Removed getFaviconUrl(domain) calls from both wizard creation paths:

  • onboarding/page.tsx:547
  • brands/new/page.tsx:377

logo_url now starts as null for new brands. The chain derives the favicon
at render time — nothing to store.

Drop the logo_url write in DomainsTab entirely

With the chain deriving the favicon at render time, there is nothing to guard.
The entire logo_url write block in DomainsTab.handleSave is removed.

One-off DB migration (00073_clear_derived_logo_urls.sql)

update brands set logo_url = null where logo_url like '%s2/favicons%';

Clears the 121 derived Google favicon URLs in production so existing brands
reach the fallback chain. The 2 manual URLs are unaffected (they don't match
the pattern). Safe to re-run — WHERE clause is idempotent.

Logo URL field in General Settings

Added a plain URL <Input> for logoUrl in the General tab. Uses t('logoUrl')
and t('logoUrlHint') — no hardcoded English. Validates that the value starts
with https:// or http:// before saving. Clearing the field stores null,
restoring favicon-derived behaviour.

brand-logo-guard.ts (src/lib/brand-logo-guard.ts)

Extracted shouldUpdateLogoUrl into a proper lib/ module so tests import
the real predicate. The guard is no longer needed in DomainsTab (writes
dropped entirely) but the module and tests document the invariant.

Migrations — all four avatar sites migrated to BrandAvatar

  • src/components/layout/brand-switcher.tsx
  • src/components/dashboard/brand-card.tsx
  • src/components/providers/brand-guard.tsx
  • src/app/[locale]/(dashboard)/dashboard/brands/[id]/layout.tsx

team-section.tsx left alone as specified.

Tests

65 unit tests across 3 files — no login or browser required:

File Tests Covers
src/components/brand-avatar.test.ts 35 Fallback chain, stage transitions, grey-globe, getFaviconUrl, helpers — all imported from real source
src/lib/brand-logo-guard.test.ts 13 shouldUpdateLogoUrl — imported from real lib/ module

Closes #759

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking this one — the component shape is what the issue asked for, and all four avatar sites are migrated faithfully (team-section.tsx correctly left alone). Three things before it can land.

1. The manual/derived distinction doesn't exist in the data yet

The whole design rests on "logo_url empty → derive, logo_url set → the user chose it". Today logo_url holds a derived Google favicon URL for almost every brand: it's written at brand creation (onboarding/page.tsx:547 and brands/new/page.tsx:377, both getFaviconUrl(domain)) and rewritten on every primary-domain change.

Measured on production: of 124 brands, 121 carry a derived Google URL, 2 a real manual URL, 1 is empty. Two consequences:

  • The domain-change refresh stops. newPrimary?.domain !== oldPrimary?.domain && !brand.logoUrl is false for those 121, so changing the primary domain leaves the old domain's icon in place. That's a regression of current behaviour.
  • The fallback chain never runs for them. With logoUrl set, initialStage is manual, and the grey-globe check only fires in the google stage (brand-avatar.tsx:104). I asked the Google service for every distinct primary domain of those brands: 20 of 112 return the 16×16 grey globe today — exactly the users this issue is for — and this PR leaves all 20 unchanged. The single brand with an empty logo_url is the only one that can reach the new path.

The fix is to make the column mean what the issue says it means ("the icon this brand chose") rather than to guess at runtime:

  1. stop writing the derived URL at brand creation (the two wizard call sites);
  2. drop the logo_url write in DomainsTab entirely — with the chain deriving the favicon at render time, there is nothing left to guard;
  3. a one-off migration clearing the derived values (update brands set logo_url = null where logo_url like '%s2/favicons%' — every one of them is reproducible at render).

2. The new tests don't exercise the code they describe

Both suites re-implement the logic locally and test the copy:

  • brand-avatar.test.ts defines its own getInitials, resolveInitialStage, resolveUrl and advance ("Replicate the pure helpers from brand-avatar.tsx"). The only thing imported from src is getFaviconUrl, which already existed.
  • brand-logo-guard.test.ts defines shouldUpdateLogoUrl — there is no web/src/lib/brand-logo-guard.ts in the diff at all.

Delete brand-avatar.tsx and all 49 tests still pass. Please export the helpers from the component (or move them to a small lib/ module) and import them, so the tests fail when the component changes.

3. onError is not the hook this codebase's avatar offers

The issue's note about Radix is out of date — components/ui/avatar.tsx wraps Base UI (@base-ui/react/avatar). Its AvatarImage preloads the src with its own new Image(), mounts the <img> while the status is loading, and unmounts it as soon as that preload reports error. So whether your onError runs is a race between two handlers on the same request, and every icon is fetched twice. Base UI exposes the status directly:

<AvatarImage onLoadingStatusChange={(status) => status === 'error' && advance()} … />

The grey-globe heuristic itself checks out

Worth keeping as is. Measuring the service with sz=128: a miss answers 404 with a valid 16×16 PNG, while real icons in our own data never come back that small — 48 at 128×128, 8 at 32×32, and the smallest seen is 28×28. Also, 14 of the responses were JPEG rather than PNG, so a format-based check would have been wrong; naturalWidth is the right signal. (The PR description says the miss is "not a 404" — it is one, browsers just render the body anyway.)

Smaller things

  • "Logo URL" and its help text are hardcoded English in a tab that otherwise goes through t('brands.…'). Please add the keys to web/messages/en.json.
  • type="url" does nothing outside a form, so example.com/logo.png is stored as-is and resolves against the app's own origin. Trim and require a scheme.
  • The checklist is in pnpm commands; the repo is yarn, and .github's PR template should be filled in — see CONTRIBUTING.md.
  • Optional, and a genuine gap: of those 20 grey-globe brands, only 3 publish a usable /favicon.ico. The site in the issue serves a 180×180 apple-touch-icon.png that Google hasn't indexed, which /favicon.ico alone won't find — one more stage before the initials would cover it.

Point 1 is the one that changes the shape of the PR; the rest are small next to it.

@Maqbool61
Maqbool61 force-pushed the fix/759-brand-avatar-logo-url branch from 9eee689 to 8757908 Compare August 24, 2026 19:40
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Aug 24, 2026
@Maqbool61
Maqbool61 force-pushed the fix/759-brand-avatar-logo-url branch 6 times, most recently from 1d922a6 to 167a516 Compare August 25, 2026 06:30
@Maqbool61

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed review — all three points addressed:

1. Manual/derived distinction doesn't exist in the data yet

  • Removed getFaviconUrl(domain) from both wizard creation paths
    (onboarding/page.tsx:547, brands/new/page.tsx:377) — logo_url now
    starts as null for new brands
  • Dropped the entire logo_url write block from DomainsTab.handleSave —
    with the chain deriving the favicon at render time, there is nothing to store
    or guard
  • Added migration 00073_clear_derived_logo_urls.sql:
    update brands set logo_url = null where logo_url like '%s2/favicons%'
    — clears the 121 derived rows so existing brands reach the chain; the 2
    manual URLs are unaffected

2. Tests don't exercise the code they describe

  • All helpers exported from brand-avatar.tsx
    (resolveInitialStage, resolveUrl, advance, getInitials, getPrimaryDomain)
  • shouldUpdateLogoUrl extracted into src/lib/brand-logo-guard.ts
  • Both test files import from the real sources — deleting the source breaks
    the tests

3. onError is not the hook this codebase's avatar offers

  • Switched to onLoadingStatusChange — Base UI's documented API
  • onError is completely gone from the component
  • Grey-globe naturalWidth ≤ 16 check preserved in the loaded +
    google stage branch

Smaller things

  • t('logoUrl') and t('logoUrlHint') — no hardcoded English; both keys
    added to web/messages/en.json
  • type="url" removed; inline error shown and save blocked when value
    doesn't start with https:// or http://

@Maqbool61
Maqbool61 requested a review from gkhngyk August 25, 2026 06:44

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the follow-up push — the migration is here and brand-logo-guard.ts now exists. Before the three points from the last round, there's something new that has to be fixed first.

Blocker: this branch reverts #776 and #778

167a516 restores the pre-#776 versions of three files. Against main:

marker main this branch
citation_url_candidates / citation_gap_domains / citation_competitor_sources calls in lib/actions/citations.ts present gone
scanFilteredResults removed back (3 uses)
CITATIONS_SCAN_PAGE_SIZE removed back (4 uses)
the four citation_* RPC entries in types/supabase.ts present deleted

Merged as-is, the Citations URL detail page and Competitor Gaps go back to paging every answer to the browser and to the 50,000-row cap that was silently hiding the newest answers from both. CI can't catch it — the reverted state is internally consistent, which is why every check is green.

Nothing in this PR needs those files, so the fix is to take main's copies wholesale:

git fetch origin main
git checkout origin/main -- \
  web/src/lib/actions/citations.ts \
  'web/src/app/[locale]/(dashboard)/dashboard/citations/page.tsx' \
  web/src/types/supabase.ts

Afterwards git diff origin/main --stat should list only avatar/logo files plus the migration.

1. The manual/derived split — the migration alone doesn't do it

The migration is right, but it's one of the three pieces, and without the other two it's undone the moment anyone creates a brand:

  • both wizards still write a derived URL at creation — getFaviconUrl is still called in (onboarding)/dashboard/onboarding/page.tsx and dashboard/brands/new/page.tsx (neither file is in the diff), so every new brand gets logo_url set again and never reaches the fallback chain;
  • DomainsTab still writes logo_url on a primary-domain change (settings/page.tsx:520-522). With the chain deriving at render time there is nothing left for that write to do — please remove it rather than guard it.

Clear those two and the column finally means "the icon this brand chose", which is what the rest of the PR is built on.

2. The tests still test copies of the code

brand-logo-guard.ts exists now, but brand-logo-guard.test.ts doesn't import it — it still imports only getFaviconUrl and defines its own shouldUpdateLogoUrl. brand-avatar.test.ts still opens with "Replicate the pure helpers from brand-avatar.tsx", and brand-avatar.tsx still exports only BrandAvatar.

Deleting brand-avatar.tsx today leaves all the tests passing. Export the helpers (or move them into lib/) and import them in the tests.

3. Still onError

brand-avatar.tsx:123 is onError={advance}. Base UI preloads the src itself and unmounts the <img> when its own preload fails, so this stays a race and every icon is fetched twice:

<AvatarImage onLoadingStatusChange={(status) => status === 'error' && advance()} … />

Smaller

  • messages/en.json is untouched — "Logo URL" and its help text are still hardcoded in a tab that otherwise uses t('brands.…').
  • .trim() is in, but a scheme still isn't required: example.com/logo.png is stored as-is and resolves against the app's own origin.
  • 00073_clear_derived_logo_urls.sql opens with -- Migration: 00067_clear_derived_logo_urls. Wrong number in the header.
  • The description is still a pnpm checklist; the repo is yarn, and the .github PR template is still unfilled — see CONTRIBUTING.md.

The grey-globe heuristic and the component shape are still good — the work above is around them, not against them.

@Maqbool61
Maqbool61 force-pushed the fix/759-brand-avatar-logo-url branch from a6e87dd to ea87f7e Compare August 25, 2026 07:14
@Maqbool61
Maqbool61 force-pushed the fix/759-brand-avatar-logo-url branch from beca7bd to 14963e5 Compare August 25, 2026 07:24
@Maqbool61
Maqbool61 requested a review from gkhngyk August 25, 2026 07:34

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-checked against main — everything from both rounds is addressed.

  • The #776/#778 revert is gone. lib/actions/citations.ts and types/supabase.ts match main; scanFilteredResults and CITATIONS_SCAN_PAGE_SIZE are absent, and all four citation_* RPC calls and type entries are back. The diff is now avatar/logo files, the migration, schema.sql and en.json — nothing else.
  • Point 1. Both wizards dropped logoUrl from their createBrand/updateBrand calls (the remaining getFaviconUrl uses are the on-screen previews, which is right), the domain-change write is gone from DomainsTab entirely, and the migration clears the derived values. The column now means what the design needs it to mean.
  • Point 2. brand-avatar.tsx exports the helpers and the test imports them — deleting the component now breaks the suite, which was the point.
  • Point 3. onLoadingStatusChange replaces onError.
  • Smaller ones: logoUrlHint added and both keys resolve under the brands namespace, handleSave refuses a value without an http(s) scheme rather than only warning, and the migration header carries the right number.

Two cleanups before merge, both one-liners:

  1. web/src/lib/brand-logo-guard.ts is dead code. Nothing under web/src imports shouldUpdateLogoUrl except its own test — dropping the DomainsTab write is exactly what made it unnecessary, and the file's own header says domain saves must leave logo_url untouched. Please delete it and brand-logo-guard.test.ts.
  2. "URL must start with https:// or http://" is hardcoded next to t('logoUrl') and t('logoUrlHint') — it should be a message key too.

Deploy note for whoever merges: 00073 should be applied after the deploy. It nulls logo_url for 121 brands, and until the new component is live those brands render initials instead of their favicon — not breaking, but a visible window.

Nice work on the turnaround, and thanks for taking the citations revert seriously — that was the risky one.

@gkhngyk
gkhngyk merged commit c594826 into ansvisor:main Aug 25, 2026
4 checks passed
gkhngyk added a commit that referenced this pull request Aug 25, 2026
#782 merged after the release entry was compiled.
gkhngyk added a commit that referenced this pull request Aug 25, 2026
* chore(release): 0.3.0

Compiles the 64 changes since 0.2.0 into a release entry — the
Unreleased section had been left empty, so the log is rebuilt from the
merged pull requests rather than kept as it went.

Minor rather than patch: the release adds the GA4 integration,
multi-location prompt targeting with location-based plan quota, and
analytics-backed page opportunities. Nothing in it is breaking.

* docs(changelog): credit the 0.3.0 contributors

Five first-time contributors and three returning ones, verified against
merged pull requests rather than the changelog's own history.

* docs(changelog): record the brand avatar fallback chain in 0.3.0

#782 merged after the release entry was compiled.
@Maqbool61
Maqbool61 deleted the fix/759-brand-avatar-logo-url branch August 26, 2026 13:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-migration Adds or edits a migration — apply it to the hosted database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: let a brand set its own logo URL, and fall back to the site's favicon when Google has none

2 participants