Skip to content

Commit

Permalink
Fix x-codeSamples load when switching language tabs (#707)
Browse files Browse the repository at this point in the history
* fix x-codeSamples load when switch language tabs

* remove irrelevant changes

* set code sample text on initial state load

* add missing eol
  • Loading branch information
lucasassisrosa authored Jan 24, 2024
1 parent 71012fa commit a3b467c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import { useTypedSelector } from "@theme/ApiItem/hooks";
import merge from "lodash/merge";

import { CodeSample, Language } from "./code-snippets-types";
import { mergeCodeSampleLanguage } from "./languages";
import {
getCodeSampleSourceFromLanguage,
mergeCodeSampleLanguage,
} from "./languages";

export const languageSet: Language[] = [
{
Expand Down Expand Up @@ -198,20 +201,13 @@ function CodeSnippets({ postman, codeSamples }: Props) {
return defaultLang[0] ?? mergedLangs[0];
});
const [codeText, setCodeText] = useState<string>("");
const [codeSampleCodeText, setCodeSampleCodeText] = useState<string>("");
const [codeSampleCodeText, setCodeSampleCodeText] = useState<
string | (() => string)
>(() => getCodeSampleSourceFromLanguage(language));

useEffect(() => {
// initial active language is custom code sample
if (
language &&
language.sample &&
language.samples &&
language.samplesSources
) {
const sampleIndex = language.samples.findIndex(
(smp) => smp === language.sample
);
setCodeSampleCodeText(language.samplesSources[sampleIndex]);
if (language && !!language.sample) {
setCodeSampleCodeText(getCodeSampleSourceFromLanguage(language));
}

if (language && !!language.options) {
Expand Down Expand Up @@ -344,7 +340,9 @@ function CodeSnippets({ postman, codeSamples }: Props) {
action={{
setLanguage: setLanguage,
setSelectedVariant: setSelectedVariant,
setSelectedSample: setSelectedSample,
}}
languageSet={mergedLangs}
lazy
>
{mergedLangs.map((lang) => {
Expand All @@ -367,6 +365,7 @@ function CodeSnippets({ postman, codeSamples }: Props) {
includeSample={true}
currentLanguage={lang.language}
defaultValue={selectedSample}
languageSet={mergedLangs}
lazy
>
{lang.samples.map((sample, index) => {
Expand Down Expand Up @@ -406,6 +405,7 @@ function CodeSnippets({ postman, codeSamples }: Props) {
includeVariant={true}
currentLanguage={lang.language}
defaultValue={selectedVariant}
languageSet={mergedLangs}
lazy
>
{lang.variants.map((variant, index) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,19 @@ export function mergeCodeSampleLanguage(
return language;
});
}

export function getCodeSampleSourceFromLanguage(language: Language) {
if (
language &&
language.sample &&
language.samples &&
language.samplesSources
) {
const sampleIndex = language.samples.findIndex(
(smp) => smp === language.sample
);
return language.samplesSources[sampleIndex];
}

return "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import {
useTabs,
} from "@docusaurus/theme-common/internal";
import useIsBrowser from "@docusaurus/useIsBrowser";
import { languageSet } from "@theme/ApiExplorer/CodeSnippets";
import clsx from "clsx";

function TabList({
action,
currentLanguage,
languageSet,
includeVariant,
includeSample,
className,
Expand Down Expand Up @@ -48,16 +48,18 @@ function TabList({
)[0];
newLanguage.variant = newTabValue;
action.setSelectedVariant(newTabValue.toLowerCase());
} else if (currentLanguage && includeSample) {
newLanguage = languageSet.filter(
(lang) => lang.language === currentLanguage
)[0];
newLanguage.sample = newTabValue;
action.setSelectedSample(newTabValue);
} else {
newLanguage = languageSet.filter(
(lang) => lang.language === newTabValue
)[0];
action.setSelectedVariant(newLanguage.variant.toLowerCase());
}

if (currentLanguage && includeSample) {
newLanguage.sample = newTabValue;
action.setSelectedSample(newTabValue.toLowerCase());
action.setSelectedSample(newLanguage.sample);
}

action.setLanguage(newLanguage);
Expand Down

0 comments on commit a3b467c

Please sign in to comment.