Skip to content

Commit 2025ed6

Browse files
authored
Hide Github and Edit buttons while in admin mode (#189)
…e currently in an Iframe ## Description Hide Github and Edit buttons while in admin mode. Clicking on them while editing the rule break in some way the website and there is no point to try to edit a rule this is already currently edited. ## Screenshot (optional) <img width="1903" height="906" alt="image" src="https://github.com/user-attachments/assets/e702760f-870d-4e9b-b932-8cc0aa4add7d" /> <!-- Check out the relevant rules - https://www.ssw.com.au/rules/use-pull-request-templates-to-communicate-expectations/ - https://www.ssw.com.au/rules/rules-to-better-pull-requests - https://www.ssw.com.au/rules/write-a-good-pull-request - https://www.ssw.com.au/rules/over-the-shoulder-prs - https://www.ssw.com.au/rules/do-you-use-co-creation-patterns -->
1 parent 3fc1f30 commit 2025ed6

File tree

1 file changed

+46
-16
lines changed

1 file changed

+46
-16
lines changed

app/[filename]/client-rule-page.tsx

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import Acknowledgements from "@/components/Acknowledgements";
1919
import { getAccessToken } from "@auth0/nextjs-auth0";
2020
import { BookmarkService } from "@/lib/bookmarkService";
2121
import Discussion from "@/components/Discussion";
22-
import { useRouter } from "next/navigation";
22+
import { useRouter, usePathname } from "next/navigation";
2323
import { ICON_SIZE } from "@/constants";
2424
import { extractUsernameFromUrl } from "@/lib/services/github";
2525
import Breadcrumbs from "@/components/Breadcrumbs";
@@ -40,7 +40,33 @@ export default function ClientRulePage(props: ClientRulePageProps) {
4040
const [authorUsername, setAuthorUsername] = useState<string | null>(null);
4141
const relatedRules = props.relatedRulesMapping || [];
4242
const router = useRouter();
43+
const pathname = usePathname();
44+
const [isAdminPage, setIsAdminPage] = useState(false);
4345
const [isLoadingUsername, setIsLoadingUsername] = useState(false);
46+
47+
useEffect(() => {
48+
// Check if we're inside TinaCMS admin iframe
49+
if (typeof window === 'undefined') return;
50+
51+
const isInIframe = window.self !== window.top;
52+
let parentIsAdmin = false;
53+
54+
if (isInIframe) {
55+
try {
56+
parentIsAdmin = window.top?.location.pathname.includes('/admin') || false;
57+
} catch (e) {
58+
// If we can't access parent URL but we're in iframe, assume it's admin
59+
// TinaCMS always renders the preview in an iframe
60+
parentIsAdmin = true;
61+
}
62+
}
63+
64+
const isAdmin = pathname?.includes('/admin') ||
65+
window.location.pathname.includes('/admin') ||
66+
parentIsAdmin;
67+
68+
setIsAdminPage(isAdmin);
69+
}, [pathname]);
4470
const ruleData = useTina({
4571
query: ruleQueryProps?.query,
4672
variables: ruleQueryProps?.variables,
@@ -194,24 +220,28 @@ export default function ClientRulePage(props: ClientRulePageProps) {
194220
</a>
195221
</p>
196222
<div className="flex items-center gap-4 text-2xl">
197-
<Bookmark
198-
ruleGuid={rule?.guid || ''}
223+
<Bookmark
224+
ruleGuid={rule?.guid || ''}
199225
isBookmarked={isBookmarked}
200226
onBookmarkToggle={(newStatus) => setIsBookmarked(newStatus)}
201227
/>
202-
<IconLink
203-
href={`/admin#/~/${sanitizedBasePath}/${rule?.uri}`}
204-
title="Edit rule"
205-
tooltipOpaque={true}
206-
children={<RiPencilLine size={ICON_SIZE} />}
207-
/>
208-
<IconLink
209-
href={`https://github.com/SSWConsulting/SSW.Rules.Content/blob/main/rules/${rule?.uri}/rule.md`}
210-
target="_blank"
211-
title="View rule on GitHub"
212-
tooltipOpaque={true}
213-
children={<RiGithubLine size={ICON_SIZE} className="rule-icon" />}
214-
/>
228+
{!isAdminPage && (
229+
<>
230+
<IconLink
231+
href={`/admin#/~/${sanitizedBasePath}/${rule?.uri}`}
232+
title="Edit rule"
233+
tooltipOpaque={true}
234+
children={<RiPencilLine size={ICON_SIZE} />}
235+
/>
236+
<IconLink
237+
href={`https://github.com/SSWConsulting/SSW.Rules.Content/blob/main/rules/${rule?.uri}/rule.md`}
238+
target="_blank"
239+
title="View rule on GitHub"
240+
tooltipOpaque={true}
241+
children={<RiGithubLine size={ICON_SIZE} className="rule-icon" />}
242+
/>
243+
</>
244+
)}
215245
</div>
216246
</div>
217247
</div>

0 commit comments

Comments
 (0)