-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add /icebreakers path to integrate with Icebreakers (#123)
* Add topic path to local dev server * Add topic page * Fix threadComments when allComments is undefined * Update layout * Fix the create topic flow * Change /topic to /icebreakers * Add 'cancel' and 'load...' functions * Add Icebreakers styling * Add icebreakers functionality
- Loading branch information
Showing
10 changed files
with
420 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
declare global { | ||
interface Window { | ||
getQuestion: (slug: string) => Promise<string> | ||
} | ||
} | ||
const storageKey = "icebreakerQuestions" | ||
const timeStampKey = "icebreakerQuestionsTimeStamp" | ||
|
||
const toSlug = (str: string): string => | ||
str | ||
.toLowerCase() | ||
.replace(/ /g, "-") | ||
.replace(/[^\w-]+/g, "") | ||
.replace(/_/g, "") | ||
.replace(/-{2,}/g, "-") | ||
.replace(/-$/, "") | ||
.replace(/^-/, "") | ||
|
||
const isSlugMatch = (slug: string, question: string) => | ||
slug === toSlug(question) | ||
|
||
const reverseSlug = (slug: string, questions: string[]) => | ||
questions.find(question => isSlugMatch(slug, question)) | ||
|
||
const fetchAndStoreQuestions = () => | ||
new Promise<string[]>((resolve, reject) => { | ||
const currentTimestamp = | ||
document?.getElementById("questions-time-stamp")?.innerText || "0" | ||
const storedTimestamp = localStorage.getItem(timeStampKey) | ||
|
||
const isStoredQuestionsValid = | ||
storedTimestamp && parseInt(storedTimestamp) >= parseInt(currentTimestamp) | ||
|
||
const storedQuestions = localStorage.getItem(storageKey) | ||
if (isStoredQuestionsValid && storedQuestions) { | ||
resolve(JSON.parse(storedQuestions)) | ||
return | ||
} | ||
|
||
fetchQuestions(currentTimestamp, resolve, reject) | ||
}) | ||
|
||
const fetchQuestions = async (currentTimestamp, resolve, reject) => { | ||
try { | ||
const questionFile = await fetch( | ||
"https://raw.githubusercontent.com/rendall/icebreakers/master/QUESTIONS.md" | ||
) | ||
const questionsText = await questionFile.text() | ||
const questionLines = questionsText.split("\n") | ||
const questions = questionLines | ||
.filter(line => /^ {2}\* [A-Z]/.test(line)) | ||
.map(line => line.slice(4)) | ||
|
||
localStorage.setItem(storageKey, JSON.stringify(questions)) | ||
localStorage.setItem(timeStampKey, currentTimestamp) | ||
resolve(questions) | ||
} catch (error) { | ||
reject(error) | ||
} | ||
} | ||
|
||
const getQuestion = (slug: string) => | ||
new Promise<string>((resolve, reject) => { | ||
fetchAndStoreQuestions() | ||
.then(questions => { | ||
const question = reverseSlug(slug, questions) | ||
if (question) { | ||
resolve(question) | ||
} else { | ||
reject("No question found") | ||
} | ||
}) | ||
.catch(reject) | ||
}) | ||
|
||
window.getQuestion = getQuestion | ||
|
||
export default getQuestion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
/topic/* /topic/index.html 200 | ||
/icebreakers/* /icebreakers/index.html 200 |
Oops, something went wrong.