MCPcopy
hub / github.com/algorithm-visualizer/algorithm-visualizer / loadAlgorithm

Method loadAlgorithm

src/components/App/index.js:147–195  ·  view source on GitHub ↗
({ categoryKey, algorithmKey, gistId }, { visualizationId })

Source from the content-addressed store, hash-verified

145 }
146
147 loadAlgorithm({ categoryKey, algorithmKey, gistId }, { visualizationId }) {
148 const { ext } = this.props.env;
149 const fetch = () => {
150 if (window.__PRELOADED_ALGORITHM__) {
151 this.props.setAlgorithm(window.__PRELOADED_ALGORITHM__);
152 delete window.__PRELOADED_ALGORITHM__;
153 } else if (window.__PRELOADED_ALGORITHM__ === null) {
154 delete window.__PRELOADED_ALGORITHM__;
155 return Promise.reject(new Error('Algorithm Not Found'));
156 } else if (categoryKey && algorithmKey) {
157 return AlgorithmApi.getAlgorithm(categoryKey, algorithmKey)
158 .then(({ algorithm }) => this.props.setAlgorithm(algorithm));
159 } else if (gistId === 'new' && visualizationId) {
160 return VisualizationApi.getVisualization(visualizationId)
161 .then(content => {
162 this.props.setScratchPaper({
163 login: undefined,
164 gistId,
165 title: 'Untitled',
166 files: [SCRATCH_PAPER_README_MD, createUserFile('visualization.json', JSON.stringify(content))],
167 });
168 });
169 } else if (gistId === 'new') {
170 const language = languages.find(language => language.ext === ext);
171 this.props.setScratchPaper({
172 login: undefined,
173 gistId,
174 title: 'Untitled',
175 files: [SCRATCH_PAPER_README_MD, language.skeleton],
176 });
177 } else if (gistId) {
178 return GitHubApi.getGist(gistId, { timestamp: Date.now() })
179 .then(refineGist)
180 .then(this.props.setScratchPaper);
181 } else {
182 this.props.setHome();
183 }
184 return Promise.resolve();
185 };
186 fetch()
187 .then(() => {
188 this.selectDefaultTab();
189 return null; // to suppress unnecessary bluebird warning
190 })
191 .catch(error => {
192 this.handleError(error);
193 this.props.history.push('/');
194 });
195 }
196
197 selectDefaultTab() {
198 const { ext } = this.props.env;

Callers 2

componentDidMountMethod · 0.95

Calls 2

selectDefaultTabMethod · 0.95
handleErrorMethod · 0.80

Tested by

no test coverage detected