Skip to content

fix(console): guard organization access in realtimePricing to prevent runtime crash#2961

Merged
HarshMN2345 merged 1 commit intomainfrom
chore-add-org-guard
Apr 7, 2026
Merged

fix(console): guard organization access in realtimePricing to prevent runtime crash#2961
HarshMN2345 merged 1 commit intomainfrom
chore-add-org-guard

Conversation

@HarshMN2345
Copy link
Copy Markdown
Member

What does this PR do?

(Provide a description of what this PR does.)

Test Plan

(Write your test plan here. If you changed any code, please provide us with clear instructions on how you verified your changes work.)

Related PRs and Issues

(If this PR is related to any other PR or resolves any issue or related to any issue link all related PR and issues here.)

Have you read the Contributing Guidelines on issues?

(Write your answer here.)

@HarshMN2345 HarshMN2345 merged commit b0553df into main Apr 7, 2026
2 of 4 checks passed
@HarshMN2345 HarshMN2345 deleted the chore-add-org-guard branch April 7, 2026 13:42
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Apr 7, 2026

Greptile Summary

This PR fixes a real runtime crash in realtimePricing.svelte where the Svelte 4 reactive statement $: href = ... would execute eagerly — even when the surrounding {#if $organization?.$id} template guard is false — and directly access $organization.$id without optional chaining, throwing a TypeError when $organization is undefined or null. The fix is logically correct.

  • Bug fix is valid: Svelte 4 $: reactive statements are not scoped to {#if} blocks, so the guard in the template does not protect the reactive expression from running when $organization is unset.
  • Extra intermediate reactive variable: Introducing $: orgId = $organization?.$id adds an unnecessary reactive layer; the same protection is achieved more simply by checking $organization?.$id directly in the href reactive expression.
  • Svelte 5 migration not done: Per AGENTS.md, files should be migrated to runes when touched if practical. This 46-line file is a straightforward candidate.
  • Empty PR description: The PR has no description, test plan, or linked issue, making it harder to review and audit.

Confidence Score: 5/5

Safe to merge — the fix correctly prevents a real runtime crash with no regressions introduced.

All remaining findings are P2 style/convention suggestions (extra reactive variable, Svelte 5 migration). There are no P0 or P1 defects in the changed code.

No files require special attention beyond the style suggestions noted.

Important Files Changed

Filename Overview
src/lib/components/billing/alerts/realtimePricing.svelte Correct runtime-crash fix adding optional chaining guard on $organization before building the href URL; minor style nits only (extra intermediate reactive variable, Svelte 5 migration not done).

Comments Outside Diff (1)

  1. src/lib/components/billing/alerts/realtimePricing.svelte, line 1-27 (link)

    P2 Svelte 5 runes migration not done

    Per AGENTS.md: "When touching a file, migrate it to runes if practical." This file is 46 lines and straightforward — migration is practical. The Svelte 5 runes equivalent would be:

    <script lang="ts">
        import { base } from '$app/paths';
        import { browser } from '$app/environment';
        import { HeaderAlert } from '$lib/layout';
        import { organization, currentPlan } from '$lib/stores/organization';
        import { Button } from '$lib/elements/forms';
    
        const DISMISS_KEY = 'realtimePricingDismissed';
    
        let dismissed = $state(browser && localStorage.getItem(DISMISS_KEY) === 'true');
    
        function handleDismiss() {
            dismissed = true;
            if (browser) localStorage.setItem(DISMISS_KEY, 'true');
        }
    
        const orgId = $derived($organization?.$id);
        const href = $derived(
            orgId
                ? $currentPlan?.usagePerProject
                    ? `${base}/organization-${orgId}/billing`
                    : `${base}/organization-${orgId}/usage`
                : ''
        );
    </script>

    Context Used: AGENTS.md (source)

    Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Reviews (1): Last reviewed commit: "fix(console): guard organization access ..." | Re-trigger Greptile

Comment on lines +21 to +26
$: orgId = $organization?.$id;
$: href = orgId
? $currentPlan?.usagePerProject
? `${base}/organization-${orgId}/billing`
: `${base}/organization-${orgId}/usage`
: '';
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.

P2 Unnecessary intermediate reactive variable

The extra $: orgId = $organization?.$id reactive declaration adds a second reactive dependency chain where one is sufficient. Since orgId is used only inside the href expression, you can inline the guard directly:

Suggested change
$: orgId = $organization?.$id;
$: href = orgId
? $currentPlan?.usagePerProject
? `${base}/organization-${orgId}/billing`
: `${base}/organization-${orgId}/usage`
: '';
$: href = $organization?.$id
? $currentPlan?.usagePerProject
? `${base}/organization-${$organization.$id}/billing`
: `${base}/organization-${$organization.$id}/usage`
: '';

This achieves the same crash protection with one fewer reactive declaration, and keeps the intent clear: href is empty when the org is not loaded.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants