-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f36c5b
commit 1be2762
Showing
20 changed files
with
292 additions
and
207 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,13 @@ | ||
import { Trigger } from './components/Trigger' | ||
import { ContentThemeProvider } from '@/components/ContentThemeProvider' | ||
import { StorageProvider } from '@/context/storageContext' | ||
|
||
export function App() { | ||
return ( | ||
<ContentThemeProvider> | ||
<StorageProvider> | ||
<Trigger /> | ||
</StorageProvider> | ||
</ContentThemeProvider> | ||
) | ||
} |
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
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,72 @@ | ||
import React, { createContext, useContext, useState, useCallback } from 'react' | ||
import { createChromeStorageStateHookLocal } from 'use-chrome-storage' | ||
|
||
import packageJson from '../../package.json' | ||
import type { StorageData } from '@/types' | ||
|
||
const STORAGE_KEY = 'data' | ||
|
||
const DEFAULT_STORAGE_DATA: StorageData['data'] = { | ||
meta: { | ||
version: packageJson.version, | ||
updateAt: new Date().toISOString(), | ||
}, | ||
contents: { | ||
courseList: [{ id: '-1', title: '전체' }], | ||
activityList: [], | ||
}, | ||
settings: { | ||
refreshInterval: 1000 * 60 * 20, // 20 minutes | ||
triggerImage: chrome.runtime.getURL('/assets/Lee-Gil-ya.webp'), | ||
}, | ||
} | ||
|
||
interface StorageContextType { | ||
data: StorageData['data'] | null | ||
isLoading: boolean | ||
updateData: (newData: Partial<StorageData['data']>) => void | ||
} | ||
|
||
const StorageContext = createContext<StorageContextType | undefined>(undefined) | ||
|
||
export function StorageProvider({ children }: { children: React.ReactNode }) { | ||
const [storageHook] = useState(() => createChromeStorageStateHookLocal(STORAGE_KEY, DEFAULT_STORAGE_DATA)) | ||
const [storage, setStorage, , , isInitialStateResolved] = storageHook() | ||
|
||
const updateData = useCallback( | ||
(newData: Partial<StorageData['data']>) => { | ||
setStorage(prevData => ({ | ||
...prevData, | ||
settings: { | ||
...prevData.settings, | ||
...newData.settings, | ||
}, | ||
contents: { | ||
...prevData.contents, | ||
...newData.contents, | ||
}, | ||
meta: { | ||
...prevData.meta, | ||
...newData.meta, | ||
}, | ||
})) | ||
}, | ||
[setStorage], | ||
) | ||
|
||
const value = { | ||
data: storage, | ||
isLoading: !isInitialStateResolved, | ||
updateData, | ||
} | ||
|
||
return <StorageContext.Provider value={value}>{children}</StorageContext.Provider> | ||
} | ||
|
||
export function useStorage() { | ||
const context = useContext(StorageContext) | ||
if (context === undefined) { | ||
throw new Error('useStorage must be used within a StorageProvider') | ||
} | ||
return context | ||
} |
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 |
---|---|---|
|
@@ -622,5 +622,4 @@ export const contentsData: Contents = { | |
title: '고급웹프로그래밍', | ||
}, | ||
], | ||
updateAt: '2023-10-24T11:24:04.212Z', | ||
} |
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,26 @@ | ||
import { useEffect } from 'react' | ||
|
||
import { useContentsFetcher } from './useContentsFetcher' | ||
import { useRefreshCheck } from './useRefreshCheck' | ||
import { useStorage } from '@/context/storageContext' | ||
|
||
export const useContents = () => { | ||
const { | ||
data: { contents }, | ||
} = useStorage() | ||
const { shouldRefresh } = useRefreshCheck() | ||
const { fetchContents, isLoading, progress } = useContentsFetcher() | ||
|
||
useEffect(() => { | ||
if (shouldRefresh || !contents.courseList.length) { | ||
fetchContents() | ||
} | ||
}, [shouldRefresh, contents.courseList.length]) | ||
|
||
return { | ||
contents, | ||
isLoading, | ||
progress, | ||
refetch: fetchContents, | ||
} | ||
} |
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,42 @@ | ||
import { useState } from 'react' | ||
|
||
import { useStorage } from '@/context/storageContext' | ||
import { getActivities, getAssignmentSubmitted, getCourses, getVideoSubmitted } from '@/services' | ||
|
||
export const useContentsFetcher = () => { | ||
const [isLoading, setIsLoading] = useState(false) | ||
const [progress, setProgress] = useState(0) | ||
const { data, updateData } = useStorage() | ||
|
||
const fetchContents = async () => { | ||
if (isLoading) { | ||
return | ||
} | ||
|
||
setIsLoading(true) | ||
setProgress(0) | ||
|
||
const courseList = await getCourses() | ||
const maxPos = courseList.length * 2 | ||
|
||
const activityList = await Promise.all( | ||
courseList.map(async (course, index) => { | ||
const assignmentSubmittedArray = await getAssignmentSubmitted(course.id) | ||
setProgress(((index * 2 + 1) / maxPos) * 100) | ||
const videoSubmittedArray = await getVideoSubmitted(course.id) | ||
setProgress(((index * 2 + 2) / maxPos) * 100) | ||
return getActivities(course.title, course.id, assignmentSubmittedArray, videoSubmittedArray) | ||
}), | ||
).then(activityList => activityList.flat()) | ||
|
||
updateData({ | ||
contents: { courseList: [{ id: '-1', title: '전체' }, ...courseList], activityList }, | ||
meta: { ...data.meta, updateAt: new Date().toISOString() }, | ||
}) | ||
|
||
setIsLoading(false) | ||
setProgress(0) | ||
} | ||
|
||
return { fetchContents, isLoading, progress } | ||
} |
Oops, something went wrong.