Skip to content

Commit f7254ef

Browse files
authored
[playground] Persist open tabs on compiler error (facebook#34673)
This change allows it so that tabs that were open before a compiler error are automatically opened again when the error is resolved. Quality of life change for those especially working with the advanced view of the playground. https://github.com/user-attachments/assets/cd2dc117-e6fc-4f57-a08f-259757c4f5e8
1 parent 79ca5ae commit f7254ef

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

compiler/apps/playground/components/AccordionWindow.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default function AccordionWindow(props: {
2323
tabsOpen: Set<string>;
2424
setTabsOpen: (newTab: Set<string>) => void;
2525
changedPasses: Set<string>;
26+
isFailure: boolean;
2627
}): React.ReactElement {
2728
return (
2829
<div className="flex-1 min-w-[550px] sm:min-w-0">
@@ -36,6 +37,7 @@ export default function AccordionWindow(props: {
3637
tabsOpen={props.tabsOpen}
3738
setTabsOpen={props.setTabsOpen}
3839
hasChanged={props.changedPasses.has(name)}
40+
isFailure={props.isFailure}
3941
/>
4042
);
4143
})}
@@ -50,15 +52,17 @@ function AccordionWindowItem({
5052
tabsOpen,
5153
setTabsOpen,
5254
hasChanged,
55+
isFailure,
5356
}: {
5457
name: string;
5558
tabs: TabsRecord;
5659
tabsOpen: Set<string>;
5760
setTabsOpen: (newTab: Set<string>) => void;
5861
hasChanged: boolean;
62+
isFailure: boolean;
5963
}): React.ReactElement {
6064
const id = useId();
61-
const isShow = tabsOpen.has(name);
65+
const isShow = isFailure ? name === 'Output' : tabsOpen.has(name);
6266

6367
const transitionName = `accordion-window-item-${id}`;
6468

compiler/apps/playground/components/Editor/Output.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -265,14 +265,8 @@ function OutputContent({store, compilerOutput}: Props): JSX.Element {
265265
* Update the active tab back to the output or errors tab when the compilation state
266266
* changes between success/failure.
267267
*/
268-
const [previousOutputKind, setPreviousOutputKind] = useState(
269-
compilerOutput.kind,
270-
);
271-
if (compilerOutput.kind !== previousOutputKind) {
272-
setPreviousOutputKind(compilerOutput.kind);
273-
setTabsOpen(new Set(['Output']));
274-
setActiveTab('Output');
275-
}
268+
269+
const isFailure = compilerOutput.kind !== 'ok';
276270
const changedPasses: Set<string> = new Set(['Output', 'HIR']); // Initial and final passes should always be bold
277271
let lastResult: string = '';
278272
for (const [passName, results] of compilerOutput.results) {
@@ -301,6 +295,8 @@ function OutputContent({store, compilerOutput}: Props): JSX.Element {
301295
tabs={tabs}
302296
activeTab={activeTab}
303297
onTabChange={setActiveTab}
298+
// Display the Output tab on compilation failure
299+
activeTabOverride={isFailure ? 'Output' : undefined}
304300
/>
305301
</ViewTransition>
306302
);
@@ -319,6 +315,7 @@ function OutputContent({store, compilerOutput}: Props): JSX.Element {
319315
tabsOpen={tabsOpen}
320316
tabs={tabs}
321317
changedPasses={changedPasses}
318+
isFailure={isFailure}
322319
/>
323320
</ViewTransition>
324321
);

compiler/apps/playground/components/TabbedWindow.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ export default function TabbedWindow({
1717
tabs,
1818
activeTab,
1919
onTabChange,
20+
activeTabOverride,
2021
}: {
2122
tabs: Map<string, React.ReactNode>;
2223
activeTab: string;
2324
onTabChange: (tab: string) => void;
25+
activeTabOverride?: string;
2426
}): React.ReactElement {
27+
const currentActiveTab = activeTabOverride ? activeTabOverride : activeTab;
28+
2529
const id = useId();
2630
const transitionName = `tab-highlight-${id}`;
2731

@@ -37,7 +41,7 @@ export default function TabbedWindow({
3741
<div className="flex flex-col h-full max-w-full">
3842
<div className="flex p-2 flex-shrink-0">
3943
{Array.from(tabs.keys()).map(tab => {
40-
const isActive = activeTab === tab;
44+
const isActive = currentActiveTab === tab;
4145
return (
4246
<button
4347
key={tab}
@@ -49,6 +53,8 @@ export default function TabbedWindow({
4953
{isActive && (
5054
<ViewTransition
5155
name={transitionName}
56+
enter={{default: 'none'}}
57+
exit={{default: 'none'}}
5258
share={{
5359
[TOGGLE_TAB_TRANSITION]: 'tab-highlight',
5460
default: 'none',
@@ -58,6 +64,8 @@ export default function TabbedWindow({
5864
</ViewTransition>
5965
)}
6066
<ViewTransition
67+
enter={{default: 'none'}}
68+
exit={{default: 'none'}}
6169
update={{
6270
[TOGGLE_TAB_TRANSITION]: 'tab-text',
6371
default: 'none',
@@ -69,7 +77,7 @@ export default function TabbedWindow({
6977
})}
7078
</div>
7179
<div className="flex-1 overflow-hidden w-full h-full">
72-
{tabs.get(activeTab)}
80+
{tabs.get(currentActiveTab)}
7381
</div>
7482
</div>
7583
</div>

0 commit comments

Comments
 (0)