-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #185 from jaczhi/jaczhi/chat-hide
Use local storage to hide chat channels in UI
- Loading branch information
Showing
4 changed files
with
239 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { createSignal } from "solid-js"; | ||
|
||
export function useChatState<T>(keyName: string, defaultValue: T) { | ||
const [chatState, setChatState] = createSignal<T>( | ||
JSON.parse(localStorage.getItem(keyName) || JSON.stringify(defaultValue)) | ||
); | ||
|
||
const updateChatState = (newValue: T) => { | ||
setChatState(() => newValue); | ||
localStorage.setItem(keyName, JSON.stringify(newValue)); | ||
}; | ||
|
||
return { chatState, updateChatState }; | ||
} |
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,93 @@ | ||
import { Dialog, DialogTitle, DialogContent, DialogActions, Button, List, ListItem, Checkbox, FormControlLabel } from '@suid/material' | ||
Check failure on line 1 in src/components/chat/toggle-visibility-dialog.tsx
|
||
import { createSignal, createEffect, For } from 'solid-js' | ||
|
||
interface ToggleChannelVisibilityDialogProps { | ||
open: boolean | ||
channels: () => string[] // visible channels | ||
hiddenChannels: () => string[] // hidden channels | ||
setHiddenChannels: (channels: string[]) => void | ||
onClose: () => void | ||
} | ||
|
||
export function ToggleChannelVisibilityDialog(props: ToggleChannelVisibilityDialogProps) { | ||
// Track all displayed channels and their checked state | ||
const [selectedChannels, setSelectedChannels] = createSignal<string[]>([]) | ||
|
||
// Initialize selected channels when dialog opens | ||
createEffect(() => { | ||
if (props.open) { | ||
// Start with currently visible channels | ||
setSelectedChannels([...props.channels()]) | ||
} | ||
}) | ||
|
||
const handleToggleChannel = (channel: string, checked: boolean) => { | ||
if (checked) { | ||
setSelectedChannels([...selectedChannels(), channel]) | ||
} else { | ||
setSelectedChannels(selectedChannels().filter(c => c !== channel)) | ||
} | ||
} | ||
|
||
const handleSave = () => { | ||
if (selectedChannels().length === 0) { | ||
// At least one channel must be visible | ||
return | ||
} | ||
|
||
// Calculate which channels should be hidden (all channels minus selected ones) | ||
const allChannels = [...props.channels(), ...props.hiddenChannels()] | ||
const newHiddenChannels = allChannels.filter( | ||
Check failure on line 40 in src/components/chat/toggle-visibility-dialog.tsx
|
||
channel => !selectedChannels().includes(channel) | ||
) | ||
props.setHiddenChannels(newHiddenChannels) | ||
props.onClose() | ||
} | ||
|
||
const isChannelSelected = (channel: string) => { | ||
return selectedChannels().includes(channel) | ||
} | ||
|
||
// Get all channels (visible and hidden) | ||
const allChannels = () => { | ||
const combined = [...props.channels(), ...props.hiddenChannels()] | ||
return [...new Set(combined)].sort() // Remove duplicates and sort | ||
} | ||
|
||
return ( | ||
<Dialog | ||
Check failure on line 58 in src/components/chat/toggle-visibility-dialog.tsx
|
||
open={props.open} | ||
onClose={props.onClose} | ||
fullWidth | ||
maxWidth="sm" | ||
> | ||
<DialogTitle>Toggle Channel Visibility</DialogTitle> | ||
<DialogContent> | ||
<List sx={{ minWidth: '300px' }}> | ||
<For each={allChannels()}> | ||
{(channel) => ( | ||
<ListItem disablePadding> | ||
<FormControlLabel | ||
control={ | ||
<Checkbox | ||
checked={isChannelSelected(channel)} | ||
onChange={(_, checked) => handleToggleChannel(channel, checked)} | ||
/> | ||
} | ||
label={`#${channel}`} | ||
sx={{ width: '100%', margin: '4px 0' }} | ||
/> | ||
</ListItem> | ||
)} | ||
</For> | ||
</List> | ||
</DialogContent> | ||
<DialogActions> | ||
<Button onClick={props.onClose}>Cancel</Button> | ||
<Button onClick={handleSave} disabled={selectedChannels().length === 0} color="primary"> | ||
Save | ||
</Button> | ||
</DialogActions> | ||
</Dialog> | ||
) | ||
} |