generated from pengx17/logseq-plugin-template-react
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
Showing
6 changed files
with
90 additions
and
45 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 was deleted.
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,56 @@ | ||
import React, { useContext } from 'react'; | ||
import { AppUserConfigs } from '@logseq/libs/dist/LSPlugin'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
// @ts-ignore | ||
const UserConfigsContext = React.createContext<AppUserConfigs>({ | ||
preferredLanguage: 'en', | ||
preferredThemeMode: 'light', | ||
preferredFormat: 'markdown', | ||
preferredWorkflow: 'now', | ||
preferredTodo: 'LATER', | ||
preferredDateFormat: 'MMM do, yyyy', | ||
}); | ||
|
||
function fixPreferredDateFormat(preferredDateFormat: string) { | ||
const format = preferredDateFormat | ||
.replace('yyyy', 'YYYY') | ||
.replace('dd', 'DD') | ||
.replace('do', 'Do') | ||
.replace('EEEE', 'dddd') | ||
.replace('EEE', 'ddd') | ||
.replace('EE', 'dd') | ||
.replace('E', 'dd'); | ||
return format; | ||
} | ||
|
||
export const withUserConfigs = <P extends {}>( | ||
WrapComponent: React.ComponentType<P>, | ||
) => { | ||
const WithUserConfigsComponent: typeof WrapComponent = (props) => { | ||
const [configs, setConfigs] = useState<AppUserConfigs>(); | ||
|
||
useEffect(() => { | ||
window.logseq.App.getUserConfigs().then((configs) => { | ||
setConfigs({ | ||
...configs, | ||
preferredDateFormat: fixPreferredDateFormat(configs.preferredDateFormat), | ||
}); | ||
}); | ||
}, []); | ||
|
||
return ( | ||
<UserConfigsContext.Provider value={configs!}> | ||
<WrapComponent {...props} /> | ||
</UserConfigsContext.Provider> | ||
); | ||
}; | ||
return WithUserConfigsComponent; | ||
}; | ||
|
||
const useUserConfigs = () => { | ||
const configs = useContext(UserConfigsContext); | ||
return configs; | ||
}; | ||
|
||
export default useUserConfigs; |
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