Skip to content

Commit

Permalink
fix(plugins/force-timestamp-locale): show missing locales, make it wo…
Browse files Browse the repository at this point in the history
…rk without reloading
  • Loading branch information
PalmDevs committed Nov 5, 2024
1 parent 332afa6 commit dbc4ae1
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions plugins/force-timestamp-locale/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,35 +38,41 @@ const locales = proxyLazy(() => findByPropsLazy('momentLocales').momentLocales)
() => Promise<unknown>
>

const passthroughSymbol = Symbol.for('palmdevs.force-timestamp-locale.passthrough')

const unpatches: UnpatchFunction[] = []
let momentSetLocale: (locale: string) => void

export default {
onLoad: () => {
onLoad: async () => {
const origLocale = moment.locale()

// biome-ignore lint/suspicious/noExplicitAny: I hate Bunny typings
const patchedGetSetLocale = (set: boolean) => (args: unknown[], origFunc: any) => {
const patchedGetSetLocale = (args: unknown[], origFunc: any) => {
if (args.length === 0) return origFunc()
if (set) momentSetLocale = origFunc
if (args[1] === passthroughSymbol) return origFunc(args[0])
return args[0]
}

unpatches.push(
patcher.instead('locale', moment, patchedGetSetLocale(true)),
patcher.instead('lang', moment, patchedGetSetLocale(false)),
() => momentSetLocale(origLocale),
patcher.instead('locale', moment, patchedGetSetLocale),
patcher.instead('lang', moment, patchedGetSetLocale),
() => moment.locale(origLocale, passthroughSymbol),
)

onLocaleUpdate(storage.get('locale')!)
for (const locale in locales) {
await locales[locale]()
}

moment.locale(storage.get('locale')!, passthroughSymbol)
},
onUnload: () => {
for (const unpatch of unpatches) unpatch()
},
settings: () => {
const savedLocale = storage.get('locale')
const savedLocale = storage.get('locale')!
const [_, forceUpdate] = React.useReducer(x => ~x, 0)

onLocaleUpdate(savedLocale!)
moment.locale(savedLocale, passthroughSymbol)

return (
<ReactNative.ScrollView style={{ flex: 1 }}>
Expand All @@ -79,7 +85,7 @@ export default {
forceUpdate()
}}
>
{Object.keys(locales).map(locale => {
{moment.locales().map((locale: string) => {
return <TableRadioRow key={locale} label={locale} value={locale} />
})}
</TableRadioGroup>
Expand All @@ -88,7 +94,3 @@ export default {
)
},
}

const onLocaleUpdate = (newLocale: string) => {
locales[newLocale!]().then(() => momentSetLocale(newLocale))
}

0 comments on commit dbc4ae1

Please sign in to comment.