-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: update switch component and add Splunk global context
- Loading branch information
1 parent
8ca0fdf
commit 24dc5dc
Showing
4 changed files
with
183 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* eslint-disable react/display-name */ | ||
import { CodeComponentMeta } from '@plasmicapp/host'; | ||
import React from 'react'; | ||
import SwitchPrimitive from '@/common/components/atom/switch'; | ||
|
||
type SwitchType = { | ||
onCheckedChange?: (checked: boolean) => void; | ||
checked?: any; | ||
disabled?: boolean; | ||
className?: string; | ||
name?: string; | ||
}; | ||
|
||
export const Switch = (props: SwitchType) => { | ||
const { checked, onCheckedChange, disabled, className, name } = props; | ||
return ( | ||
<SwitchPrimitive | ||
checked={checked} | ||
onChange={e => onCheckedChange?.(e.target.checked)} | ||
className={className} | ||
disabled={disabled} | ||
name={name} | ||
/> | ||
); | ||
}; | ||
|
||
export const switchMeta: CodeComponentMeta<SwitchType> = { | ||
name: 'Switch', | ||
displayName: 'Fragment/Switch', | ||
importPath: '@/common/fragment/components/switch', | ||
figmaMappings: [{ figmaComponentName: 'Switch' }], | ||
props: { | ||
checked: 'boolean', | ||
disabled: 'boolean', | ||
name: { | ||
type: 'string', | ||
advanced: true, | ||
description: 'The HTML name of the switch', | ||
}, | ||
onCheckedChange: { | ||
type: 'eventHandler', | ||
argTypes: [{ name: 'checked', type: 'boolean' }], | ||
}, | ||
}, | ||
classNameProp: 'className', | ||
states: { | ||
checked: { | ||
type: 'writable', | ||
variableType: 'text', | ||
valueProp: 'checked', | ||
onChangeProp: 'onCheckedChange', | ||
}, | ||
}, | ||
}; |
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,123 @@ | ||
import React, { useMemo } from 'react'; | ||
import { GlobalActionsProvider, GlobalContextMeta } from '@plasmicapp/host'; | ||
|
||
type SplunkProps = React.PropsWithChildren<{ | ||
defaultApiHost: string; | ||
defaultApiKey: string; | ||
}>; | ||
|
||
export const Splunk = ({ children, defaultApiHost, defaultApiKey }: SplunkProps) => { | ||
const actions = useMemo( | ||
() => ({ | ||
sendLog: async (data: Record<string, any>, apiHost?: string, apiKey?: string) => { | ||
try { | ||
const response = await fetch(`${apiHost ?? defaultApiHost}/services/collector`, { | ||
method: 'POST', | ||
headers: { | ||
'Authorization': `Splunk ${apiKey ?? defaultApiKey}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
sourcetype: '_json', | ||
event: { | ||
...data, | ||
}, | ||
}), | ||
}); | ||
return response.json(); | ||
} catch (error) { | ||
return undefined; | ||
} | ||
}, | ||
sendBatchLog: async (dataList: Record<string, any>[], apiHost?: string, apiKey?: string) => { | ||
try { | ||
const response = await fetch(`${apiHost ?? defaultApiHost}/services/collector`, { | ||
method: 'POST', | ||
headers: { | ||
'Authorization': `Splunk ${apiKey ?? defaultApiKey}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify( | ||
dataList.map(data => ({ | ||
sourcetype: '_json', | ||
event: { | ||
...data, | ||
}, | ||
})), | ||
), | ||
}); | ||
return response.json(); | ||
} catch (error) { | ||
return undefined; | ||
} | ||
}, | ||
}), | ||
[defaultApiKey, defaultApiHost], | ||
); | ||
|
||
return ( | ||
<GlobalActionsProvider contextName="Splunk" actions={actions}> | ||
{children} | ||
</GlobalActionsProvider> | ||
); | ||
}; | ||
|
||
export const splunkMeta: GlobalContextMeta<SplunkProps> = { | ||
name: 'Splunk', | ||
displayName: 'Fragment/Splunk', | ||
props: { | ||
defaultApiHost: { | ||
type: 'string', | ||
displayName: 'Default API Host', | ||
defaultValueHint: 'https://mysplunkserver.example.com', | ||
}, | ||
defaultApiKey: { | ||
type: 'string', | ||
displayName: 'Default API Key', | ||
}, | ||
}, | ||
globalActions: { | ||
sendLog: { | ||
displayName: 'Send Log', | ||
parameters: [ | ||
{ | ||
name: 'data', | ||
displayName: 'Data', | ||
type: 'object', | ||
}, | ||
{ | ||
name: 'apiHost', | ||
displayName: 'API Host (Optional)', | ||
type: 'string', | ||
}, | ||
{ | ||
name: 'apiKey', | ||
displayName: 'API Key (Optional)', | ||
type: 'string', | ||
}, | ||
], | ||
}, | ||
sendBatchLog: { | ||
displayName: 'Send Batch Log', | ||
parameters: [ | ||
{ | ||
name: 'dataList', | ||
displayName: 'Data List', | ||
type: 'object', | ||
}, | ||
{ | ||
name: 'apiHost', | ||
displayName: 'API Host (Optional)', | ||
type: 'string', | ||
}, | ||
{ | ||
name: 'apiKey', | ||
displayName: 'API Key (Optional)', | ||
type: 'string', | ||
}, | ||
], | ||
}, | ||
}, | ||
providesData: true, | ||
importPath: '@/common/fragment/splunk', | ||
}; |
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