Skip to content

Commit

Permalink
ref: Make react_config available in context
Browse files Browse the repository at this point in the history
This patch refactors a 10-year-old hack where we used a template tag to produce the initial data JSON, making the contents of that blob unavailable to any Django template. With this, all views extending from base gets a `react_config` context object so we can do things like sniffing user's language or theme preferences if available and modify the HTML output based on that.

Related #81611
  • Loading branch information
BYK committed Dec 4, 2024
1 parent d40c56b commit 7fcb1c5
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/sentry/templates/sentry/base-react.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

{% load sentry_api sentry_assets sentry_helpers %}

{% block wrapperclass %}theme-{% user_theme %}{% endblock %}

{% block body %}
<div id="blk_router">
<div class="loading triangle">
Expand Down
3 changes: 1 addition & 2 deletions src/sentry/templates/sentry/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
{% load sentry_assets %}
{% load sentry_features %}
{% load sentry_helpers %}
{% load sentry_react %}
{% load sentry_status %}
{% get_sentry_version %}
<!DOCTYPE html>
Expand Down Expand Up @@ -41,7 +40,7 @@
{% block initial_data %}
{% script %}
<script>
window.__initialData = {% get_react_config %};
window.__initialData = {% react_config|to_json %};
</script>
{% endscript %}
{% endblock %}
Expand Down
13 changes: 0 additions & 13 deletions src/sentry/templatetags/sentry_react.py

This file was deleted.

5 changes: 4 additions & 1 deletion src/sentry/web/frontend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
from sentry.utils.audit import create_audit_entry
from sentry.utils.auth import construct_link_with_query, is_valid_redirect
from sentry.utils.http import absolute_uri, is_using_customer_domain, origin_from_request
from sentry.web.client_config import get_client_config
from sentry.web.frontend.generic import FOREVER_CACHE
from sentry.web.helpers import render_to_response
from sudo.views import redirect_to_sudo
Expand Down Expand Up @@ -467,7 +468,9 @@ def get_not_2fa_compliant_url(self, request: HttpRequest, *args: Any, **kwargs:
return reverse("sentry-account-settings-security")

def get_context_data(self, request: HttpRequest, **kwargs: Any) -> dict[str, Any]:
context = csrf(request)
context = {"react_config": get_client_config(request, self.active_organization)} | csrf(
request
)
return context

def respond(
Expand Down

0 comments on commit 7fcb1c5

Please sign in to comment.